diff --git a/src/rehype-toc.ts b/src/rehype-toc.ts index ffa5634..b86eef3 100644 --- a/src/rehype-toc.ts +++ b/src/rehype-toc.ts @@ -23,18 +23,19 @@ export function toc(this: Processor, opts?: Options): Transformer { let mainNode: Element | undefined; let mainParent: Parent | undefined; + let placeholderNode: Element | undefined; + let placeholderParent: Parent | undefined; if (options.placeholder) { // Find the element if option is given - [mainNode, mainParent] = findPlaceholderNode(root, options.placeholder); + [placeholderNode, placeholderParent] = findPlaceholderNode(root, options.placeholder); - if (!mainNode || !mainParent) { return root; } - } - else { - // Find the
or element - [mainNode, mainParent] = findMainNode(root); + if (!placeholderNode || !placeholderParent) { return root; } } + // Find the
or element + [mainNode, mainParent] = findMainNode(root); + // Find all heading elements let headings = findHeadings(mainNode, options); @@ -46,7 +47,7 @@ export function toc(this: Processor, opts?: Options): Transformer { if (node) { // Add the table of contents to the
element - insertTOC(node, mainNode, mainParent, { ...options, replace: !!options.placeholder }); + insertTOC(node, placeholderNode || mainNode, placeholderParent || mainParent, { ...options, replace: !!options.placeholder }); } return root;