|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +import { select } from '@wordpress/data'; |
| 5 | +import { create } from '@wordpress/rich-text'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Takes a flat list of heading parameters and nests them based on each header's |
| 9 | + * immediate parent's level. |
| 10 | + * |
| 11 | + * @param {Array} headingsList The flat list of headings to nest. |
| 12 | + * @param {number} index The current list index. |
| 13 | + * @return {Array} The nested list of headings. |
| 14 | + */ |
| 15 | +export function linearToNestedHeadingList( headingsList, index = 0 ) { |
| 16 | + const nestedHeadingsList = []; |
| 17 | + |
| 18 | + headingsList.forEach( function( heading, key ) { |
| 19 | + if ( heading.content === undefined ) { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + // Make sure we are only working with the same level as the first iteration in our set. |
| 24 | + if ( heading.level === headingsList[ 0 ].level ) { |
| 25 | + // Check that the next iteration will return a value. |
| 26 | + // If it does and the next level is greater than the current level, |
| 27 | + // the next iteration becomes a child of the current interation. |
| 28 | + if ( |
| 29 | + headingsList[ key + 1 ] !== undefined && |
| 30 | + headingsList[ key + 1 ].level > heading.level |
| 31 | + ) { |
| 32 | + // We need to calculate the last index before the next iteration that has the same level (siblings). |
| 33 | + // We then use this last index to slice the array for use in recursion. |
| 34 | + // This prevents duplicate nodes. |
| 35 | + let endOfSlice = headingsList.length; |
| 36 | + for ( let i = key + 1; i < headingsList.length; i++ ) { |
| 37 | + if ( headingsList[ i ].level === heading.level ) { |
| 38 | + endOfSlice = i; |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // We found a child node: Push a new node onto the return array with children. |
| 44 | + nestedHeadingsList.push( { |
| 45 | + block: heading, |
| 46 | + index: index + key, |
| 47 | + children: linearToNestedHeadingList( |
| 48 | + headingsList.slice( key + 1, endOfSlice ), |
| 49 | + index + key + 1 |
| 50 | + ), |
| 51 | + } ); |
| 52 | + } else { |
| 53 | + // No child node: Push a new node onto the return array. |
| 54 | + nestedHeadingsList.push( { |
| 55 | + block: heading, |
| 56 | + index: index + key, |
| 57 | + children: null, |
| 58 | + } ); |
| 59 | + } |
| 60 | + } |
| 61 | + } ); |
| 62 | + |
| 63 | + return nestedHeadingsList; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Gets a list of heading texts, anchors and levels in the current document. |
| 68 | + * |
| 69 | + * @return {Array} The list of headings. |
| 70 | + */ |
| 71 | +export function getHeadingsList() { |
| 72 | + return convertBlocksToTableOfContents( getHeadingBlocks() ); |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Gets a list of heading blocks in the current document. |
| 77 | + * |
| 78 | + * @return {Array} The list of heading blocks. |
| 79 | + */ |
| 80 | +export function getHeadingBlocks() { |
| 81 | + const editor = select( 'core/block-editor' ); |
| 82 | + return editor |
| 83 | + .getBlocks() |
| 84 | + .filter( ( block ) => block.name === 'core/heading' ); |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Extracts text, anchor and level from a list of heading blocks. |
| 89 | + * |
| 90 | + * @param {Array} headingBlocks The list of heading blocks. |
| 91 | + * @return {Array} The list of heading parameters. |
| 92 | + */ |
| 93 | +export function convertBlocksToTableOfContents( headingBlocks ) { |
| 94 | + return headingBlocks.map( function( heading ) { |
| 95 | + // This is a string so that it can be stored/sourced as an attribute in the table of contents |
| 96 | + // block using a data attribute. |
| 97 | + const level = heading.attributes.level.toString(); |
| 98 | + |
| 99 | + const headingContent = heading.attributes.content; |
| 100 | + const anchorContent = heading.attributes.anchor; |
| 101 | + |
| 102 | + // Strip html from heading to use as the table of contents entry. |
| 103 | + const content = headingContent |
| 104 | + ? create( { html: headingContent } ).text |
| 105 | + : ''; |
| 106 | + |
| 107 | + const anchor = anchorContent ? '#' + anchorContent : ''; |
| 108 | + |
| 109 | + return { content, anchor, level }; |
| 110 | + } ); |
| 111 | +} |
0 commit comments