feat(dashboard): session detail page renders the logs/*.md content (markdown -> html)

This commit is contained in:
Lang
2026-10-14 21:03:16 +08:00
parent 7c7e3bf242
commit 097537edd3
+29 -53
View File
@@ -70,6 +70,19 @@ tr:last-child td{border-bottom:none}
.toolbar input{cursor:text;min-width:220px}
.muted{color:#8a94a6}
.wrapx{overflow:auto;max-height:72vh;border-radius:10px}
.md{background:#1b1e27;border:1px solid #2a2e3a;border-radius:10px;padding:4px 20px 18px;max-width:920px;margin:16px 0}
.md h1{font-size:22px;border-bottom:1px solid #232b38;padding-bottom:.3em}
.md h2{font-size:16px;text-transform:none;letter-spacing:0;color:#dfe5ee;border-bottom:1px solid #232b38;padding-bottom:.3em;margin-top:1.3em}
.md h3{font-size:14px;color:#c7d0de}
.md table{background:transparent;border:1px solid #2a2e3a;margin:10px 0}
.md th{background:#1e222b}
.md code{background:#0f1218;padding:1px 5px;border-radius:4px}
.md pre{background:#0f1218;border:1px solid #2a2e3a;border-radius:8px;padding:12px;overflow:auto;font-size:12.5px}
.md pre code{background:none;padding:0}
.md ul{padding-left:22px}.md li{margin:2px 0}
.md a{color:#7cc4ff}
.md hr{border:0;border-top:1px solid #232b38;margin:18px 0}
.md blockquote{border-left:3px solid #2f5f96;margin:10px 0;padding:2px 14px;color:#b9c3d3}
"""
# 页头/页脚:banner 结构(参考 FRA 模板)
@@ -128,60 +141,25 @@ CHART_JS = r"""
# ---------- 服务端渲染小工具(会话详情页用) ----------
KIND_CN = {"word": "单词", "phrase": "词组", "expression": "表达"}
def esc(v) -> str:
return _html.escape("" if v is None else str(v))
def badge(s: str) -> str:
return f'<span class="badge b-{s}">{s}</span>' if s else ""
def render_md(text: str) -> str:
"""Markdown → HTML。有 markdown 库就用,没有则降级为 <pre>。"""
try:
import markdown as _md
return _md.markdown(text, extensions=["tables", "fenced_code"])
except ImportError:
return "<pre>" + _html.escape(text) + "</pre>"
def items_table(items: list[dict]) -> str:
if not items:
return '<p class="muted">无</p>'
head = ("<thead><tr><th>id</th><th>类型</th><th>条目</th><th>等级</th>"
"<th>释义</th><th>上下文</th><th>状态</th><th>修正</th></tr></thead>")
rows = "".join(
f'<tr><td class="mono">{esc(i["id"])}</td><td>{KIND_CN.get(i["kind"], esc(i["kind"]))}</td>'
f'<td><b>{esc(i["item"])}</b></td><td>{esc(i["level"])}</td><td>{esc(i["meaning"])}</td>'
f'<td class="muted">{esc(i["context"])}</td><td>{badge(i["status"])}</td>'
f'<td class="fx mono">{esc(i["fix"])}</td></tr>' for i in items)
return f"<table>{head}<tbody>{rows}</tbody></table>"
def grammar_table(grs: list[dict]) -> str:
if not grs:
return '<p class="muted">无</p>'
head = ("<thead><tr><th>id</th><th>类别</th><th>问题</th><th>规则</th>"
"<th>例子</th><th>状态</th></tr></thead>")
rows = "".join(
f'<tr><td class="mono">{esc(g["id"])}</td><td>{esc(g["category"])}</td>'
f'<td>{esc(g["issue"])}</td><td class="muted">{esc(g["rule"])}</td>'
f'<td class="fx mono">{esc(g["examples"])}</td><td>{badge(g["status"])}</td></tr>'
for g in grs)
return f"<table>{head}<tbody>{rows}</tbody></table>"
def session_body(s: dict, its: list[dict], grs: list[dict], files: list[str]) -> str:
kv = [("日期", s["date"]), ("类型", s["type"]), ("材料", s["material_level"]),
("听", s["listening"]), ("说", s["speaking"]), ("读", s["reading"]), ("写", s["writing"])]
cards = "".join(
f'<div class="card"><div class="k">{k}</div><div class="v">{esc(v) or "—"}</div></div>'
for k, v in kv)
note = esc(s["note"]) or '<span class="muted">—</span>'
links = "".join(f'<li><a href="../../logs/{esc(f)}">{esc(f)}</a></li>' for f in files) \
or '<li class="muted">无</li>'
return (
f'<p><a href="log.html">← 返回日志</a></p>'
f'<h1>{esc(s["date"])} · {esc(s["type"]) or "session"}</h1>'
f'<div class="cards">{cards}</div>'
f'<h2>备注</h2><p>{note}</p>'
f'<h2>相关词条</h2>{items_table(its)}'
f'<h2>相关语法</h2>{grammar_table(grs)}'
f'<h2>记录文件</h2><ul>{links}</ul>')
def session_body(s: dict, files: list[tuple[str, str]]) -> str:
head = (f'<p><a href="log.html">← 返回日志</a></p>'
f'<h1>{esc(s["date"])} · {esc(s["type"]) or "session"}</h1>')
if not files:
return head + '<p class="muted">没有对应的 logs/*.md 记录文件。</p>'
return head + "".join(f'<div class="md">{render_md(c)}</div>' for _, c in files)
def html_page(title: str, active: str, body: str, script: str, generated: str) -> str:
@@ -344,11 +322,9 @@ def main() -> int:
n = 0
for s in data["training_log"]:
date = s.get("date", "")
its = [i for i in payload["items"] if (i.get("added") or "") == date]
grs = [g for g in payload["grammar"]
if date and date in (g.get("first", ""), g.get("last", ""))]
files = sorted(p.name for p in logs_dir.glob(f"{date}*.md")) if logs_dir.exists() else []
body = session_body(s, its, grs, files)
files = [(p.name, p.read_text(encoding="utf-8"))
for p in sorted(logs_dir.glob(f"{date}*.md"))] if logs_dir.exists() else []
body = session_body(s, files)
(OUT / f"session-{s['id']}.html").write_text(
html_page(f"{date} {s.get('type','')}", "log", body, "", gen), encoding="utf-8")
n += 1