feat: 共享 markdown 渲染器,完整语法支持

This commit is contained in:
2026-06-19 23:29:05 +08:00
parent 0528eee4de
commit e4df6487ea
5 changed files with 243 additions and 53 deletions
+3 -21
View File
@@ -3,6 +3,7 @@
<head>
{{template "head" .}}
<script src="/static/js/sse.js"></script>
<script src="/static/js/markdown.js"></script>
</head>
<body class="bg-gray-50 min-h-screen">
{{template "nav" .}}
@@ -266,28 +267,9 @@
});
}
// Simple markdown rendering (bold, headers, lists, code)
// Markdown rendering (delegates to shared Markdown.render)
function renderMarkdown(text) {
if (!text) return '';
let html = text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/^### (.+)$/gm, '<h3>$1</h3>')
.replace(/^## (.+)$/gm, '<h2>$1</h2>')
.replace(/^# (.+)$/gm, '<h1>$1</h1>')
.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>')
.replace(/`(.+?)`/g, '<code class="bg-gray-100 px-1 rounded">$1</code>')
.replace(/^- (.+)$/gm, '<li class="bullet-item">$1</li>')
.replace(/^(\d+)\. (.+)$/gm, '<li class="numbered-item">$2</li>')
.replace(/\n/g, '\n');
// Wrap consecutive bullet <li> in <ul>
html = html.replace(/((?:<li class="bullet-item">.*<\/li>\n?)+)/g, '<ul>$1</ul>');
// Wrap consecutive numbered <li> in <ol>
html = html.replace(/((?:<li class="numbered-item">.*<\/li>\n?)+)/g, '<ol>$1</ol>');
// Convert remaining newlines to <br> (outside lists)
html = html.replace(/\n/g, '<br>');
return html;
return Markdown.render(text);
}
function copyMarkdown() {