Skip to content

Commit 7aa6aa9

Browse files
author
Jule Marcoueille
committed
Add optional TOC inclusion feature
1 parent 182702a commit 7aa6aa9

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/options.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ export interface Options {
3232
/**
3333
* The optional placeholder string to replace with the table of contents.
3434
*
35-
* Defaults to "{{TOC}}";
35+
* Defaults to `undefined`;
3636
*/
37-
placeholder?: string;
37+
placeholder?: string | undefined;
3838

3939
/**
4040
* HTML heading elements to include in the table of contents.
@@ -113,7 +113,7 @@ export class NormalizedOptions {
113113
public readonly position: InsertPosition;
114114
public readonly headings: HeadingTagName[];
115115
public readonly cssClasses: Required<CssClasses>;
116-
public readonly placeholder?: string;
116+
public readonly placeholder?: string | undefined;
117117
public readonly customizeTOC?: CustomizationHook;
118118
public readonly customizeTOCItem?: CustomizationHook;
119119

@@ -125,7 +125,7 @@ export class NormalizedOptions {
125125

126126
this.nav = options.nav === undefined ? true : Boolean(options.nav);
127127
this.position = options.position || "afterbegin";
128-
this.placeholder = options.placeholder || "{{TOC}}";
128+
this.placeholder = options.placeholder;
129129
this.headings = options.headings || ["h1", "h2", "h3", "h4", "h5", "h6"];
130130
this.cssClasses = {
131131
toc: cssClasses.toc === undefined ? "toc" : cssClasses.toc,

src/rehype-toc.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,19 @@ export function toc(this: Processor, opts?: Options): Transformer {
2121
throw new Error("`root` is not a Root hast node.");
2222
}
2323

24-
// Find the <main> or <body> element
25-
let [mainNode, mainParent] = findMainNode(root);
24+
let mainNode: Element | undefined;
25+
let mainParent: Parent | undefined;
26+
27+
if (options.placeholder) {
28+
// Find the <target> element if option is given
29+
[mainNode, mainParent] = findPlaceholderNode(root, options.placeholder);
30+
31+
if (!mainNode || !mainParent) { return root; }
32+
}
33+
else {
34+
// Find the <main> or <body> element
35+
[mainNode, mainParent] = findMainNode(root);
36+
}
2637

2738
// Find all heading elements
2839
let headings = findHeadings(mainNode, options);
@@ -33,16 +44,9 @@ export function toc(this: Processor, opts?: Options): Transformer {
3344
// Allow the user to customize the table of contents before we add it to the page
3445
let node = customizationHooks(tocNode, options);
3546

36-
// Find the <target> element is option is given
37-
let target: Element | undefined;
38-
let parent: Parent | undefined;
39-
if (options.placeholder) {
40-
[target, parent] = findPlaceholderNode(root, options.placeholder);
41-
}
42-
4347
if (node) {
4448
// Add the table of contents to the <main> element
45-
insertTOC(node, target || mainNode, parent || mainParent, { ...options, replace: !!target && !!parent });
49+
insertTOC(node, mainNode, mainParent, { ...options, replace: !!options.placeholder });
4650
}
4751

4852
return root;

0 commit comments

Comments
 (0)