question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[v2] Heading custom ID extended syntax support

See original GitHub issue

Hey there 👋 . First off, thanks so much for all the hard work on v2! I’m really excited to start using it.

I have an interesting situation where we need to be able to define custom IDs for headings generated from markdown. I am part of a team in the process of moving our old docs site written in drupal over to Gatsby/MDX. We have lots and lots of pages within our docs site (5000+) and many of them link to each other. Because we want to maintain backwards compatibility with the existing site, we want to maintain the existing id attributes on headings so that our links don’t break.

Problem

In v1, it appears that the extended syntax for custom heading IDs worked correctly when using the gatsby-remark-autolink-headers. I suspect this was a result of how the AST was parsed in v1.

Take the following example:

# A heading {#custom-id}

This would result in the AST looking something like the following:

{
  type: 'heading',
  depth: 2,
  children: [
    {
      type: 'text',
      value: 'A heading {#custom-id}',
    }
  ],
}

Note here how the custom ID was part of the value of the text child. It appears the gatsby-remark-autolink-headers plugin expects this as part of the AST so that it can update the h tag with the custom ID.

With the much more robust parsing in v2 (which is AWESOME), the same node is parsed into the following:

{
  type: 'heading',
  depth: 3,
  children: [
    {
      type: 'text',
      value:
        'A heading',
    },
    {
      type: 'mdxSpanExpression',
      value: '#custom-id',
    },
  ],
}

Because of this, unfortunately it causes issues when compiling to into JSX because it results in an expression like the following:

<h1>{`A heading`}
{#test}</h1>

Alternatives

I’m not really sure what I expect the behavior to be here. I can understand why the parser would parse this heading expression as it does. Writing the heading as a regular JSX header works as intended:

// Works
<h1 id="custom-id">A heading</h1> 

However this is a bit of a bummer as we lose some readability because of this.

Would it be possible to support the custom ID syntax on headings to support this use case?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
wooormcommented, Sep 29, 2020

I don’t think it should be changed here, as it’s by design: {...} is an expression here. For example, {1 + 1} could output 2.

0reactions
wooormcommented, Nov 13, 2020

I’m going to close this as it is an intended change: to move closer to JSX.

I don’t see why that gatsby plugin does work that isn‘t in its readme, anyway: MDX has remark and rehype plugins, where you can do custom IDs in code, and if you want them in content, well, there are other remark plugins that allow that. Or you can use JSX in MDX?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Extended Syntax - Markdown Guide
You can link to headings with custom IDs in the file by creating a standard link with a number sign ( # )...
Read more >
Handbook Markdown Guide - GitLab
Heading. List: List item with custom class; List item with custom id. To a link, in-line. This is ...
Read more >
Markdown Syntax | Hub - JetBrains
To apply a heading style, start the line with one to six number signs # . Follow the number signs with a space...
Read more >
Markdown - Dendron
Syntax Highlighting. You can add an optional language identifier to enable syntax highlighting in your fenced code block. For example, to syntax ......
Read more >
Syntax Extensions - MyST-Parser
This extension is currently only supported for HTML output, ... myst-anchors -l 2 docs/syntax/optional.md <h1 id="optional-myst-syntaxes"></h1> <h2 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found