Skip to content

Commit

Permalink
Fix list to paragraph transform
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jan 14, 2018
1 parent ae9b07f commit c831336
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions blocks/library/list/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,6 +17,41 @@ import { registerBlockType, createBlock } from '../../api';
import Editable from '../../editable';
import BlockControls from '../../block-controls';

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( 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.' ),
Expand Down Expand Up @@ -104,20 +139,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: <p>{ children }</p> } ) ),
citation: ( values.length === 1 ? undefined : [ get( last( values ), 'props.children' ) ] ),
value: listItemsToArray( values )
.map( ( content ) => ( { children: <p>{ content }</p> } ) ),
} );
},
},
Expand Down

0 comments on commit c831336

Please sign in to comment.