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

25
src/escape.rs Normal file
View File

@@ -0,0 +1,25 @@
//! Small text/JS escaping helpers shared by the page builder, the Markdown
//! pipeline and the renderer.
pub fn escape_js(s: &str) -> String {
s.replace('\\', "\\\\")
.replace('\'', "\\'")
.replace('\n', "\\n")
.replace('\r', "\\r")
}
pub fn html_escape_ta(s: &str) -> String {
s.replace('&', "&amp;").replace('<', "&lt;")
}
pub fn html_escape_attr(s: &str) -> String {
s.replace('&', "&amp;")
.replace('"', "&quot;")
.replace('<', "&lt;")
}
pub fn html_escape_text(s: &str) -> String {
s.replace('&', "&amp;")
.replace('<', "&lt;")
.replace('>', "&gt;")
}