Skip to content

Commit 6646eed

Browse files
author
Jule Marcoueille
committed
Fix TOC generation
1 parent 04aef46 commit 6646eed

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/rehype-toc.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,19 @@ export function toc(this: Processor, opts?: Options): Transformer {
2323

2424
let mainNode: Element | undefined;
2525
let mainParent: Parent | undefined;
26+
let placeholderNode: Element | undefined;
27+
let placeholderParent: Parent | undefined;
2628

2729
if (options.placeholder) {
2830
// Find the <target> element if option is given
29-
[mainNode, mainParent] = findPlaceholderNode(root, options.placeholder);
31+
[placeholderNode, placeholderParent] = findPlaceholderNode(root, options.placeholder);
3032

31-
if (!mainNode || !mainParent) { return root; }
32-
}
33-
else {
34-
// Find the <main> or <body> element
35-
[mainNode, mainParent] = findMainNode(root);
33+
if (!placeholderNode || !placeholderParent) { return root; }
3634
}
3735

36+
// Find the <main> or <body> element
37+
[mainNode, mainParent] = findMainNode(root);
38+
3839
// Find all heading elements
3940
let headings = findHeadings(mainNode, options);
4041

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

4748
if (node) {
4849
// Add the table of contents to the <main> element
49-
insertTOC(node, mainNode, mainParent, { ...options, replace: !!options.placeholder });
50+
insertTOC(node, placeholderNode || mainNode, placeholderParent || mainParent, { ...options, replace: !!options.placeholder });
5051
}
5152

5253
return root;

0 commit comments

Comments
 (0)