Skip to content

Commit

Permalink
Rework solution to use special core/nextpage block which means format…
Browse files Browse the repository at this point in the history
…ting does not get removed during raw-handling transforms
  • Loading branch information
mpkelly committed Nov 6, 2023
1 parent c2c6e11 commit cd0bcc2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
27 changes: 21 additions & 6 deletions packages/blocks/src/api/raw-handling/nested-list-converted.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* Internal dependencies
*/

function isList( node ) {
return node.nodeName === 'OL' || node.nodeName === 'UL';
}
Expand All @@ -10,6 +6,22 @@ function createLineBreak( doc ) {
return doc.createElement( 'br' );
}

function getDepth( listNode, depth = 2 ) {
if ( isList( listNode.parentElement ) ) {
return getDepth( listNode.parentElement, depth + 2 );
}
return depth;
}

function createSpace( length ) {
return (
Array.from( { length } )
// eslint-disable-next-line no-unused-vars
.map( ( i ) => `\u00A0\u00A0` )
.join( ' ' )
);
}

function createBullet( doc, child, isNested ) {
const node = doc.createElement( 'pre' );
const isOrdered = child.parentElement.nodeName === 'OL';
Expand All @@ -22,7 +34,9 @@ function createBullet( doc, child, isNested ) {
bullet = `${ index + 1 }. `;
}
if ( isNested ) {
bullet = ` ${ bullet }`;
const space = createSpace( getDepth( child.parentElement ) );

bullet = `${ space }${ bullet }`;
}
node.innerText = bullet;
return node;
Expand All @@ -47,7 +61,8 @@ export default function nestedListedConverter( node, doc ) {
if ( isList( node ) ) {
const nodes = transformList( node );
if ( nodes.length ) {
const wrapper = doc.createElement( 'p' );
const wrapper = doc.createElement( 'wp-block' );
wrapper.dataset.block = 'core/nextpage';
nodes.forEach( ( child ) => {
wrapper.appendChild( child );
} );
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/api/raw-handling/paste-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function filterInlineHTML( HTML, preserveWhiteSpace ) {
HTML = deepFilterHTML( HTML, [
headRemover,
googleDocsUIDRemover,
nestedListedConverter,
msListIgnore,
phrasingContentReducer,
commentRemover,
Expand Down

0 comments on commit cd0bcc2

Please sign in to comment.