markdown-it-toc-done-right
markdown-it-toc-done-right copied to clipboard
add the slugified URL to the AST
it would be convenient if the generated AST had a field to hold the slugified links. I know we can augment the AST using the callback function, but having it built-in ensures the same slugify method is used.
Example of current AST:
{
"l": 2,
"n": "My Title",
"c": []
}
Example of proposed AST:
{
"l": 2,
"n": "My Title",
"u": "#my-title",
"c": []
}
I can make a PR if you’re interested!
I believe this is somehow connected to #47. Same idea applies, why not slugify by yourself? Decoupling the AST from the slugifyer seems to be a better architecture.
why not slugify by yourself? Decoupling the AST from the slugifyer seems to be a better architecture.
Yeah, I agree that with the current approach it is not critical and it is rather easy to do it in the callback.
But in fact, I'm even wondering if in most cases the heading id could not be retrieved from the heading token itself (when building the AST) rather than from slugifying its content?
For instance, what if the id was added by something else than markdown-it-anchor
, like manually added by markdown-it-attrs
. The slugify function would not return the right ID in that case…
Is the ID available from the token at the time the AST is built?
Oh, thank you! Just made a test and confirm that you are right. Using markdown-it-attrs
to define custom id's makes the AST not useful:
let md = require('markdown-it')();
let markdownItAttrs = require('markdown-it-attrs');
let markdownItAnchor = require('markdown-it-anchor');
let markdownInToc = require('markdown-it-toc-done-right')
md.use(markdownItAttrs)
.use(markdownItAnchor)
.use(markdownInToc);
let src = [
'[TOC]',
'# header {#custom_id}',
'paragraph {data-toggle=modal}'
].join('\n')
md.render(src);
So yeah, we probably need to put the output id in the AST as you pointed out. using "u" as unique abbreviation is fine for me.
FWIW we're using this to capture the TOC AST for rendering the TOC in a separate page component. An updated AST would be extremely useful.