90 lines
3.6 KiB
TypeScript
90 lines
3.6 KiB
TypeScript
// utils/markdown.ts — Issue#4 迷你 Markdown 渲染器(发布日志专用,零依赖)。
|
|
//
|
|
// 支持范围(够用即止,不求 GFM 全集):
|
|
// 标题 #~####、无序列表(-/*)、有序列表(1. / 1、)、粗体 **x**、
|
|
// 行内代码 \`x\`、围栏代码块 \`\`\`、链接 [t](https://…)(仅 http/https)、
|
|
// 空行分段(段内换行 → <br>)。
|
|
// 安全口径:**输入先整体 HTML 转义再解析**——内容源虽是仓库静态数据
|
|
// (releases.ts),仍按不可信文本处理,任何标签/引号都不会进入 DOM;
|
|
// 链接仅允许 http(s) 协议(javascript: 伪协议不命中即保持原样文本)。
|
|
|
|
/** HTML 全量转义(含引号,防属性注入)。 */
|
|
function escapeHtml(s: string): string {
|
|
return s
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''')
|
|
}
|
|
|
|
/** 行内格式:行内代码(哨兵保护,内部不做粗体/链接替换)→ 粗体 → 链接。 */
|
|
const CODE_SENTINEL = '@@GOSMS_MD_CODE_'
|
|
function inline(s: string): string {
|
|
const codes: string[] = []
|
|
let t = s.replace(/`([^`]+)`/g, (_m, c: string) => {
|
|
codes.push(c)
|
|
return `${CODE_SENTINEL}${codes.length - 1}@@`
|
|
})
|
|
t = t.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
|
|
t = t.replace(/\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)/g, '<a href="$2" target="_blank" rel="noopener noreferrer">$1</a>')
|
|
return t.replace(new RegExp(`${CODE_SENTINEL}(\\d+)@@`, 'g'), (_m, i: string) => `<code>${codes[Number(i)]!}</code>`)
|
|
}
|
|
|
|
/** 块级行判据(段落收集的截断条件):代码围栏 / 标题 / 列表。 */
|
|
const BLOCK_START = /^(#{1,4}\s|\s*[-*]\s|\s*\d+(?:\.\s+|、\s*)|```)/
|
|
/** 有序列表行:点号后须空格(英文习惯),顿号后空格可选(中文习惯)。 */
|
|
const OL_LINE = /^\s*\d+(?:\.\s+|、\s*)/
|
|
|
|
/** Markdown → HTML(输入不可信,输出已全量转义;v-html 挂载)。 */
|
|
export function renderMarkdown(src: string): string {
|
|
const lines = escapeHtml(src.replace(/\r\n/g, '\n')).split('\n')
|
|
const out: string[] = []
|
|
let i = 0
|
|
while (i < lines.length) {
|
|
const line = lines[i]!
|
|
// 围栏代码块:内容保持原样(不再做行内格式化)
|
|
if (line.trim().startsWith('```')) {
|
|
const buf: string[] = []
|
|
i++
|
|
while (i < lines.length && !lines[i]!.trim().startsWith('```')) buf.push(lines[i++]!)
|
|
i++ // 跳过闭合围栏(缺闭合则吞到结尾)
|
|
out.push(`<pre><code>${buf.join('\n')}</code></pre>`)
|
|
continue
|
|
}
|
|
// 标题 # ~ ####
|
|
const h = /^(#{1,4})\s+(.*)$/.exec(line)
|
|
if (h) {
|
|
const lv = h[1]!.length
|
|
out.push(`<h${lv}>${inline(h[2]!)}</h${lv}>`)
|
|
i++
|
|
continue
|
|
}
|
|
// 无序列表
|
|
if (/^\s*[-*]\s+/.test(line)) {
|
|
const items: string[] = []
|
|
while (i < lines.length && /^\s*[-*]\s+/.test(lines[i]!)) items.push(inline(lines[i++]!.replace(/^\s*[-*]\s+/, '')))
|
|
out.push(`<ul>${items.map((x) => `<li>${x}</li>`).join('')}</ul>`)
|
|
continue
|
|
}
|
|
// 有序列表(1. 或 1、)
|
|
if (OL_LINE.test(line)) {
|
|
const items: string[] = []
|
|
while (i < lines.length && OL_LINE.test(lines[i]!)) items.push(inline(lines[i++]!.replace(OL_LINE, '')))
|
|
out.push(`<ol>${items.map((x) => `<li>${x}</li>`).join('')}</ol>`)
|
|
continue
|
|
}
|
|
// 空行
|
|
if (line.trim() === '') {
|
|
i++
|
|
continue
|
|
}
|
|
// 段落:连续普通行合并,行间 <br>
|
|
const buf: string[] = [line]
|
|
i++
|
|
while (i < lines.length && lines[i]!.trim() !== '' && !BLOCK_START.test(lines[i]!)) buf.push(lines[i++]!)
|
|
out.push(`<p>${buf.map(inline).join('<br>')}</p>`)
|
|
}
|
|
return out.join('\n')
|
|
}
|