Skip to content

Commit

Permalink
Fixed quote to heading transformation. (#5088)
Browse files Browse the repository at this point in the history
This commit corrects the logic of quote to heading transform.
The logic is now the following:
If no citation and no content exist an empty heading is created.
If citation exists but the content does not exist a heading is created with citation as the content.
In other citations the first paragraph of the quote is transformed into and heading and new quote is created equal to the original but with the first paragraph removed. If the quote just contains one paragraph the new quote will contain just the citation.
  • Loading branch information
jorgefilipecosta authored Feb 16, 2018
1 parent 1076953 commit 0992d4e
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 0992d4e

Please sign in to comment.