Skip to content

Commit

Permalink
Fix TOC generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jule Marcoueille committed Jan 25, 2021
1 parent 04aef46 commit 6646eed
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/rehype-toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <target> 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 <main> or <body> element
[mainNode, mainParent] = findMainNode(root);
if (!placeholderNode || !placeholderParent) { return root; }
}

// Find the <main> or <body> element
[mainNode, mainParent] = findMainNode(root);

// Find all heading elements
let headings = findHeadings(mainNode, options);

Expand All @@ -46,7 +47,7 @@ export function toc(this: Processor, opts?: Options): Transformer {

if (node) {
// Add the table of contents to the <main> element
insertTOC(node, mainNode, mainParent, { ...options, replace: !!options.placeholder });
insertTOC(node, placeholderNode || mainNode, placeholderParent || mainParent, { ...options, replace: !!options.placeholder });
}

return root;
Expand Down

0 comments on commit 6646eed

Please sign in to comment.