1use comrak::nodes::NodeValue;
8use comrak::{parse_document, Arena, Options};
9use syntect::parsing::SyntaxSet;
10use syntect::util::LinesWithEndings;
11
12use crate::error::Result;
13
14pub struct Highlighter {
18 ss: SyntaxSet,
19 theme: syntect::highlighting::Theme,
20}
21
22impl Default for Highlighter {
23 fn default() -> Self {
24 Self::new()
25 }
26}
27
28impl Highlighter {
29 pub fn new() -> Self {
30 use syntect::highlighting::ThemeSet;
31 let mut ts = ThemeSet::load_defaults();
32 let theme = ts
34 .themes
35 .remove("InspiredGitHub")
36 .unwrap_or_else(|| ts.themes.into_values().next().unwrap_or_default());
37 Self {
38 ss: SyntaxSet::load_defaults_newlines(),
39 theme,
40 }
41 }
42
43 fn highlight_code(&self, code: &str, lang: &str) -> String {
47 use syntect::easy::HighlightLines;
48
49 let mapped_lang = match lang {
51 "ts" | "typescript" | "tsx" | "jsx" => "js",
52 "sh" | "shell" | "zsh" => "bash",
53 "yml" => "yaml",
54 "golang" => "go",
55 "csharp" => "cs",
56 "toml" => "ini", other => other,
58 };
59
60 let syntax = self
61 .ss
62 .find_syntax_by_token(mapped_lang)
63 .unwrap_or_else(|| self.ss.find_syntax_plain_text());
64
65 let mut hl = HighlightLines::new(syntax, &self.theme);
66
67 let mut out = String::with_capacity(code.len() * 2);
68 out.push_str("<code-block><pre class=\"code-block\"><code>");
69
70 for line in LinesWithEndings::from(code) {
71 let regions = hl.highlight_line(line, &self.ss).unwrap_or_default();
72 for &(style, text) in ®ions {
73 let cls = scope_to_css_class(style.foreground);
74 if cls.is_empty() {
75 emit_escaped(&mut out, text);
76 } else {
77 out.push_str("<span class=\"hl-");
78 out.push_str(cls);
79 out.push_str("\">");
80 emit_escaped(&mut out, text);
81 out.push_str("</span>");
82 }
83 }
84 }
85
86 out.push_str("</code></pre></code-block>");
87 out
88 }
89}
90
91fn emit_escaped(buf: &mut String, s: &str) {
92 let bytes = s.as_bytes();
96 let mut start = 0;
97 for (i, &b) in bytes.iter().enumerate() {
98 let esc = match b {
99 b'&' => "&",
100 b'<' => "<",
101 b'>' => ">",
102 b'{' => "{",
103 b'}' => "}",
104 _ => continue,
105 };
106 buf.push_str(&s[start..i]);
108 buf.push_str(esc);
109 start = i + 1;
110 }
111 buf.push_str(&s[start..]);
112}
113
114fn scope_to_css_class(fg: syntect::highlighting::Color) -> &'static str {
118 let hex = ((fg.r as u32) << 16) | ((fg.g as u32) << 8) | (fg.b as u32);
119 match hex {
120 0xa71d5d => "keyword",
122 0x183691 => "string",
124 0x969896 => "comment",
126 0xed6a43 => "number",
128 0x63a35c => "tag",
130 0x795da3 => "attr",
132 0x0086b3 => "property",
134 0x6f42c1 => "function",
136 0x333333 => "variable",
138 _ => "",
139 }
140}
141
142fn slugify(text: &str) -> String {
144 let mut slug = String::with_capacity(text.len());
145 for ch in text.chars() {
146 if ch.is_ascii_alphanumeric() {
147 slug.push(ch.to_ascii_lowercase());
148 } else if (ch == ' ' || ch == '-' || ch == '_') && !slug.ends_with('-') {
149 slug.push('-');
150 }
151 }
152 slug.trim_end_matches('-').to_string()
153}
154
155fn collect_node_text<'a>(
158 node: &'a comrak::arena_tree::Node<'a, std::cell::RefCell<comrak::nodes::Ast>>,
159) -> (String, String) {
160 let mut plain = String::new();
161 let mut html = String::new();
162 let mut stack: Vec<&comrak::arena_tree::Node<'a, std::cell::RefCell<comrak::nodes::Ast>>> =
163 Vec::new();
164 let mut child = node.last_child();
166 while let Some(c) = child {
167 stack.push(c);
168 child = c.previous_sibling();
169 }
170 while let Some(n) = stack.pop() {
171 let data = n.data.borrow();
172 match &data.value {
173 NodeValue::Text(t) => {
174 plain.push_str(t);
175 html.push_str(t);
176 }
177 NodeValue::Code(code) => {
178 plain.push_str(&code.literal);
179 html.push_str("<code>");
180 emit_escaped(&mut html, &code.literal);
183 html.push_str("</code>");
184 }
185 _ => {
186 drop(data);
187 let mut c = n.last_child();
188 while let Some(ch) = c {
189 stack.push(ch);
190 c = ch.previous_sibling();
191 }
192 }
193 }
194 }
195 (plain, html)
196}
197
198pub fn render_markdown(
201 content: &str,
202 highlighter: &Highlighter,
203 base_path: &str,
204) -> Result<String> {
205 let arena = Arena::new();
206 let mut options = Options::default();
207 options.extension.table = true;
208 options.extension.strikethrough = true;
209 options.extension.autolink = true;
210 options.render.r#unsafe = true; let root = parse_document(&arena, content, &options);
213
214 for node in root.descendants() {
219 let mut data = node.data.borrow_mut();
220 match &mut data.value {
221 NodeValue::CodeBlock(ref mut block) => {
222 let lang = if block.info.is_empty() {
223 "text"
224 } else {
225 block.info.split_whitespace().next().unwrap_or("text")
226 };
227 let highlighted = highlighter.highlight_code(&block.literal, lang);
228 data.value = NodeValue::HtmlInline(highlighted);
229 }
230 NodeValue::Code(ref code) => {
231 let mut html = String::with_capacity(code.literal.len() + 13);
234 html.push_str("<code>");
235 emit_escaped(&mut html, &code.literal);
236 html.push_str("</code>");
237 data.value = NodeValue::HtmlInline(html);
238 }
239 NodeValue::Link(ref mut link) => {
240 if link.url.starts_with('/') && !link.url.starts_with(base_path) && base_path != "/"
242 {
243 link.url = format!("{}{}", base_path.trim_end_matches('/'), &link.url);
244 }
245 }
246 _ => {}
247 }
248 }
249
250 let headings: Vec<_> = root
252 .descendants()
253 .filter(|node| matches!(node.data.borrow().value, NodeValue::Heading(_)))
254 .collect();
255
256 for node in headings {
257 let (level, plain_text, html_text) = {
258 let data = node.data.borrow();
259 if let NodeValue::Heading(ref h) = data.value {
260 let (plain, html) = collect_node_text(node);
261 (h.level, plain, html)
262 } else {
263 continue;
264 }
265 };
266 let slug = slugify(&plain_text);
267 let html = format!(
268 "<h{level} id=\"{slug}\">{html_text} <a class=\"header-anchor\" href=\"#{slug}\">#</a></h{level}>"
269 );
270 while let Some(child) = node.first_child() {
272 child.detach();
273 }
274 node.data.borrow_mut().value = NodeValue::HtmlInline(html);
275 }
276
277 let mut html = String::with_capacity(content.len());
278 comrak::format_html(root, &options, &mut html)
279 .map_err(|e| crate::error::Error::Markdown(e.to_string()))?;
280
281 Ok(html)
282}
283
284#[cfg(test)]
285mod tests {
286 use super::*;
287 #[test]
288 fn syntect_language_mapping() {
289 let ss = SyntaxSet::load_defaults_newlines();
290 for tok in &[
292 "js", "go", "python", "py", "cpp", "rust", "rs", "html", "css", "json", "bash", "yaml",
293 "cs",
294 ] {
295 assert!(
296 ss.find_syntax_by_token(tok).is_some(),
297 "syntect should support token '{tok}'"
298 );
299 }
300 for tok in &["ts", "typescript", "tsx", "jsx", "toml", "csharp", "golang"] {
302 assert!(
303 ss.find_syntax_by_token(tok).is_none(),
304 "'{tok}' should NOT be in syntect defaults (we alias it)"
305 );
306 }
307 }
308
309 #[test]
310 fn inline_code_escapes_template_braces() {
311 let h = Highlighter::new();
315 let html = render_markdown("Use `{{value}}` for escaped output.", &h, "/").unwrap();
316 assert!(
317 html.contains("{{value}}"),
318 "inline code braces should be escaped: {html}"
319 );
320 assert!(
321 !html.contains("{{value}}"),
322 "raw `{{{{value}}}}` must not survive: {html}"
323 );
324 }
325
326 #[test]
327 fn code_block_escapes_template_braces() {
328 let h = Highlighter::new();
331 let html = render_markdown("```html\n<p>{{name}}</p>\n```\n", &h, "/").unwrap();
332 assert!(
333 html.contains("{{name}}"),
334 "code block braces should be escaped: {html}"
335 );
336 }
337}