Skip to content

feature request: minDepth option #74

Closed
@dmca-glasgow

Description

@dmca-glasgow
Contributor

Initial checklist

  • I read the support docs
    I read the contributing guide
    I agree to follow the code of conduct
    I searched issues and couldn’t find anything (or linked relevant results below)

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).

Activity

added
👋 phase/newPost is being triaged automatically
🤞 phase/openPost is being triaged manually
and removed
👋 phase/newPost is being triaged automatically
on May 28, 2024
changed the title [-]minDepth option[/-] [+]feature request: minDepth option[/+] on May 28, 2024
wooorm

wooorm commented on May 28, 2024

@wooorm
Member

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

dmca-glasgow

dmca-glasgow commented on May 29, 2024

@dmca-glasgow
ContributorAuthor

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

wooorm commented on May 29, 2024

@wooorm
Member

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

added
👍 phase/yesPost is accepted and can be worked on
and removed
🤞 phase/openPost is being triaged manually
on May 29, 2024
github-actions

github-actions commented on May 29, 2024

@github-actions
added a commit that references this issue on Jun 2, 2024
623c332
github-actions

github-actions commented on Jun 2, 2024

@github-actions
wooorm

wooorm commented on Jun 2, 2024

@wooorm
Member

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @wooorm@dmca-glasgow

      Issue actions

        feature request: minDepth option · Issue #74 · syntax-tree/mdast-util-toc