From bbce154e23d502102baf1f76b11e5f3bd3d3b158 Mon Sep 17 00:00:00 2001 From: Jorge Date: Thu, 15 Feb 2018 18:14:11 +0000 Subject: [PATCH] Fixed quote to heading transformation. --- blocks/library/quote/index.js | 49 +++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/blocks/library/quote/index.js b/blocks/library/quote/index.js index de4119dd887679..f96f6d4b98ea6e 100644 --- a/blocks/library/quote/index.js +++ b/blocks/library/quote/index.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { isString, get } from 'lodash'; +import { castArray, get, isString } from 'lodash'; import classnames from 'classnames'; /** @@ -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 ]; }, }, ],