-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
feature request: minDepth option #74
Labels
💪 phase/solved
Post is done
Comments
Can you make this idea practical with a realistic input/actual/expected example? |
Sure, given the following example: import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';
import { toc } from 'mdast-util-toc';
const markdown = `
# My awesome course
## Heading 2.1
Paragraph
### Heading 3.1
Paragraph
#### Heading 4.1
Paragraph
### Heading 3.2
Paragraph
## Heading 2.2
Paragraph
### Heading 3.3
Paragraph
## Heading 2.3
Paragraph
### Heading 3.4
Paragraph
#### Heading 4.2
Paragraph
`
const processor = unified().use(remarkParse).use(remarkStringify)
const mdast = processor.parse(markdown);
const tocMdast = toc(mdast as Nodes, { maxDepth: 3 }).map
const tableOfContents = processor.stringify({ type: 'root', children: [tocMdast] })
console.log(tableOfContents) The output is: * [My amazing course](#my-amazing-course)
* [Heading 2.1](#heading-21)
* [Heading 3.1](#heading-31)
* [Heading 3.2](#heading-32)
* [Heading 2.2](#heading-22)
* [Heading 3.3](#heading-33)
* [Heading 2.3](#heading-23)
* [Heading 3.4](#heading-34) What I'm hoping to achieve is: ### Table of contents
* [Heading 2.1](#heading-21)
* [Heading 3.1](#heading-31)
* [Heading 3.2](#heading-32)
* [Heading 2.2](#heading-22)
* [Heading 3.3](#heading-33)
* [Heading 2.3](#heading-23)
* [Heading 3.4](#heading-34) Thanks. |
Right, right. Yeah, I think this makes sense. Want to work on a PR? |
This comment has been minimized.
This comment has been minimized.
wooorm
pushed a commit
that referenced
this issue
Jun 2, 2024
Closes GH-74. Closes GH-75. Reviewed-by: Titus Wormer <[email protected]>
This comment has been minimized.
This comment has been minimized.
Done in |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Initial checklist
Problem
I am trying to create a table of contents for h2s and h3s only. There is a
maxDepth
option which enables me to ignore h4-h6 headings, but not aminDepth
option to ignore h1s.In my document, the single h1 is the main title of the document, with subheadings in the h2-h6 range.
It makes sense to me that the table of contents not contain the h1.
Solution
Add a
minDepth
option.Alternatives
I could remove the h1 from the
mdast
tree before passing it totoc(mdast)
.The text was updated successfully, but these errors were encountered: