Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New placeholder feature & hast refactoring #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add optional TOC inclusion feature
Jule Marcoueille committed Jan 12, 2021
commit a7d5114223a3af72b6892018853268393e61218a
8 changes: 4 additions & 4 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -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.
@@ -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;

@@ -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,
24 changes: 14 additions & 10 deletions src/rehype-toc.ts
Original file line number Diff line number Diff line change
@@ -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);
@@ -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;