|
I found that VitePress use its own patch of markdown-it-anchor, and seems bundle it to the dist package. However I found markdown-it-anchor internal For example, if you write this: ## Title 1 {#title-1}
## Title 1 {#title-1}It will raise an error: However, this will not raise any error: ## Title 1
## Title 1Actually, I want to use my other markdown-it plugins to automatically generate title id, but this annoying error troubles me, the id was generated by other plugins, if So I want to fork the markdown-it-anchor and use it for myself. Just change it to: - if (failOnNonUnique && Object.prototype.hasOwnProperty.call(slugs, uniq)) {
+ if (false && failOnNonUnique && Object.prototype.hasOwnProperty.call(slugs, uniq)) {
throw new Error(`User defined \`id\` attribute \`${slug}\` is not unique. Please fix it in your Markdown to continue.`)
} else {But it seems VitePress use it own markdown-it-anchor instead of the installed one. Even with config: import anchorPlugin from "markdown-it-anchor"; // The forked package.
export default defineConfig({
markdown: {
anchor: {
permalink: undefined,
},
config: md => {
md.use(anchorPlugin);
},
},
});It will still raise the error. How to solve it? |
Replies: 1 comment
|
The key is plugin order. VitePress installs its own For this particular change, the least invasive solution is to run your fork in import { defineConfig } from 'vitepress'
import anchorPlugin from 'your-fork-of-markdown-it-anchor'
export default defineConfig({
markdown: {
preConfig(md) {
// If another plugin creates heading ids, install it before this line.
md.use(anchorPlugin, {
permalink: false
})
}
}
})Your fork's core rule then runs first and changes the second explicit If you truly need to replace the built-in rule, this also works in markdown: {
config(md) {
md.core.ruler.disable('anchor')
md.use(anchorPlugin)
}
}However, that removes VitePress's custom anchor behavior (including its accessible Why this works is visible in VitePress's current setup order: |
The key is plugin order. VitePress installs its own
markdown-it-anchorbeforemarkdown.configruns, so adding your fork inconfigis too late: the built-in core rule throws first.For this particular change, the least invasive solution is to run your fork in
preConfig:Your fork's core rule then runs first and changes the second explicit
title-1totitle-1-1. VitePress's own a…