Files
md-preview/src/page.rs
Richard Wu 1115052762 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.
2026-11-03 23:02:58 +08:00

66 lines
2.8 KiB
Rust

//! The single in-memory HTML document the webview loads.
//!
//! Markup, CSS and JS live in `assets/page/`; this module fills the
//! `__MDP_*__` placeholders. Keeping the UI out of the Rust source means
//! editing the page no longer requires escaping braces inside a `format!`
//! string, and the JS can be syntax-checked as a real file.
//!
//! User content is substituted last so a document that happens to contain a
//! placeholder-looking string is not rewritten by an earlier replacement.
use crate::escape::{escape_js, html_escape_attr, html_escape_ta};
use crate::EnhanceFlags;
use crate::Strings;
const PAGE_HTML: &str = include_str!("../assets/page/page.html");
const PAGE_CSS: &str = include_str!("../assets/page/page.css");
const PAGE_JS: &str = include_str!("../assets/page/page.js");
const PREVIEW_ENHANCE_JS: &str = include_str!("../assets/enhance/preview-enhance.js");
const HLJS_CODE: &str = include_str!("../assets/hljs/vs-dark.css");
pub fn build_page(
preview_html: &str,
raw_md: &str,
base_href: Option<&str>,
flags: EnhanceFlags,
s: &Strings,
home: bool,
) -> String {
let base_tag = base_href
.map(|href| format!(r#"<base id="base-href" href="{}">"#, html_escape_attr(href)))
.unwrap_or_else(|| r#"<base id="base-href">"#.to_string());
PAGE_HTML
.replace("__MDP_CSS_CODE__", HLJS_CODE)
.replace("__MDP_PAGE_CSS__", PAGE_CSS)
.replace("__MDP_PAGE_JS__", PAGE_JS)
.replace("__MDP_PREVIEW_ENHANCE_JS__", PREVIEW_ENHANCE_JS)
.replace("__MDP_BASE_TAG__", &base_tag)
.replace("__MDP_BODY_CLASS__", if home { "home" } else { "" })
.replace("__MDP_NEW_TAB__", s.new_tab)
.replace("__MDP_BTN_OPEN_LOCATION__", s.btn_open_location)
.replace("__MDP_BTN_NEW_WINDOW__", s.btn_new_window)
.replace("__MDP_BTN_EDIT__", s.btn_edit)
.replace("__MDP_BTN_PREVIEW__", s.btn_preview)
.replace("__MDP_HOME_TAB__", &escape_js(s.home_tab))
.replace("__MDP_FOLD_TOGGLE__", s.fold_toggle)
.replace("__MDP_CANNOT_SAVE_JS__", &escape_js(s.cannot_save))
.replace("__MDP_BTN_ZOOM__", s.btn_zoom)
.replace("__MDP_BTN_ZOOM_OUT__", s.btn_zoom_out)
.replace("__MDP_BTN_ZOOM_RESET__", s.btn_zoom_reset)
.replace("__MDP_BTN_ZOOM_IN__", s.btn_zoom_in)
.replace("__MDP_SEARCH_PLACEHOLDER__", s.search_placeholder)
.replace("__MDP_STAT_WORDS_JS__", &escape_js(s.stat_words))
.replace("__MDP_STAT_CHARS_JS__", &escape_js(s.stat_chars))
.replace(
"__MDP_NEEDS_MATH__",
if flags.math { "true" } else { "false" },
)
.replace(
"__MDP_NEEDS_MERMAID__",
if flags.mermaid { "true" } else { "false" },
)
.replace("__MDP_PREVIEW_HTML__", preview_html)
.replace("__MDP_RAW_MD_ESCAPED__", &html_escape_ta(raw_md))
}