feat: 静态资源与前端 Markdown、浏览量、Gitea 同步内容

- 将 Markdown/KaTeX/高亮/Mermaid 移至 public 与 CDN,减小 Worker 体积
- 文章列表:标题、简介、时间与浏览量;访问文章时递增 view_count
- 新增 migrations/0003 与 blog 路由等配置
- 加入 public 资源与站点图标

Made-with: Cursor
This commit is contained in:
2026-04-14 14:07:46 +08:00
parent 276a7fbc83
commit 323869e6ca
9 changed files with 499 additions and 157 deletions

127
public/css/markdown.css Normal file
View File

@@ -0,0 +1,127 @@
.markdown-body {
overflow-wrap: break-word;
}
.markdown-body > *:first-child {
margin-top: 0;
}
.markdown-body > *:last-child {
margin-bottom: 0;
}
.markdown-body h1 {
font-size: 1.75rem;
margin: 24px 0 12px;
}
.markdown-body h2 {
font-size: 1.45rem;
margin: 22px 0 10px;
}
.markdown-body h3 {
font-size: 1.2rem;
margin: 18px 0 8px;
}
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
font-size: 1.05rem;
margin: 16px 0 8px;
}
.markdown-body p {
margin: 0 0 12px;
}
.markdown-body ul,
.markdown-body ol {
margin: 0 0 12px;
padding-left: 1.6em;
}
.markdown-body li {
margin: 0 0 4px;
}
.markdown-body li > p {
margin: 0 0 6px;
}
.markdown-body blockquote {
border-left: 4px solid #ccc;
margin: 0 0 16px;
padding: 2px 0 2px 16px;
color: #444;
}
.markdown-body blockquote > :last-child {
margin-bottom: 0;
}
.markdown-body pre {
padding: 0;
margin: 0 0 16px;
border-radius: 6px;
overflow-x: auto;
font-size: 0.9em;
background: #f6f8fa;
}
.markdown-body pre code.hljs {
display: block;
padding: 12px 14px;
overflow-x: auto;
background: transparent;
}
.markdown-body code {
font-family: ui-monospace, "Cascadia Code", "Source Code Pro", Menlo, Consolas, monospace;
font-size: 0.9em;
}
.markdown-body pre code {
background: none;
padding: 0;
font-size: inherit;
}
.markdown-body :not(pre) > code {
background: #f0f0f0;
padding: 2px 6px;
border-radius: 4px;
}
.markdown-body table {
border-collapse: collapse;
width: 100%;
margin: 0 0 16px;
font-size: 0.95em;
}
.markdown-body th,
.markdown-body td {
border: 1px solid #ccc;
padding: 8px 10px;
vertical-align: top;
}
.markdown-body th {
background: #f5f5f5;
font-weight: 600;
}
.markdown-body hr {
margin: 20px 0;
}
.markdown-body a {
text-decoration: underline;
}
.markdown-body li:has(> input[type="checkbox"]) {
list-style: none;
margin-left: -1.5em;
padding-left: 0;
}
.markdown-body input[type="checkbox"] {
margin-right: 6px;
vertical-align: middle;
}
.markdown-body .katex-display {
overflow-x: auto;
overflow-y: hidden;
max-width: 100%;
padding: 8px 0;
}
.markdown-body .katex {
max-width: 100%;
}
.markdown-body .mermaid {
margin: 16px 0;
overflow-x: auto;
text-align: center;
}
.markdown-pending .markdown-loading {
color: #666;
margin: 0;
}

BIN
public/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 KiB

View File

@@ -0,0 +1,96 @@
import { marked } from "https://esm.sh/marked@15";
import markedKatex from "https://esm.sh/marked-katex-extension@5?deps=marked@15,katex@0.16";
import hljs from "https://esm.sh/highlight.js@11";
import mermaid from "https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs";
marked.use(
markedKatex({
throwOnError: false,
nonStandard: true
})
);
marked.setOptions({ gfm: true, breaks: false });
function decodeHtmlEntities(str) {
return String(str)
.replace(/ /g, "\u00a0")
.replace(/&/g, "&")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
.replace(/&#(\d+);/g, (_, n) => {
const code = Number(n);
return code >= 0 && code <= 0x10ffff ? String.fromCharCode(code) : "";
})
.replace(/&#x([0-9a-fA-F]+);/g, (_, h) => {
const code = parseInt(h, 16);
return code >= 0 && code <= 0x10ffff ? String.fromCharCode(code) : "";
});
}
function escapeHtml(s) {
return String(s)
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#39;");
}
function mermaidFencedToDiv(html) {
return html.replace(
/<pre><code class="language-mermaid">([\s\S]*?)<\/code><\/pre>/gi,
(_, inner) => {
const raw = decodeHtmlEntities(inner);
return `<div class="mermaid">${escapeHtml(raw)}</div>`;
}
);
}
function decodeMdFromB64(b64) {
return decodeURIComponent(escape(atob(b64)));
}
async function renderOne(el) {
const b64 = el.getAttribute("data-b64-md");
if (!b64) return;
let md;
try {
md = decodeMdFromB64(b64);
} catch {
el.innerHTML = "<p>正文解码失败。</p>";
el.classList.remove("markdown-pending");
return;
}
el.removeAttribute("data-b64-md");
let html = marked.parse(md, { async: false });
if (typeof html !== "string") html = "";
html = mermaidFencedToDiv(html);
el.innerHTML = html;
el.classList.remove("markdown-pending");
el.querySelectorAll("pre code").forEach((block) => {
try {
hljs.highlightElement(block);
} catch {
/* ignore unknown languages */
}
});
const mNodes = el.querySelectorAll(".mermaid");
if (mNodes.length) {
mermaid.initialize({ startOnLoad: false, theme: "neutral" });
await mermaid.run({ nodes: mNodes });
}
}
function run() {
document.querySelectorAll(".markdown-body[data-b64-md]").forEach((el) => {
renderOne(el).catch(() => {
el.innerHTML = "<p>渲染失败。</p>";
el.classList.remove("markdown-pending");
});
});
}
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", run);
else run();

BIN
public/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB