Skip to content

Commit

Permalink
Fixed quote to heading transformation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Feb 16, 2018
1 parent 14df09b commit bbce154
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions blocks/library/quote/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { isString, get } from 'lodash';
import { castArray, get, isString } from 'lodash';
import classnames from 'classnames';

/**
Expand Down Expand Up @@ -122,32 +122,41 @@ export const settings = {
type: 'block',
blocks: [ 'core/heading' ],
transform: ( { value, citation, ...attrs } ) => {
const textElement = value[ 0 ];
if ( ! textElement ) {
// if no text content exist just transform the quote into an heading block
// using citation as the content, it may be empty creating an empty heading block.
if ( ( ! value || ! value.length ) ) {
return createBlock( 'core/heading', {
content: citation,
} );
}
const textContent = isString( textElement.children ) ?
textElement.children :
textElement.children.props.children;
if ( Array.isArray( value ) || citation ) {
const text = createBlock( 'core/heading', {
content: textContent,
} );
const quote = createBlock( 'core/quote', {
...attrs,
citation,
value: Array.isArray( value ) ?
value.slice( 1 ) :
[],
} );

return [ text, quote ];
const firstValue = get( value, [ 0, 'children' ] );
const headingContent = castArray( isString( firstValue ) ?
firstValue :
get( firstValue, [ 'props', 'children' ], '' )
);

// if the quote content just contains a paragraph and no citation exist
// convert the quote content into and heading block.
if ( ! citation && value.length === 1 ) {
return createBlock( 'core/heading', {
content: headingContent,
} );
}
return createBlock( 'core/heading', {
content: textContent,

// In the normal case convert the first paragraph of quote into an heading
// and create a new quote block equal tl what we had excluding the first paragraph
const heading = createBlock( 'core/heading', {
content: headingContent,
} );

const quote = createBlock( 'core/quote', {
...attrs,
citation,
value: value.slice( 1 ),
} );

return [ heading, quote ];
},
},
],
Expand Down

0 comments on commit bbce154

Please sign in to comment.