refactor: split the page, markdown pipeline and escaping out of main.rs

- assets/page/{page.html,page.css,page.js} now hold the UI. build_page only
  fills __MDP_*__ placeholders, so editing the page no longer means escaping
  braces inside a format! string. Output was verified byte-identical for the
  home page, a math/mermaid document and the startup page.
- src/markdown.rs: md -> html pipeline (GFM, AI-output dialect, front matter,
  embedded images, ==highlight==, heading ids).
- src/page.rs: page assembly.
- src/escape.rs: shared JS/HTML escaping.
- src/main.rs drops from 5535 to 3709 lines. No behavior change; 62 tests.
This commit is contained in:
2026-11-03 23:02:58 +08:00
parent 44fe93ec20
commit 1115052762
7 changed files with 1887 additions and 1835 deletions

376
assets/page/page.css Normal file
View File

@@ -0,0 +1,376 @@
:root { color-scheme: light dark; --chrome-top: 10px; --content-scale: 1; }
/* Reserve scrollbar space permanently so the fixed toolbar doesn't shift
between modes (one with scrollbar, one without). */
html { overflow-y: scroll; scrollbar-gutter: stable; }
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
margin: 0; padding: 0;
line-height: 1.6; font-size: 15px;
color: #1a1a1a; background: #fff;
}
body.has-tabs { --chrome-top: 50px; }
#app { width: 100%; box-sizing: border-box; margin: 0 auto; padding: 24px 48px; }
#preview { font-size: calc(15px * var(--content-scale)); }
#preview .front-matter {
margin: 0 0 1.5em;
padding: .85em 0;
border-top: 1px solid #e1e4e8;
border-bottom: 1px solid #e1e4e8;
color: #59636e;
}
#preview .front-matter pre {
margin: 0;
padding: 0;
border-radius: 0;
overflow: visible;
background: transparent;
color: inherit;
font: inherit;
white-space: pre-wrap;
overflow-wrap: anywhere;
}
#preview h1,#preview h2,#preview h3,#preview h4 { margin-top: 1.4em; }
#preview h1 { border-bottom: 1px solid #e1e4e8; padding-bottom: .3em; }
#preview h2 { border-bottom: 1px solid #e1e4e8; padding-bottom: .2em; }
/* Heading folding: triangle shows on heading hover; the body collapses down to
the next heading of the same or higher level. State is per-render only. */
#preview .mdp-fold > h1,
#preview .mdp-fold > h2,
#preview .mdp-fold > h3,
#preview .mdp-fold > h4,
#preview .mdp-fold > h5,
#preview .mdp-fold > h6 { position: relative; }
/* Fixed 90%-of-H2 size for every heading level. */
#preview .mdp-fold-toggle {
position: absolute; left: -34px; top: 50%;
width: 30px; height: 30px; padding: 0; border: 0; border-radius: 50%;
transform: translateY(-50%);
display: grid; place-items: center;
background: transparent; color: #999; cursor: pointer;
opacity: 0; transition: opacity .12s ease, background .12s ease;
}
#preview .mdp-fold-toggle svg { grid-area: 1 / 1; overflow: visible; }
#preview .mdp-fold-toggle .mdp-fold-i-right { display: none; width: 13.5px; height: 20px; }
#preview .mdp-fold-toggle .mdp-fold-i-down { display: block; width: 20px; height: 13.5px; }
#preview h1:hover > .mdp-fold-toggle,
#preview h2:hover > .mdp-fold-toggle,
#preview h3:hover > .mdp-fold-toggle,
#preview h4:hover > .mdp-fold-toggle,
#preview h5:hover > .mdp-fold-toggle,
#preview h6:hover > .mdp-fold-toggle,
#preview .mdp-fold-toggle:focus-visible { opacity: .6; }
#preview .mdp-fold-toggle:hover { opacity: 1; background: rgba(0,0,0,.07); }
#preview .mdp-fold.collapsed > :is(h1, h2, h3, h4, h5, h6) > .mdp-fold-toggle .mdp-fold-i-right { display: block; }
#preview .mdp-fold.collapsed > :is(h1, h2, h3, h4, h5, h6) > .mdp-fold-toggle .mdp-fold-i-down { display: none; }
#preview .mdp-fold.collapsed > .mdp-fold-body { display: none; }
#preview code { background: #f0f0f0; padding: 2px 6px; border-radius: 4px; font-size: 90%; }
/* Code blocks stay dark (VS Dark) even in the light app theme. */
#preview pre { background: #1e1e1e; color: #d4d4d4; padding: 16px; border-radius: 8px; overflow-x: auto; }
#preview pre code { background: none; padding: 0; font-size: 14px; }
#preview blockquote { border-left: 4px solid #ddd; margin: 0; padding: 0 1em; color: #666; }
#preview .markdown-alert-note,
#preview .markdown-alert-tip,
#preview .markdown-alert-important,
#preview .markdown-alert-warning,
#preview .markdown-alert-caution {
margin: 1em 0;
padding: 0.75em 1em;
border-radius: 6px;
color: inherit;
}
#preview .markdown-alert-title {
display: flex;
align-items: center;
gap: .35em;
margin: 0 0 .45em;
font-weight: 600;
line-height: 1.25;
}
#preview .markdown-alert-title + p { margin-top: 0; }
#preview .markdown-alert-note { border-color: #0969da; background: #ddf4ff; }
#preview .markdown-alert-tip { border-color: #1a7f37; background: #dafbe1; }
#preview .markdown-alert-important { border-color: #8250df; background: #fbefff; }
#preview .markdown-alert-warning { border-color: #9a6700; background: #fff8c5; }
#preview .markdown-alert-caution { border-color: #cf222e; background: #ffebe9; }
#preview .markdown-alert-note .markdown-alert-title { color: #0969da; }
#preview .markdown-alert-tip .markdown-alert-title { color: #1a7f37; }
#preview .markdown-alert-important .markdown-alert-title { color: #8250df; }
#preview .markdown-alert-warning .markdown-alert-title { color: #9a6700; }
#preview .markdown-alert-caution .markdown-alert-title { color: #cf222e; }
#preview .mdp-mark { border-radius: 3px; padding: 0 0.12em; background: #fff2a8; color: inherit; }
#preview mark.search-hit { border-radius: 3px; padding: 0 0.12em; background: #fff2a8; color: inherit; }
#preview mark.search-hit.current { background: #ffcc4d; color: #1a1a1a; }
#preview table { border-collapse: collapse; width: 100%; }
#preview .mdp-table-wrap {
width: min(calc(100vw - 64px), 1280px);
margin: 1em 0 1em 50%;
transform: translateX(-50%);
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
#preview .mdp-table-wrap table { width: max-content; min-width: 100%; }
#preview table th, #preview table td { border: 1px solid #ddd; padding: 8px 12px; text-align: left; }
#preview table th { background: #f6f8fa; font-weight: 600; color: #1a1a1a; white-space: nowrap; }
#preview table td { min-width: 64px; max-width: 360px; vertical-align: top; overflow-wrap: break-word; }
#preview img { max-width: 100%; }
#preview .katex-display { overflow-x: auto; overflow-y: hidden; padding: 0.15em 0; }
#preview .mdp-mermaid { margin: 1.2em 0; overflow-x: auto; text-align: center; }
/* Compensate zoom's percentage width so window-fitted diagrams can grow and scroll. */
#preview .mdp-mermaid svg { max-width: 100%; height: auto; display: block; margin: 0 auto; width: calc(100% * var(--content-scale)); zoom: var(--content-scale); }
#preview .mdp-mermaid-error, #preview .mdp-math-error { color: #b42318; }
#preview hr { border: none; border-top: 1px solid #e1e4e8; margin: 2em 0; }
#preview a { color: #0969da; text-decoration: none; }
#preview a:hover { text-decoration: underline; }
#preview ul, #preview ol { padding-left: 2em; }
#preview input[type="checkbox"] { margin-right: 6px; }
#preview dl { margin: 1em 0; }
#preview dt { font-weight: 600; }
#preview dd { margin: 0 0 .6em 1.5em; }
#preview .footnote-reference { font-size: .75em; }
#preview .footnote-definition {
margin-top: 1.5em;
padding-top: .75em;
border-top: 1px solid #e1e4e8;
color: #59636e;
font-size: .9em;
}
#preview .footnote-definition-label { margin-right: .35em; font-weight: 600; }
.empty { display: flex; flex-direction: column; align-items: center; justify-content: center;
min-height: 60vh; color: #999; font-size: 18px; gap: 12px; text-align: center; }
.empty.has-recent {
justify-content: flex-start;
min-height: calc(100vh - var(--chrome-top) - 38px);
padding: clamp(56px, 10vh, 96px) 0 40px;
box-sizing: border-box;
}
.empty .icon { font-size: 48px; opacity: 0.4; }
.empty-actions { display: flex; gap: 8px; margin-top: 6px; }
.empty-open {
min-height: 40px; padding: 0 16px;
border: 1px solid #ddd; border-radius: 8px; background: #fff;
color: #1a1a1a; font: inherit; font-size: 15px; cursor: pointer;
}
.empty-open:hover { background: #f5f5f5; color: #000; }
.recent { width: min(480px, 100%); margin-top: 16px; text-align: left; }
.recent-title { margin: 0 0 8px; padding: 0; border: 0; font-size: 11px; font-weight: 600; letter-spacing: 0; color: #b6b6b6; text-transform: uppercase; }
.recent-list { display: grid; gap: 6px; }
.recent-item {
width: 100%; min-height: 44px; padding: 7px 10px; border: 1px solid #eee;
border-radius: 8px; background: #fff; color: inherit; text-align: left; cursor: pointer;
display: grid; gap: 1px;
}
.recent-item:hover { background: #f7f7f7; }
.recent-name { color: #555; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent-path { color: #aaa; font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tabbar {
display: none; position: sticky; top: 0; z-index: 110; height: 40px;
box-sizing: border-box; align-items: stretch; gap: 4px; padding: 4px 8px;
border-bottom: 1px solid #e6e6e6; background: rgba(248,248,248,0.96);
backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
body.has-tabs .tabbar { display: flex; }
.tabs { display: flex; flex: 1; min-width: 0; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
flex: 0 1 180px; min-width: 96px; max-width: 200px; height: 31px;
display: flex; align-items: center; gap: 7px; padding: 0 8px 0 10px;
box-sizing: border-box; border: 1px solid transparent; border-radius: 7px;
color: #6b6b6b; background: transparent; cursor: default; user-select: none;
font-size: 13px;
}
.tab:hover { background: rgba(0,0,0,0.045); }
.tab.dragging { opacity: .55; cursor: grabbing; }
.tab.active { color: #202020; background: #fff; border-color: #ddd; box-shadow: 0 1px 2px rgba(0,0,0,.04); }
.tab.missing { color: #a15c00; }
.tab-status { width: 7px; height: 7px; flex: 0 0 auto; border-radius: 50%; background: transparent; }
.tab.dirty .tab-status { background: #2979c9; }
.tab.missing .tab-status { width: auto; height: auto; background: none; border-radius: 0; font-weight: 700; }
.tab-name { min-width: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab-close {
width: 20px; height: 20px; flex: 0 0 auto; padding: 0; border: 0; border-radius: 5px;
display: grid; place-items: center; color: inherit; background: transparent; cursor: pointer;
font: 16px/1 -apple-system, BlinkMacSystemFont, sans-serif; opacity: .58;
}
.tab-close:hover { opacity: 1; background: rgba(0,0,0,.08); }
.tab-close[hidden] { display: none; }
.tab-menu {
position: fixed; z-index: 200; min-width: 188px; padding: 4px;
background: #fff; border: 1px solid #ddd; border-radius: 8px;
box-shadow: 0 8px 24px rgba(0,0,0,.16);
}
.tab-menu[hidden] { display: none; }
.tab-menu-path {
max-width: 280px; padding: 3px 8px 5px; overflow: hidden;
color: #8a8a8a; font-size: 11px; line-height: 1.3;
white-space: nowrap; text-overflow: ellipsis;
}
.tab-menu button {
display: block; width: 100%; padding: 5px 8px; border: 0; border-radius: 5px;
background: transparent; color: #222; text-align: left; font: inherit;
font-size: 13px; cursor: pointer;
}
.tab-menu button:hover { background: #f2f2f2; }
.tab-menu button:disabled { opacity: .42; cursor: default; }
.tab-menu button:disabled:hover { background: transparent; }
.tab-open {
width: 31px; height: 31px; flex: 0 0 auto; padding: 0; border: 0; border-radius: 7px;
color: #666; background: transparent; cursor: pointer; font: 20px/1 -apple-system, sans-serif;
}
.tab-open:hover { color: #111; background: rgba(0,0,0,.06); }
.doc-stats {
flex: 0 0 auto; align-self: center; padding: 0 6px;
color: #8b8b8b; font-size: 11px; white-space: nowrap; user-select: none;
font-variant-numeric: tabular-nums;
}
.missing-file { min-height: 55vh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; text-align: center; }
.missing-file h2, .missing-file p { margin: 0; }
.missing-file p { color: #777; }
.missing-file code { max-width: min(620px, 90vw); overflow-wrap: anywhere; color: #8a5a12; }
.missing-mark { width: 38px; height: 38px; border: 2px solid #c98322; border-radius: 50%; display: grid; place-items: center; color: #a15c00; font-weight: 700; font-size: 22px; }
.missing-actions { display: flex; gap: 8px; margin-top: 8px; }
.missing-actions button { min-height: 36px; padding: 0 13px; border: 1px solid #d8d8d8; border-radius: 7px; background: #fff; color: #333; cursor: pointer; font: inherit; }
.missing-actions button:hover { background: #f4f4f4; }
/* Floating toolbar (top-right) — hover-reveal, hidden in empty state */
.toolbar {
position: fixed; top: var(--chrome-top); right: 12px;
display: flex; gap: 6px; z-index: 100;
opacity: 0; pointer-events: none;
transition: opacity 0.18s ease;
}
html:hover .toolbar { opacity: 1; pointer-events: auto; }
body.home .toolbar { display: none !important; }
.toolbar button {
width: 34px; height: 34px; padding: 0;
background: rgba(255,255,255,0.8);
backdrop-filter: blur(6px);
-webkit-backdrop-filter: blur(6px);
border: 1px solid rgba(0,0,0,0.08);
border-radius: 8px;
display: grid; place-items: center;
cursor: pointer; color: #555;
transition: color 0.15s, background 0.15s;
}
.toolbar button:hover { color: #000; background: rgba(255,255,255,1); }
.toolbar button[hidden] { display: none !important; }
.zoom-control { position: relative; }
.zoom-popover {
position: absolute; top: 40px; right: 0;
display: none; align-items: center; gap: 2px; padding: 4px;
border: 1px solid rgba(0,0,0,.1); border-radius: 8px;
background: rgba(255,255,255,.96); box-shadow: 0 6px 20px rgba(0,0,0,.12);
backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.zoom-control.open .zoom-popover { display: flex; }
.toolbar .zoom-popover button { width: 30px; height: 30px; border: 0; background: transparent; }
.toolbar .zoom-popover button:hover { background: rgba(0,0,0,.06); }
.toolbar .zoom-popover .zoom-reset {
width: 52px; font-size: 11px; font-variant-numeric: tabular-nums;
}
.findbar {
position: fixed; top: var(--chrome-top); left: 50%; transform: translateX(-50%);
display: none; align-items: center; gap: 6px; z-index: 101;
padding: 6px; border: 1px solid rgba(0,0,0,0.08); border-radius: 10px;
background: rgba(255,255,255,0.96); box-shadow: 0 8px 24px rgba(0,0,0,0.12);
backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
body.finding .findbar { display: flex; }
.findbar input {
width: min(42vw, 320px); height: 30px; box-sizing: border-box; border: 0;
outline: none; background: transparent; color: inherit; font: inherit;
}
.findbar span { min-width: 14px; color: #8c959f; text-align: center; }
.findbar button {
width: 30px; height: 30px; padding: 0; border: 0; border-radius: 7px;
display: grid; place-items: center; color: #555; background: transparent; cursor: pointer;
}
.findbar button:hover { background: #f0f0f0; color: #111; }
@media (prefers-color-scheme: dark) {
body { color: #d4d4d4; background: #1e1e1e; }
#preview a { color: #6cb6ff; }
#preview h1, #preview h2 { border-color: #333; }
#preview .mdp-fold-toggle { color: #aaa; }
#preview .mdp-fold-toggle:hover { background: rgba(255,255,255,.1); }
#preview .front-matter { border-color: #333; color: #a9b1ba; }
#preview .front-matter pre { background: transparent !important; }
#preview code:not(pre code) { background: #2d2d2d; }
#preview blockquote { border-color: #444; color: #aaa; }
#preview .footnote-definition { border-color: #444; color: #aaa; }
#preview .markdown-alert-note,
#preview .markdown-alert-tip,
#preview .markdown-alert-important,
#preview .markdown-alert-warning,
#preview .markdown-alert-caution { background: #161b22; color: #d4d4d4; }
#preview .markdown-alert-note { border-color: #2f81f7; }
#preview .markdown-alert-tip { border-color: #3fb950; }
#preview .markdown-alert-important { border-color: #a371f7; }
#preview .markdown-alert-warning { border-color: #d29922; }
#preview .markdown-alert-caution { border-color: #f85149; }
#preview .markdown-alert-note .markdown-alert-title { color: #2f81f7; }
#preview .markdown-alert-tip .markdown-alert-title { color: #3fb950; }
#preview .markdown-alert-important .markdown-alert-title { color: #a371f7; }
#preview .markdown-alert-warning .markdown-alert-title { color: #d29922; }
#preview .markdown-alert-caution .markdown-alert-title { color: #f85149; }
#preview table th { background: #2d2d2d; color: #f0f0f0; }
#preview table td, #preview table th { border-color: #444; }
#preview hr { border-color: #333; }
.toolbar button {
background: rgba(40,40,40,0.8);
border-color: rgba(255,255,255,0.1);
color: #bbb;
}
.toolbar button:hover { color: #fff; background: rgba(55,55,55,1); }
.zoom-popover { background: rgba(34,34,34,.96); border-color: rgba(255,255,255,.12); }
.toolbar .zoom-popover button:hover { background: rgba(255,255,255,.1); }
.empty-open { background: #242424; border-color: #444; color: #ddd; }
.empty-open:hover { background: #2d2d2d; color: #fff; }
.recent-name { color: #ddd; }
.recent-item { background: #242424; border-color: #333; }
.recent-item:hover { background: #2d2d2d; }
.findbar { background: rgba(34,34,34,0.96); border-color: rgba(255,255,255,0.1); }
.findbar button:hover { background: #333; color: #fff; }
.tabbar { background: rgba(28,28,28,.96); border-color: #363636; }
.tab { color: #aaa; }
.tab:hover { background: rgba(255,255,255,.07); }
.tab.active { color: #eee; background: #2c2c2c; border-color: #444; }
.tab.missing { color: #e3a04b; }
.tab-close:hover, .tab-open:hover { background: rgba(255,255,255,.1); color: #fff; }
.tab-menu { background: #2a2a2a; border-color: #444; box-shadow: 0 8px 24px rgba(0,0,0,.45); }
.tab-menu-path { color: #999; }
.tab-menu button { color: #ddd; }
.tab-menu button:hover { background: rgba(255,255,255,.08); }
.tab-menu button:disabled:hover { background: transparent; }
.missing-file p { color: #aaa; }
.missing-actions button { background: #292929; border-color: #444; color: #ddd; }
.missing-actions button:hover { background: #333; }
}
/* Source editor textarea — height is auto-grown by JS to match content,
so the page (html) owns the only vertical scrollbar. */
#editor {
display: none;
width: 100%;
box-sizing: border-box;
border: none; outline: none; resize: none;
overflow: hidden;
font: calc(14px * var(--content-scale))/1.6 "SF Mono","Menlo","Consolas",monospace;
background: transparent; color: inherit;
padding: 0;
}
body.editing #preview { display: none; }
body.editing #editor { display: block; padding: 16px 48px; }
body.editing #app { max-width: none; padding: 0; }
@page {
margin: 12mm;
}
@media print {
.toolbar, .tabbar, #editor { display: none !important; }
#preview { display: block !important; }
#app { max-width: none; padding: 0; }
#preview .mdp-table-wrap { width: auto; margin: 1em 0; transform: none; overflow: visible; }
#preview pre { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
#preview .mdp-fold-body { display: block !important; }
#preview .mdp-fold-toggle { display: none !important; }
}