Skip to content

Syntax for custom id of headings #2042

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

Closed
4 tasks done
danon opened this issue May 17, 2022 · 4 comments
Closed
4 tasks done

Syntax for custom id of headings #2042

danon opened this issue May 17, 2022 · 4 comments
Labels
👀 no/external This makes more sense somewhere else 👎 phase/no Post cannot or will not be acted on

Comments

@danon
Copy link

danon commented May 17, 2022

Initial checklist

Problem

I like generating dynamic ids for my headings to build tables of content, that's so cool.

I'm missing a valid MDX syntax to include an id for a heading.

Solution

But I miss a feature where I could add a custom id for a heading. Many Markdown parsers support this syntax for custom id:

## Heading hello {#custom-id}

which can be then rendered into

<h2 id="custom-id">Heading hello</h2>

Alternatives

Currently, you could achieve something similar by adding a link to a heading

## [Heading hello](#custom-id)

but that's really an id of a link, not a heading. And it adds a link where it's not actually necessary. It's just a somewhat compliant syntax, to include any id in any form, with the implication of it being parsed with the intention of the link id being turned into the heading id.

Another alternative could be just using HTML all the time, instead of .mdx

<h2 id="custom-id">Heading</h2>

but if I wanted to do that, I would just write React, not .mdx.

@ChristianMurphy
Copy link
Member

This isn't part of CommonMark, GFM, or JSX so I'm not sure it really fits in MDX core.
Support can be added through a plugin, there is a plugin (in need of updating to support the latest verion of remark/mdx) which can support the custom heading syntax you are interested in https://github.com/imcuttle/remark-heading-id

@ChristianMurphy ChristianMurphy added the 👀 no/external This makes more sense somewhere else label May 17, 2022
@danon
Copy link
Author

danon commented May 17, 2022

@ChristianMurphy It doesn't have to be this syntax ## Heading {#id} syntax exactly.

It can be anything that allows to mention the id for a heading. Maybe there already is something like that, but I just don't know it?

@ChristianMurphy
Copy link
Member

Without any plugin, JSX can be used

<h2 id="custom-id">Heading hello</h2>

Or if you prefer the custom {#heading} syntax, it looks like there is a newer plugin with better MDX support https://github.com/Eyas/md-heading-id (source: #1953 (comment))

@ChristianMurphy ChristianMurphy added the 👎 phase/no Post cannot or will not be acted on label May 17, 2022
@wooorm
Copy link
Member

wooorm commented May 17, 2022

Or <a name="custom-id"></a> in front of the heading btw. That also works in normal markdown, and on GitHub

@ChristianMurphy ChristianMurphy closed this as not planned Won't fix, can't repro, duplicate, stale May 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
👀 no/external This makes more sense somewhere else 👎 phase/no Post cannot or will not be acted on
Development

No branches or pull requests

3 participants