Mdx: Headings should generate ids for deep-linking

Created on 11 Oct 2019  路  6Comments  路  Source: mdx-js/mdx

Subject of the feature

Add ids to headings for deep linking.

Problem

Most markdown implementations automatically add a slugified id to all headings (h1鈥揾6) for deep-linking support. I would expect MDX to do the same.

Expected behavior

Markdown such as ## More information should render as <h2 id="more-information">More information</h2>. This way, links with this hash deep link to the heading (e.g. https://example.com/foo#more-information).

Alternatives

Similar behavior can be accomplished by creating custom components that add the id, but this requires additional configuration.

馃憖 areexternal

Most helpful comment

There's a rehype plugin for that! rehype-slug. Read more about plugins here: https://mdxjs.com/advanced/plugins

All 6 comments

There's a rehype plugin for that! rehype-slug. Read more about plugins here: https://mdxjs.com/advanced/plugins

Yeah, I think we shouldn't do this by default in MDX since it's pretty opinionated, but we should definitely document this in the guides.

Hey

I'm exploring how to bring i18n support to https://github.com/facebook/docusaurus where MDX files will be translated (through git or a saas like Crowdin/Transifex).

Auto-generated ids might be a problem in practice, because changing the text does change the id, which may break linking from other pages. This means that a translating changing some heading would have to go through all the other docs of the site and update all the anchor links everywhere.

Having a way to choose the id would be useful.

Related discussions about ReactJS translation system and anchor links:
https://github.com/reactjs/reactjs.org/issues/1605#issuecomment-458816106
https://github.com/reactjs/reactjs.org/issues/1605#issuecomment-458819231
https://github.com/ethereum/ethereum-org-website/issues/272
https://github.com/reactjs/reactjs.org/pull/1636/files

Looks like it's possible to do as a remark plugin
https://github.com/reactjs/reactjs.org/pull/1636/files#diff-9a50f5acbfae877b9f3d761e266b0339R21

Yet just wanted to expose the i18n usecase so that you might want to include core support?
(btw I have similar usecases for tunability to provide targets to links, size to images etc... all can probably be solved with remark)

@keithjgrant This can be accomplished by using remark/plugins.

I accomplished it, with the following webpack setup:

const remarkSlugs = require('remark-slug');
const rehypeHtml = require('rehype-stringify');

...
  rules: [
    ...
      {
        test: /\.mdx?$/,
        use: [
          {
            loader: '@mdx-js/loader',
            options: {
              remarkPlugins: [remarkSlugs],
              rehypePlugins: [rehypeHtml],
            },
          },
        ]
      }
  ]

@keithjgrant This can be accomplished by using remark/plugins.

I accomplished it, with the following webpack setup:

const remarkSlugs = require('remark-slug');
const rehypeHtml = require('rehype-stringify');

...
  rules: [
    ...
      {
        test: /\.mdx?$/,
        use: [
          {
            loader: '@mdx-js/loader',
            options: {
              remarkPlugins: [remarkSlugs],
              rehypePlugins: [rehypeHtml],
            },
          },
        ]
      }
  ]

Do you have an example repo that demonstrate the use of the deep-linking? A codesandbox example? I am interesting in knowing how to do this. Thank you

Hi all! I鈥檓 going to close this as it can be accomplished with a plugin.

The internationalization context @slorber brought up is interesting, but IMO could also be in a plugin.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

johno picture johno  路  3Comments

lex111 picture lex111  路  4Comments

EddyVinck picture EddyVinck  路  3Comments

johno picture johno  路  4Comments

ludwigfrank picture ludwigfrank  路  3Comments