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

Custom anchor id not get extracted into toc in .mdx file #13

Open
jkzing opened this issue Nov 13, 2023 · 1 comment
Open

Custom anchor id not get extracted into toc in .mdx file #13

jkzing opened this issue Nov 13, 2023 · 1 comment

Comments

@jkzing
Copy link
Member

jkzing commented Nov 13, 2023

Reproduction

import { compile } from './index.js';

const { toc } = await compile({
  value: `# Title

## Foo {#my-anchor}

## Bar {Math.PI}
`,
  filepath: 'README.mdx',
  development: true,
  root: '/root',
});
console.log(toc);

Expected toc should be:

[
  { text: 'Foo', id: 'my-anchor', depth: 2 },
  { text: 'Bar ', id: 'bar-', depth: 2 }
]

But got:

[
  { text: 'Foo', id: 'foo-', depth: 2 },
  { text: 'Bar ', id: 'bar-', depth: 2 }
]

More information

Did a little dig into it, and found it caused by the parse options passed to markdown-rs here:

let parse_options = ParseOptions {
constructs: Constructs {
frontmatter: true,
// Enable GFM Grammer
gfm_autolink_literal: true,
gfm_label_start_footnote: true,
gfm_footnote_definition: true,
gfm_strikethrough: true,
gfm_table: true,
gfm_task_list_item: true,
// If is_mdx is true, use mdx constructs, or use markdown constructs
..if is_mdx {
Constructs::mdx()
} else {
Constructs::default()
}
},

Specifically it is the option mdx_expression_text inside Constructs::mdx(). The parser would parse #my-anchor as an javascript expression when this option is turned on.

Maybe we can give mdx_expression_parse a custom function to handle this kind of special cases, but I haven't find a clean fix yet.

@10Derozan
Copy link
Member

Maybe we can escape heading id in processor, like web-infra-dev/rspress#413.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants