diff --git a/blocks/library/list/index.js b/blocks/library/list/index.js index 3e538c294170d2..d128a5754e2536 100644 --- a/blocks/library/list/index.js +++ b/blocks/library/list/index.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { find, compact, get, initial, last, isEmpty } from 'lodash'; +import { find, get, last, isEmpty, castArray } from 'lodash'; /** * WordPress dependencies @@ -17,6 +17,47 @@ import { registerBlockType, createBlock } from '../../api'; import Editable from '../../editable'; import BlockControls from '../../block-controls'; +/** + * Converts an array of list items to an array of content. + * + * @param {Array} items Array of React list items. + * @return {Array} Array of Editable value arrays. + */ +function listItemsToArray( items ) { + return items.reduce( ( acc, item ) => { + // Ignore spacing between list items. + if ( typeof item === 'string' ) { + return acc; + } + + const listContent = castArray( get( item, 'props.children', [] ) ); + + listContent.forEach( ( content, index ) => { + if ( content.type === 'ol' || content.type === 'ul' ) { + acc.push( ...listItemsToArray( castArray( get( content, 'props.children', [] ) ) ) ); + acc.push( [] ); + } else { + // No line breaks form prettier. + if ( ! /\S/.test( content ) ) { + return; + } + + if ( index === 0 ) { + acc.push( [] ); + } + + last( acc ).push( content ); + } + } ); + + if ( last( acc ).length === 0 ) { + acc.splice( -1, 1 ); + } + + return acc; + }, [] ); +} + registerBlockType( 'core/list', { title: __( 'List' ), description: __( 'List. Numbered or bulleted.' ), @@ -104,20 +145,16 @@ registerBlockType( 'core/list', { type: 'block', blocks: [ 'core/paragraph' ], transform: ( { values } ) => - compact( values.map( ( value ) => get( value, 'props.children', null ) ) ) - .map( ( content ) => createBlock( 'core/paragraph', { - content: [ content ], - } ) ), + listItemsToArray( values ) + .map( ( content ) => createBlock( 'core/paragraph', { content } ) ), }, { type: 'block', blocks: [ 'core/quote' ], transform: ( { values } ) => { return createBlock( 'core/quote', { - value: compact( ( values.length === 1 ? values : initial( values ) ) - .map( ( value ) => get( value, 'props.children', null ) ) ) - .map( ( children ) => ( { children:

{ children }

} ) ), - citation: ( values.length === 1 ? undefined : [ get( last( values ), 'props.children' ) ] ), + value: listItemsToArray( values ) + .map( ( content ) => ( { children:

{ content }

} ) ), } ); }, },