This commit is contained in:
yin 2026-11-21 16:14:42 +08:00
parent cfe318d817
commit d28e720654
6 changed files with 99 additions and 58 deletions

View File

@ -3,7 +3,7 @@
/**
* 极简 Markdown → HTML(文档站专用)
*
* 只支持写文档够用的子集:标题 / 段落 / 列表 / 引用 / 代码块 / 行内代码 / 粗体 / 斜体 / 删除线 / 链接 / 分割线。
* 只支持写文档够用的子集:标题 / 段落 / 列表 / 引用 / 代码块 / 行内代码 / 粗体 / 斜体 / 删除线 / 链接 / 图片 / 分割线。
* **先转义再拼 HTML**:文档站是公开页面,后台输入的内容一律当纯文本处理,避免把 `<script>` 原样吐出去。
*
* `render()` 同时返回文章内目录(`toc`),供页面右侧的「本页目录」使用。
@ -34,7 +34,7 @@ function safeHref(href) {
return raw;
}
/** 行内样式:先处理代码片段与链接,再处理强调(避免把链接文字里的 * 吃掉) */
/** 行内样式:先处理代码片段、图片与链接,再处理强调(避免把链接文字里的 * 吃掉) */
function inline(text) {
const codeSpans = [];
let out = escapeHtml(text).replace(/`([^`]+)`/g, (match, code) => {
@ -42,8 +42,16 @@ function inline(text) {
return `\u0000${codeSpans.length - 1}\u0000`;
});
// 图片:必须写在链接之前,否则 `![alt](src)` 会先被链接规则吃成 `!<a>…</a>`
// 此时整段已被 escapeHtml 过,URL 里的 & 是 &amp;,还原后再统一按属性转义,避免 &amp;amp;
out = out.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g, (match, alt, src) => {
const target = safeHref(src.replace(/&amp;/g, '&'));
if (!target) return alt;
return `<img src="${escapeHtml(target)}" alt="${alt}" loading="lazy">`;
});
out = out.replace(/\[([^\]]*)\]\(([^)\s]+)\)/g, (match, label, href) => {
const target = safeHref(href);
const target = safeHref(href.replace(/&amp;/g, '&'));
if (!target) return label;
const external = /^https?:\/\//i.test(target);
return `<a href="${escapeHtml(target)}"${external ? ' target="_blank" rel="noopener"' : ''}>${label}</a>`;
@ -165,6 +173,7 @@ function render(markdown) {
function plainText(markdown, maxLength = 120) {
const text = String(markdown || '')
.replace(/```[\s\S]*?```/g, ' ')
.replace(/!\[[^\]]*\]\([^)]*\)/g, ' ')
.replace(/[#>*`~\-]/g, ' ')
.replace(/\s+/g, ' ')
.trim();

View File

@ -528,6 +528,16 @@ a:hover { text-decoration: underline; }
}
.doc-body hr { border: 0; border-top: 1px solid var(--border); margin: 28px 0; }
.doc-body a { word-break: break-word; }
.doc-body img {
display: block;
max-width: 100%;
height: auto;
margin: 18px 0;
border: 1px solid var(--border);
border-radius: 10px;
background: #fff;
}
.doc-body p > img:only-child { margin: 8px 0 18px; }
.doc-toc {
position: fixed;

View File

@ -1285,6 +1285,7 @@
"resolved": "https://registry.npmjs.org/@types/node/-/node-26.6.5.tgz",
"integrity": "sha512-uMzsv9stDdofIZBx4UGh4Z9ZgXnGC9seRDscilBcTHAeOFk67MZg/k9bWVfO+bk4qZmG+l52hRnLwUiH2LnJTg==",
"license": "MIT",
"peer": true,
"dependencies": {
"undici-types": "~8.9.0"
}
@ -1391,7 +1392,6 @@
"os": [
"aix"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1409,7 +1409,6 @@
"os": [
"darwin"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1427,7 +1426,6 @@
"os": [
"darwin"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1445,7 +1443,6 @@
"os": [
"freebsd"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1463,7 +1460,6 @@
"os": [
"freebsd"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1481,7 +1477,6 @@
"os": [
"linux"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1499,7 +1494,6 @@
"os": [
"linux"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1517,7 +1511,6 @@
"os": [
"linux"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1535,7 +1528,6 @@
"os": [
"linux"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1553,7 +1545,6 @@
"os": [
"linux"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1571,7 +1562,6 @@
"os": [
"linux"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1589,7 +1579,6 @@
"os": [
"linux"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1607,7 +1596,6 @@
"os": [
"linux"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1625,7 +1613,6 @@
"os": [
"netbsd"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1643,7 +1630,6 @@
"os": [
"netbsd"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1661,7 +1647,6 @@
"os": [
"openbsd"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1679,7 +1664,6 @@
"os": [
"openbsd"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1697,7 +1681,6 @@
"os": [
"sunos"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1715,7 +1698,6 @@
"os": [
"win32"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -1733,7 +1715,6 @@
"os": [
"win32"
],
"peer": true,
"engines": {
"node": ">=16.20.0"
}
@ -3654,6 +3635,7 @@
"resolved": "https://registry.npmjs.org/egg/-/egg-3.34.0.tgz",
"integrity": "sha512-Zs4gwrp/iY7Vkdfm4/UTy1cZrEcWuK9ZiBix30Sco5ennR6k7TcsssYfIS40lgVvI7U4HwcR9HOmhYML3A0A3w==",
"license": "MIT",
"peer": true,
"dependencies": {
"@types/accepts": "^1.3.5",
"@types/koa": "^2.13.5",
@ -4068,6 +4050,7 @@
"resolved": "https://registry.npmjs.org/egg-mock/-/egg-mock-5.15.2.tgz",
"integrity": "sha512-8g+GzGcjfPaM/XwGaoej27wfIw7qeKR3tqYZPB3mIRWMzChYnyI2Bb+5qud23Ealj0raNamE+85ZsBKI2kq8nw==",
"dev": true,
"peer": true,
"dependencies": {
"@eggjs/utils": "^4.0.2",
"@types/supertest": "^2.0.7",
@ -8721,7 +8704,8 @@
"resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.14.tgz",
"integrity": "sha512-ZhYeb6nRaXCfhnndflDK8qI6ZQ/YcWZCISRAWICW9XYqMUwjZM9Z0DveWX/ABN01oxSHwVxKQmxeYZSsm0jh5A==",
"dev": true,
"license": "Apache-2.0"
"license": "Apache-2.0",
"peer": true
},
"node_modules/require-directory": {
"version": "2.1.1",

View File

@ -127,6 +127,7 @@
"cpu": [
"arm"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -143,6 +144,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -159,6 +161,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -175,6 +178,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -191,6 +195,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -207,6 +212,7 @@
"cpu": [
"arm"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -223,6 +229,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -239,6 +246,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -255,6 +263,7 @@
"cpu": [
"ppc64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -271,6 +280,7 @@
"cpu": [
"s390x"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -287,6 +297,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -303,6 +314,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -319,6 +331,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -335,6 +348,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -351,6 +365,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -377,8 +392,9 @@
"version": "26.6.5",
"resolved": "https://registry.npmjs.org/@types/node/-/node-26.6.5.tgz",
"integrity": "sha512-uMzsv9stDdofIZBx4UGh4Z9ZgXnGC9seRDscilBcTHAeOFk67MZg/k9bWVfO+bk4qZmG+l52hRnLwUiH2LnJTg==",
"devOptional": true,
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"undici-types": "~8.9.0"
}
@ -812,6 +828,7 @@
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
"integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
"dev": true,
"hasInstallScript": true,
"license": "MIT",
"optional": true,
@ -865,6 +882,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -885,6 +903,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -905,6 +924,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -925,6 +945,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -945,6 +966,7 @@
"cpu": [
"arm"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -965,6 +987,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -985,6 +1008,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -1005,6 +1029,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -1025,6 +1050,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -1045,6 +1071,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -1065,6 +1092,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -1289,6 +1317,7 @@
"integrity": "sha512-eTBTTqMR6uHKLgcUqpJP8ntzDI6V4LYA8negAMBEI7gkgVvOdrx6VsvS2Ym+83KhOPdLCHWJu0w7UQFJZhHKNw==",
"devOptional": true,
"license": "MIT",
"peer": true,
"dependencies": {
"@oxc-project/types": "=0.154.0",
"@rolldown/pluginutils": "^1.0.0"
@ -1354,6 +1383,7 @@
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
"devOptional": true,
"license": "Apache-2.0",
"peer": true,
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
@ -1372,7 +1402,7 @@
"version": "8.9.0",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-8.9.0.tgz",
"integrity": "sha512-KTDyRTYX8sWmKXAikPHHSyc63CRPETMctyjKFupcC6OBLXT3xsN0e9aF7m+mIXutFWpUXuedtowG7iLOzp0kQg==",
"devOptional": true,
"dev": true,
"license": "MIT"
},
"node_modules/unplugin": {
@ -1455,6 +1485,7 @@
"integrity": "sha512-IftLE8phJhdNRIGNEV4JCCvfNScoZQVfcXaN8TQropWGJtOHcu+iAB9pHcuGvmEb/kddw0LBe/jS8ztdC6xdiQ==",
"devOptional": true,
"license": "MIT",
"peer": true,
"dependencies": {
"lightningcss": "^1.33.0",
"picomatch": "^4.0.7",
@ -1539,6 +1570,7 @@
"resolved": "https://registry.npmjs.org/vue/-/vue-3.5.44.tgz",
"integrity": "sha512-dvvmaOCG7lAMkJTPqPwWeGMsPS6DIsFsfZDUgBp9V6mHf9b3+/wyVwYR4Es+WV6uJC0FKRxp81Obcq0ZwUcGFQ==",
"license": "MIT",
"peer": true,
"dependencies": {
"@vue/compiler-dom": "3.5.44",
"@vue/compiler-sfc": "3.5.44",

View File

@ -448,8 +448,13 @@ onMounted(() => load(false));
</label>
<label class="field span-2">
<span>正文(Markdown:标题 # / ##、列表、代码块、链接、粗体)</span>
<textarea v-model="articleForm.content" rows="18" spellcheck="false" placeholder="## 小标题&#10;&#10;正文内容…" />
<span>正文(Markdown:标题 # / ##、列表、代码块、链接、粗体、图片)</span>
<textarea
v-model="articleForm.content"
rows="18"
spellcheck="false"
placeholder="## 小标题&#10;&#10;正文内容…&#10;&#10;![图片说明](/download/doc-images/example.png)"
/>
</label>
</div>

View File

@ -127,6 +127,7 @@
"cpu": [
"arm"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -143,6 +144,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -159,6 +161,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -175,6 +178,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -191,6 +195,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -207,6 +212,7 @@
"cpu": [
"arm"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -223,9 +229,7 @@
"cpu": [
"arm64"
],
"libc": [
"glibc"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -242,9 +246,7 @@
"cpu": [
"arm64"
],
"libc": [
"musl"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -261,9 +263,7 @@
"cpu": [
"ppc64"
],
"libc": [
"glibc"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -280,9 +280,7 @@
"cpu": [
"s390x"
],
"libc": [
"glibc"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -299,9 +297,7 @@
"cpu": [
"x64"
],
"libc": [
"glibc"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -318,9 +314,7 @@
"cpu": [
"x64"
],
"libc": [
"musl"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -337,6 +331,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -353,6 +348,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -369,6 +365,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
@ -849,6 +846,7 @@
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
"integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
"dev": true,
"hasInstallScript": true,
"license": "MIT",
"optional": true,
@ -902,6 +900,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -922,6 +921,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -942,6 +942,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -962,6 +963,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -982,6 +984,7 @@
"cpu": [
"arm"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -1002,9 +1005,7 @@
"cpu": [
"arm64"
],
"libc": [
"glibc"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -1025,9 +1026,7 @@
"cpu": [
"arm64"
],
"libc": [
"musl"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -1048,9 +1047,7 @@
"cpu": [
"x64"
],
"libc": [
"glibc"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -1071,9 +1068,7 @@
"cpu": [
"x64"
],
"libc": [
"musl"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -1094,6 +1089,7 @@
"cpu": [
"arm64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -1114,6 +1110,7 @@
"cpu": [
"x64"
],
"dev": true,
"license": "MPL-2.0",
"optional": true,
"os": [
@ -1338,6 +1335,7 @@
"integrity": "sha512-bWvlMpFKiTfDKfnK7FC2MPgPeWCf/Kpwsq49ApdW+FQaBZb6O6dZ4MAPbPUjp7hYQrw/RobqCzn64BRrQecGHw==",
"devOptional": true,
"license": "MIT",
"peer": true,
"dependencies": {
"@oxc-project/types": "=0.156.0",
"@rolldown/pluginutils": "^1.0.0"
@ -1403,6 +1401,7 @@
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
"devOptional": true,
"license": "Apache-2.0",
"peer": true,
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
@ -1498,6 +1497,7 @@
"integrity": "sha512-z1YpjieJ9g0ebTmQpLBv/oq9E3F9EIlOe+7LahW8SrE3LTiG3udBRUXQOlYXP9b+IC2q2phHeVxj9xGlw34OEQ==",
"devOptional": true,
"license": "MIT",
"peer": true,
"dependencies": {
"lightningcss": "^1.33.0",
"picomatch": "^4.0.7",
@ -1617,6 +1617,7 @@
"resolved": "https://registry.npmjs.org/vue/-/vue-3.5.44.tgz",
"integrity": "sha512-dvvmaOCG7lAMkJTPqPwWeGMsPS6DIsFsfZDUgBp9V6mHf9b3+/wyVwYR4Es+WV6uJC0FKRxp81Obcq0ZwUcGFQ==",
"license": "MIT",
"peer": true,
"dependencies": {
"@vue/compiler-dom": "3.5.44",
"@vue/compiler-sfc": "3.5.44",