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

feature request: minDepth option #74

Closed
4 tasks done
dmca-glasgow opened this issue May 28, 2024 · 6 comments · Fixed by #75
Closed
4 tasks done

feature request: minDepth option #74

dmca-glasgow opened this issue May 28, 2024 · 6 comments · Fixed by #75
Labels
💪 phase/solved Post is done

Comments

@dmca-glasgow
Copy link
Contributor

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 a minDepth 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 to toc(mdast).

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels May 28, 2024
@dmca-glasgow dmca-glasgow changed the title minDepth option feature request: minDepth option May 28, 2024
@wooorm
Copy link
Member

wooorm commented May 28, 2024

Can you make this idea practical with a realistic input/actual/expected example?

@dmca-glasgow
Copy link
Contributor Author

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.

@wooorm
Copy link
Member

wooorm commented May 29, 2024

Right, right. Yeah, I think this makes sense. Want to work on a PR?

@wooorm wooorm added the 🙆 yes/confirmed This is confirmed and ready to be worked on label May 29, 2024
@github-actions github-actions bot added 👍 phase/yes Post is accepted and can be worked on and removed 🤞 phase/open Post is being triaged manually labels May 29, 2024

This comment has been minimized.

@dmca-glasgow dmca-glasgow mentioned this issue May 31, 2024
5 tasks
@wooorm wooorm closed this as completed in #75 Jun 2, 2024
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.

@wooorm wooorm added the 💪 phase/solved Post is done label Jun 2, 2024
@github-actions github-actions bot removed 👍 phase/yes Post is accepted and can be worked on 🙆 yes/confirmed This is confirmed and ready to be worked on labels Jun 2, 2024
@wooorm
Copy link
Member

wooorm commented Jun 2, 2024

Done in 7.1.0. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💪 phase/solved Post is done
Development

Successfully merging a pull request may close this issue.

2 participants