test(markdown): cover attrs/anchor plugin order independence

attrs registers its core rule at a fixed position (before linkify)
while anchor pushes to the end of the chain, so user-defined ids from
curly attributes win regardless of registration order. Remove the
comment that claimed the order matters.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pull/5335/head
Divyansh Singh 2 months ago
parent 7550517916
commit 85ede55fdb

@ -1,3 +1,6 @@
import { anchor as anchorPlugin } from '@mdit/plugin-anchor'
import attrsPlugin from 'markdown-it-attrs'
import { MarkdownItAsync } from 'markdown-it-async'
import {
createMarkdownRenderer,
disposeMdItInstance,
@ -110,4 +113,20 @@ describe('node/markdown/markdown', () => {
)
})
})
// attrs applies at a fixed position in the core chain (before linkify),
// while anchor pushes to its end, so anchor always sees user-defined ids
// no matter which plugin is registered first
test('anchor respects ids from attrs regardless of plugin order', async () => {
for (const plugins of [
[attrsPlugin, anchorPlugin],
[anchorPlugin, attrsPlugin]
] as const) {
const md = new MarkdownItAsync()
for (const plugin of plugins) md.use(plugin)
expect(await md.renderAsync('## Title {#custom-id}')).toContain(
'id="custom-id"'
)
}
})
})

@ -361,8 +361,6 @@ export async function createMarkdownRenderer(
mditCjkFriendly(md)
}
if (options.anchor !== false) {
// must be applied after attrs so that user-defined ids from curly
// attributes take precedence over slugified ones
anchorPlugin(md, {
slugify,
getTokensText: (tokens) => {

Loading…
Cancel
Save