valeriangalliat / markdown-it-anchor

A markdown-it plugin that adds an `id` attribute to headings and optionally permalinks.
The Unlicense
291 stars 72 forks source link

KaTeX Expressions aren't Included in Headings #112

Closed manuth closed 2 years ago

manuth commented 2 years ago

Currently, KaTeX expressions inside of headings aren't part of the anchors. I'd love to either have an option for configuring additional token-types to include in anchors or to have the token type math_inline added to this filter: https://github.com/valeriangalliat/markdown-it-anchor/blob/7f947ce9a0137ab68dbfef79a4c4d0a2ef4bfd37/index.js#L51

This would allow users to make anchors look more like the ones generated by GitHub.

Thanks for your great work 😄

valeriangalliat commented 2 years ago

Interesting! I'm thinking I could use a blacklist approach instead of the current whitelist approach, e.g. doing .filter(token => token.type !== 'html_inline' && token.type !== 'image') instead of .filter(token => token.type === 'text' || token.type === 'code_inline').

This yields the same output with all my tests using native markdown-it tokens but in the case of the KaTeX plugin, that would include the expression as well. This would be a breaking change though and that behavior might be undesirable with other plugins, so I guess it's safer to leave it as is and introduce a configuration option...

Probably something like:

md.use(anchor, {
  getTokensText: tokens => tokens.filter(t => t.type !== 'html_inline' && t.type !== 'image').map(t => t.content).join('')
})

I'll give it some more thoughts and get back to you :)

manuth commented 2 years ago

Awesome, that would be great! Thanks a ton for taking care 😄

valeriangalliat commented 2 years ago

getTokensText is released as part of v8.6.0!

manuth commented 2 years ago

Awesome! Thank you very much for your effort! 😄