feat(docs): 添加 Markdown 转义插件解决 HTML 标签显示问题

- 在 VitePress 配置中集成 markdown-escape-plugin
- 实现代码块保护机制避免代码中的 < > 符号被转义
- 添加预处理器确保代码块内容不被 HTML 转义影响
- 通过占位符机制实现代码块的临时替换和恢复
- 提供完整的代码块解析和转义处理流程
This commit is contained in:
GeWuYou 2026-02-11 14:44:15 +08:00
parent 8c04a5008a
commit 75826961d4

View File

@ -1,12 +1,15 @@
import { defineConfig } from 'vitepress'
export default defineConfig({
title: 'GFramework',
description: '面向游戏开发场景的模块化 C# 框架',
/** GitHub Pages / 子路径部署 */
base: '/GFramework/',
vite: {
plugins: [markdownEscapePlugin()]
},
/** 多语言 */
locales: {
root: {
@ -167,3 +170,35 @@ export default defineConfig({
}
}
})
import { defineConfig } from 'vitepress'
function markdownEscapePlugin() {
return {
name: 'markdown-escape-plugin',
enforce: 'pre', // 在 vitepress 之前执行
transform(code: string, id: string) {
if (!id.endsWith('.md')) return
const codeBlocks: string[] = []
// 1⃣ 替换代码块
const replaced = code.replace(/```[\s\S]*?```/g, (match) => {
const index = codeBlocks.length
codeBlocks.push(match)
return `__CODE_BLOCK_${index}__`
})
// 2⃣ 转义普通文本中的 < >
let escaped = replaced
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
// 3⃣ 恢复代码块
codeBlocks.forEach((block, index) => {
escaped = escaped.replace(`__CODE_BLOCK_${index}__`, block)
})
return escaped
}
}
}