| 1 | |
| 2 | |
| 3 | |
| 4 | use loro::LoroDoc; |
| 5 | use note_model::marks::Marks; |
| 6 | use note_model::render::{Embed, embed_of}; |
| 7 | use note_model::write::{Block, Inline}; |
| 8 | |
| 9 | |
| 10 | pub fn to_html(doc: &LoroDoc) -> String { |
| 11 | let blocks = note_model::flat::doc_blocks(doc); |
| 12 | render_blocks(&blocks) |
| 13 | } |
| 14 | |
| 15 | |
| 16 | |
| 17 | pub fn html_page(title: &str, body: &str, lang: &str, print: bool) -> String { |
| 18 | let script = if print { |
| 19 | "<script>addEventListener('load',()=>print())</script>" |
| 20 | } else { |
| 21 | "" |
| 22 | }; |
| 23 | format!( |
| 24 | "<!DOCTYPE html>\n<html lang=\"{lang}\">\n<head>\n<meta charset=\"utf-8\">\n\ |
| 25 | <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n\ |
| 26 | <title>{title}</title>\n<style>{STYLE}</style>\n</head>\n<body>\n\ |
| 27 | <main class=\"note\">\n{body}\n</main>\n{script}\n</body>\n</html>\n", |
| 28 | lang = esc(lang), |
| 29 | title = esc(title), |
| 30 | ) |
| 31 | } |
| 32 | |
| 33 | const STYLE: &str = "\ |
| 34 | body{margin:0;font-family:system-ui,-apple-system,\"Segoe UI\",Roboto,sans-serif;\ |
| 35 | font-size:16px;line-height:1.6;color:#1c1c1e;background:#fff}\ |
| 36 | .note{max-width:42rem;margin:2.5rem auto;padding:0 1.25rem}\ |
| 37 | h1,h2,h3,h4,h5,h6{line-height:1.25;margin:1.6em 0 .5em}\ |
| 38 | p,ul,ol,blockquote,pre,table{margin:0 0 1em}\ |
| 39 | ul,ol{padding-left:1.5em}\ |
| 40 | blockquote{margin-left:0;padding-left:1em;border-left:3px solid #d0d0d5;color:#555}\ |
| 41 | code{font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:.9em;\ |
| 42 | background:#f2f2f4;padding:.1em .3em;border-radius:4px}\ |
| 43 | pre{background:#f2f2f4;padding:.75em 1em;border-radius:6px;overflow-x:auto;white-space:pre}\ |
| 44 | pre code{background:none;padding:0;font-size:.875em}\ |
| 45 | img,video,audio{max-width:100%;height:auto}audio{display:block}\ |
| 46 | hr{border:0;border-top:1px solid #d0d0d5;margin:2em 0}\ |
| 47 | a{color:#0a58ca}\ |
| 48 | table{border-collapse:collapse;max-width:100%}\ |
| 49 | th,td{border:1px solid #d0d0d5;padding:.4em .6em;text-align:left}\ |
| 50 | @page{margin:2cm}\ |
| 51 | @media print{body{color:#000}.note{max-width:none;margin:0;padding:0}\ |
| 52 | a{color:inherit}pre,blockquote,img,video,audio{break-inside:avoid}pre{background:#f2f2f2}code{background:#f2f2f2}}"; |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | fn esc(s: &str) -> String { |
| 59 | let mut out = String::with_capacity(s.len()); |
| 60 | for c in s.chars() { |
| 61 | match c { |
| 62 | '&' => out.push_str("&"), |
| 63 | '<' => out.push_str("<"), |
| 64 | '>' => out.push_str(">"), |
| 65 | '"' => out.push_str("""), |
| 66 | '\'' => out.push_str("'"), |
| 67 | _ => out.push(c), |
| 68 | } |
| 69 | } |
| 70 | out |
| 71 | } |
| 72 | |
| 73 | |
| 74 | |
| 75 | fn safe_href(href: &str) -> Option<&str> { |
| 76 | let h = href.trim(); |
| 77 | if h.is_empty() { |
| 78 | return None; |
| 79 | } |
| 80 | let lower = h.to_ascii_lowercase(); |
| 81 | if lower.starts_with("http://") || lower.starts_with("https://") || lower.starts_with("mailto:") { |
| 82 | return Some(h); |
| 83 | } |
| 84 | |
| 85 | match h.find(':') { |
| 86 | Some(i) if !h[..i].contains(['/', '?', '#']) => None, |
| 87 | _ => Some(h), |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | fn safe_src(src: &str) -> Option<&str> { |
| 93 | let lower = src.trim().to_ascii_lowercase(); |
| 94 | if lower.starts_with("data:image/png") |
| 95 | || lower.starts_with("data:image/jpeg") |
| 96 | || lower.starts_with("data:image/gif") |
| 97 | || lower.starts_with("data:image/webp") |
| 98 | { |
| 99 | return Some(src.trim()); |
| 100 | } |
| 101 | safe_href(src) |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | pub(super) fn render_blocks(blocks: &[Block]) -> String { |
| 110 | let mut out = String::new(); |
| 111 | for block in blocks { |
| 112 | if let Some(html) = render_block(block) { |
| 113 | out.push_str(&html); |
| 114 | out.push('\n'); |
| 115 | } |
| 116 | } |
| 117 | out |
| 118 | } |
| 119 | |
| 120 | fn render_block(block: &Block) -> Option<String> { |
| 121 | let html = match block { |
| 122 | Block::Paragraph(inlines) => { |
| 123 | let inner = render_inlines(inlines); |
| 124 | if inner.trim().is_empty() { |
| 125 | return None; |
| 126 | } |
| 127 | format!("<p>{}</p>", inner.trim()) |
| 128 | } |
| 129 | Block::Heading(level, inlines) => { |
| 130 | let level = (*level).clamp(1, 6); |
| 131 | let inner = render_inlines(inlines).replace('\n', " "); |
| 132 | let inner = inner.trim(); |
| 133 | if inner.is_empty() { |
| 134 | return None; |
| 135 | } |
| 136 | format!("<h{level}>{inner}</h{level}>") |
| 137 | } |
| 138 | Block::Quote(blocks) => { |
| 139 | let inner = render_blocks(blocks); |
| 140 | if inner.trim().is_empty() { |
| 141 | return None; |
| 142 | } |
| 143 | format!("<blockquote>\n{inner}</blockquote>") |
| 144 | } |
| 145 | Block::Code { lang, text } => code_block(lang.as_deref(), text), |
| 146 | Block::Rule => "<hr>".to_string(), |
| 147 | Block::List { |
| 148 | ordered, |
| 149 | start, |
| 150 | items, |
| 151 | } => render_list(*ordered, *start, items)?, |
| 152 | }; |
| 153 | if html.trim().is_empty() { |
| 154 | None |
| 155 | } else { |
| 156 | Some(html) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | fn render_list(ordered: bool, start: i64, items: &[Vec<Block>]) -> Option<String> { |
| 161 | let mut rendered = String::new(); |
| 162 | for item in items { |
| 163 | rendered.push_str(&format!("<li>{}</li>\n", render_blocks(item).trim_end())); |
| 164 | } |
| 165 | if rendered.is_empty() { |
| 166 | return None; |
| 167 | } |
| 168 | if ordered { |
| 169 | let start_attr = if start == 1 { |
| 170 | String::new() |
| 171 | } else { |
| 172 | format!(" start=\"{start}\"") |
| 173 | }; |
| 174 | Some(format!("<ol{start_attr}>\n{rendered}</ol>")) |
| 175 | } else { |
| 176 | Some(format!("<ul>\n{rendered}</ul>")) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | fn code_block(lang: Option<&str>, text: &str) -> String { |
| 181 | let content = text.trim_end_matches('\n'); |
| 182 | |
| 183 | let lang = lang |
| 184 | .filter(|l| !l.trim().is_empty()) |
| 185 | .map(str::to_owned) |
| 186 | .or_else(|| note_model::lang::detect(content).map(str::to_owned)) |
| 187 | .unwrap_or_default(); |
| 188 | let class = if lang.trim().is_empty() { |
| 189 | String::new() |
| 190 | } else { |
| 191 | format!(" class=\"language-{}\"", esc(lang.trim())) |
| 192 | }; |
| 193 | format!("<pre><code{class}>{}</code></pre>", esc(content)) |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | fn render_inlines(inlines: &[Inline]) -> String { |
| 202 | let mut out = String::new(); |
| 203 | for inline in inlines { |
| 204 | match inline { |
| 205 | Inline::Text(s, m) => out.push_str(&render_run(s, m)), |
| 206 | Inline::HardBreak => out.push_str("<br>"), |
| 207 | Inline::Image { src, alt, title } => { |
| 208 | out.push_str(&image_html(src, alt, title.as_deref())); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | out |
| 213 | } |
| 214 | |
| 215 | fn image_html(src: &str, alt: &str, title: Option<&str>) -> String { |
| 216 | let Some(safe) = safe_src(src) else { |
| 217 | return esc(alt); |
| 218 | }; |
| 219 | let title = title |
| 220 | .filter(|t| !t.is_empty()) |
| 221 | .map(|t| format!(" title=\"{}\"", esc(t))) |
| 222 | .unwrap_or_default(); |
| 223 | let (src, alt) = (esc(safe), esc(alt)); |
| 224 | match embed_of(safe) { |
| 225 | Some(Embed::Audio) => format!("<audio controls src=\"{src}\" aria-label=\"{alt}\"{title}></audio>"), |
| 226 | Some(Embed::Video) => format!("<video controls src=\"{src}\" aria-label=\"{alt}\"{title}></video>"), |
| 227 | _ => format!("<img src=\"{src}\" alt=\"{alt}\"{title}>"), |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | fn render_run(s: &str, m: &Marks) -> String { |
| 232 | let core = s.trim(); |
| 233 | if core.is_empty() { |
| 234 | return esc(s); |
| 235 | } |
| 236 | |
| 237 | let lead_len = s.len() - s.trim_start().len(); |
| 238 | let lead = &s[..lead_len]; |
| 239 | let trail = &s[lead_len + core.len()..]; |
| 240 | |
| 241 | let mut out = if m.code { |
| 242 | format!("<code>{}</code>", esc(core)) |
| 243 | } else { |
| 244 | esc(core) |
| 245 | }; |
| 246 | if m.em { |
| 247 | out = format!("<em>{out}</em>"); |
| 248 | } |
| 249 | if m.strong { |
| 250 | out = format!("<strong>{out}</strong>"); |
| 251 | } |
| 252 | if let Some((href, title)) = &m.link { |
| 253 | if let Some(href) = safe_href(href) { |
| 254 | let title = title |
| 255 | .as_deref() |
| 256 | .map(|t| format!(" title=\"{}\"", esc(t))) |
| 257 | .unwrap_or_default(); |
| 258 | out = format!("<a href=\"{}\"{title}>{out}</a>", esc(href)); |
| 259 | } |
| 260 | } |
| 261 | format!("{}{out}{}", esc(lead), esc(trail)) |
| 262 | } |