Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed quote to heading transformation. #5088

Merged
merged 1 commit into from
Feb 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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