Skip to content

Commit

Permalink
Add optional TOC inclusion feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Jule Marcoueille committed Jan 12, 2021
1 parent 182702a commit 7aa6aa9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export interface Options {
/**
* The optional placeholder string to replace with the table of contents.
*
* Defaults to "{{TOC}}";
* Defaults to `undefined`;
*/
placeholder?: string;
placeholder?: string | undefined;

/**
* HTML heading elements to include in the table of contents.
Expand Down Expand Up @@ -113,7 +113,7 @@ export class NormalizedOptions {
public readonly position: InsertPosition;
public readonly headings: HeadingTagName[];
public readonly cssClasses: Required<CssClasses>;
public readonly placeholder?: string;
public readonly placeholder?: string | undefined;
public readonly customizeTOC?: CustomizationHook;
public readonly customizeTOCItem?: CustomizationHook;

Expand All @@ -125,7 +125,7 @@ export class NormalizedOptions {

this.nav = options.nav === undefined ? true : Boolean(options.nav);
this.position = options.position || "afterbegin";
this.placeholder = options.placeholder || "{{TOC}}";
this.placeholder = options.placeholder;
this.headings = options.headings || ["h1", "h2", "h3", "h4", "h5", "h6"];
this.cssClasses = {
toc: cssClasses.toc === undefined ? "toc" : cssClasses.toc,
Expand Down
24 changes: 14 additions & 10 deletions src/rehype-toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,19 @@ export function toc(this: Processor, opts?: Options): Transformer {
throw new Error("`root` is not a Root hast node.");
}

// Find the <main> or <body> element
let [mainNode, mainParent] = findMainNode(root);
let mainNode: Element | undefined;
let mainParent: Parent | undefined;

if (options.placeholder) {
// Find the <target> element if option is given
[mainNode, mainParent] = findPlaceholderNode(root, options.placeholder);

if (!mainNode || !mainParent) { return root; }
}
else {
// Find the <main> or <body> element
[mainNode, mainParent] = findMainNode(root);
}

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

// Find the <target> element is option is given
let target: Element | undefined;
let parent: Parent | undefined;
if (options.placeholder) {
[target, parent] = findPlaceholderNode(root, options.placeholder);
}

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

return root;
Expand Down

0 comments on commit 7aa6aa9

Please sign in to comment.