UNPKG

@wordpress/blocks

Version:
8 lines (7 loc) 5.06 kB
{ "version": 3, "sources": ["../../../src/api/raw-handling/special-comment-converter.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { remove, replace } from '@wordpress/dom';\n\n/**\n * Looks for `<!--nextpage-->` and `<!--more-->` comments and\n * replaces them with a custom element representing a future block.\n *\n * The custom element is a way to bypass the rest of the `raw-handling`\n * transforms, which would eliminate other kinds of node with which to carry\n * `<!--more-->`'s data: nodes with `data` attributes, empty paragraphs, etc.\n *\n * The custom element is then expected to be recognized by any registered\n * block's `raw` transform.\n *\n * @param {Node} node The node to be processed.\n * @param {Document} doc The document of the node.\n * @return {void}\n */\nexport default function specialCommentConverter( node, doc ) {\n\tif ( node.nodeType !== node.COMMENT_NODE ) {\n\t\treturn;\n\t}\n\n\tif (\n\t\tnode.nodeValue !== 'nextpage' &&\n\t\tnode.nodeValue.indexOf( 'more' ) !== 0\n\t) {\n\t\treturn;\n\t}\n\n\tconst block = createBlock( node, doc );\n\n\t// If our `<!--more-->` comment is in the middle of a paragraph, we should\n\t// split the paragraph in two and insert the more block in between. If it's\n\t// inside an empty paragraph, we should still move it out of the paragraph\n\t// and remove the paragraph. If there's no paragraph, fall back to simply\n\t// replacing the comment.\n\tif ( ! node.parentNode || node.parentNode.nodeName !== 'P' ) {\n\t\treplace( node, block );\n\t} else {\n\t\tconst childNodes = Array.from( node.parentNode.childNodes );\n\t\tconst nodeIndex = childNodes.indexOf( node );\n\t\tconst wrapperNode = node.parentNode.parentNode || doc.body;\n\n\t\tconst paragraphBuilder = ( acc, child ) => {\n\t\t\tif ( ! acc ) {\n\t\t\t\tacc = doc.createElement( 'p' );\n\t\t\t}\n\n\t\t\tacc.appendChild( child );\n\n\t\t\treturn acc;\n\t\t};\n\n\t\t// Split the original parent node and insert our more block\n\t\t[\n\t\t\tchildNodes.slice( 0, nodeIndex ).reduce( paragraphBuilder, null ),\n\t\t\tblock,\n\t\t\tchildNodes.slice( nodeIndex + 1 ).reduce( paragraphBuilder, null ),\n\t\t].forEach(\n\t\t\t( element ) =>\n\t\t\t\telement && wrapperNode.insertBefore( element, node.parentNode )\n\t\t);\n\n\t\t// Remove the old parent paragraph\n\t\tremove( node.parentNode );\n\t}\n}\n\nfunction createBlock( commentNode, doc ) {\n\tif ( commentNode.nodeValue === 'nextpage' ) {\n\t\treturn createNextpage( doc );\n\t}\n\n\t// Grab any custom text in the comment.\n\tconst customText = commentNode.nodeValue.slice( 4 ).trim();\n\n\t/*\n\t * When a `<!--more-->` comment is found, we need to look for any\n\t * `<!--noteaser-->` sibling, but it may not be a direct sibling\n\t * (whitespace typically lies in between)\n\t */\n\tlet sibling = commentNode;\n\tlet noTeaser = false;\n\twhile ( ( sibling = sibling.nextSibling ) ) {\n\t\tif (\n\t\t\tsibling.nodeType === sibling.COMMENT_NODE &&\n\t\t\tsibling.nodeValue === 'noteaser'\n\t\t) {\n\t\t\tnoTeaser = true;\n\t\t\tremove( sibling );\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn createMore( customText, noTeaser, doc );\n}\n\nfunction createMore( customText, noTeaser, doc ) {\n\tconst node = doc.createElement( 'wp-block' );\n\tnode.dataset.block = 'core/more';\n\tif ( customText ) {\n\t\tnode.dataset.customText = customText;\n\t}\n\tif ( noTeaser ) {\n\t\t// \"Boolean\" data attribute.\n\t\tnode.dataset.noTeaser = '';\n\t}\n\treturn node;\n}\n\nfunction createNextpage( doc ) {\n\tconst node = doc.createElement( 'wp-block' );\n\tnode.dataset.block = 'core/nextpage';\n\n\treturn node;\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAAgC;AAiBjB,SAAR,wBAA0C,MAAM,KAAM;AAC5D,MAAK,KAAK,aAAa,KAAK,cAAe;AAC1C;AAAA,EACD;AAEA,MACC,KAAK,cAAc,cACnB,KAAK,UAAU,QAAS,MAAO,MAAM,GACpC;AACD;AAAA,EACD;AAEA,QAAM,QAAQ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