{
  "version": 3,
  "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  ) }\">${ 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  ]\n\t\t\t\t\t: []\n\t\t\t)\n\t);\n}\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA4B;AAE5B,yBAAoD;AA0B7C,SAAS,0BACf,aACsB;AACtB,QAAM,oBAAyC,CAAC;AAEhD,cAAY,QAAS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