{"version":3,"file":"utils.cjs","names":["createBlock","escapeAttribute","escapeEditableHTML"],"sources":["../../src/table-of-contents/utils.ts"],"sourcesContent":["import { createBlock } from '@wordpress/blocks';\nimport type { Block } from '@wordpress/blocks';\nimport { escapeAttribute, escapeEditableHTML } from '@wordpress/escape-html';\n\nexport interface HeadingData {\n\t/** The plain text content of the heading. */\n\tcontent: string;\n\t/** The heading level. */\n\tlevel: number;\n\t/** Link to the heading. */\n\tlink: string;\n}\n\nexport interface NestedHeadingData {\n\t/** The heading content, level, and link. */\n\theading: HeadingData;\n\t/** The sub-headings of this heading, if any. */\n\tchildren: NestedHeadingData[] | null;\n}\n\n/**\n * Takes a flat list of heading parameters and nests them based on each header's\n * immediate parent's level.\n *\n * @param headingList The flat list of headings to nest.\n *\n * @return The nested list of headings.\n */\nexport function linearToNestedHeadingList(\n\theadingList: HeadingData[]\n): NestedHeadingData[] {\n\tconst nestedHeadingList: NestedHeadingData[] = [];\n\n\theadingList.forEach( ( heading, key ) => {\n\t\tif ( heading.content === '' ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Make sure we are only working with the same level as the first iteration in our set.\n\t\tif ( heading.level === headingList[ 0 ].level ) {\n\t\t\t// Check that the next iteration will return a value.\n\t\t\t// If it does and the next level is greater than the current level,\n\t\t\t// the next iteration becomes a child of the current iteration.\n\t\t\tif ( headingList[ key + 1 ]?.level > heading.level ) {\n\t\t\t\t// We must calculate the last index before the next iteration that\n\t\t\t\t// has the same level (siblings). We then use this index to slice\n\t\t\t\t// the array for use in recursion. This prevents duplicate nodes.\n\t\t\t\tlet endOfSlice = headingList.length;\n\t\t\t\tfor ( let i = key + 1; i < headingList.length; i++ ) {\n\t\t\t\t\tif ( headingList[ i ].level === heading.level ) {\n\t\t\t\t\t\tendOfSlice = i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// We found a child node: Push a new node onto the return array\n\t\t\t\t// with children.\n\t\t\t\tnestedHeadingList.push( {\n\t\t\t\t\theading,\n\t\t\t\t\tchildren: linearToNestedHeadingList(\n\t\t\t\t\t\theadingList.slice( key + 1, endOfSlice )\n\t\t\t\t\t),\n\t\t\t\t} );\n\t\t\t} else {\n\t\t\t\t// No child node: Push a new node onto the return array.\n\t\t\t\tnestedHeadingList.push( {\n\t\t\t\t\theading,\n\t\t\t\t\tchildren: null,\n\t\t\t\t} );\n\t\t\t}\n\t\t}\n\t} );\n\n\treturn nestedHeadingList;\n}\n\n/**\n * Converts a nested heading list into `core/list-item` blocks, with\n * sub-headings nested in a `core/list` block inside their parent item.\n *\n * @param nestedHeadingList The nested list of headings.\n * @param ordered           Whether nested lists are ordered.\n *\n * @return The list item blocks.\n */\nexport function createListItemBlocks(\n\tnestedHeadingList: NestedHeadingData[],\n\tordered: boolean\n): Block[] {\n\treturn nestedHeadingList.map(\n\t\t( { heading: { content, link }, children } ) =>\n\t\t\tcreateBlock(\n\t\t\t\t'core/list-item',\n\t\t\t\t{\n\t\t\t\t\t// Headings are plain text, and links can carry query args on\n\t\t\t\t\t// paginated posts, so both need escaping to become markup.\n\t\t\t\t\tcontent: link\n\t\t\t\t\t\t? `<a href=\"${ escapeAttribute(\n\t\t\t\t\t\t\t\tlink\n\t\t\t\t\t\t\t) }\">${ escapeEditableHTML( content ) }</a>`\n\t\t\t\t\t\t: escapeEditableHTML( content ),\n\t\t\t\t},\n\t\t\t\tchildren?.length\n\t\t\t\t\t? [\n\t\t\t\t\t\t\tcreateBlock(\n\t\t\t\t\t\t\t\t'core/list',\n\t\t\t\t\t\t\t\t{ ordered },\n\t\t\t\t\t\t\t\tcreateListItemBlocks( children, ordered )\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t]\n\t\t\t\t\t: []\n\t\t\t)\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA4BA,SAAgB,0BACf,aACsB;CACtB,MAAM,oBAAyC,CAAC;CAEhD,YAAY,SAAW,SAAS,QAAS;EACxC,IAAK,QAAQ,YAAY,I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