mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-25 21:34:28 +08:00
feat(docs): 添加 Markdown 转义插件解决 HTML 标签显示问题
- 在 VitePress 配置中集成 markdown-escape-plugin - 实现代码块保护机制避免代码中的 < > 符号被转义 - 添加预处理器确保代码块内容不被 HTML 转义影响 - 通过占位符机制实现代码块的临时替换和恢复 - 提供完整的代码块解析和转义处理流程
This commit is contained in:
parent
8c04a5008a
commit
75826961d4
@ -1,12 +1,15 @@
|
|||||||
import { defineConfig } from 'vitepress'
|
import { defineConfig } from 'vitepress'
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
|
|
||||||
title: 'GFramework',
|
title: 'GFramework',
|
||||||
description: '面向游戏开发场景的模块化 C# 框架',
|
description: '面向游戏开发场景的模块化 C# 框架',
|
||||||
|
|
||||||
/** GitHub Pages / 子路径部署 */
|
/** GitHub Pages / 子路径部署 */
|
||||||
base: '/GFramework/',
|
base: '/GFramework/',
|
||||||
|
vite: {
|
||||||
|
plugins: [markdownEscapePlugin()]
|
||||||
|
},
|
||||||
/** 多语言 */
|
/** 多语言 */
|
||||||
locales: {
|
locales: {
|
||||||
root: {
|
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, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
|
||||||
|
// 3️⃣ 恢复代码块
|
||||||
|
codeBlocks.forEach((block, index) => {
|
||||||
|
escaped = escaped.replace(`__CODE_BLOCK_${index}__`, block)
|
||||||
|
})
|
||||||
|
|
||||||
|
return escaped
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user