fix: unify anchor IDs between VitePress and GitHub with custom slugify

Add GitHub-compatible slugify (github-slugger algorithm) to VitePress
config so heading anchor IDs are consistent across both platforms.
Update all inline anchor links in 22 doc files accordingly.
This commit is contained in:
程序员阿江(Relakkes)
2026-04-05 10:13:57 +08:00
parent 755804fb96
commit 072e5bcc48
23 changed files with 72 additions and 52 deletions
+20
View File
@@ -1,5 +1,19 @@
import { defineConfig } from 'vitepress'
// GitHub-compatible slugify (matches github-slugger algorithm)
// Makes heading anchor IDs consistent between VitePress and GitHub rendering
function slugify(str: string): string {
return str
.replace(/<[^>]*>/g, '')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&amp;/g, '&')
.trim()
.toLowerCase()
.replace(/[^\p{L}\p{M}\p{N}\p{Pc}\- ]/gu, '')
.replace(/ /g, '-')
}
const zhSidebar = [
{
text: '快速开始',
@@ -136,6 +150,12 @@ export default defineConfig({
lastUpdated: true,
base: '/',
markdown: {
anchor: {
slugify,
},
},
head: [
['script', { async: '', src: 'https://www.googletagmanager.com/gtag/js?id=G-D42DM82263' }],
['script', {}, `window.dataLayer = window.dataLayer || [];\nfunction gtag(){dataLayer.push(arguments);}\ngtag('js', new Date());\ngtag('config', 'G-D42DM82263');`],