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
Fix TOC generation
  • Loading branch information
Jule Marcoueille committed Jan 25, 2021
commit 6646eedbdff7b643695408adc943037ca0569a91
15 changes: 8 additions & 7 deletions src/rehype-toc.ts
Original file line number Diff line number Diff line change
@@ -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);

@@ -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;