feat(render): integrate katex-rs math rendering
Enable ENABLE_MATH in pulldown-cmark options and implement InlineMath/DisplayMath event handlers in the render pipeline. - Inline math ($...$) renders via render_math(_, false) - Display math ($$...$$) wrapped in <div class="math-display"> - Graceful degradation: errors logged to stderr, raw LaTeX shown with class="math-error" for styling
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
//! Markdown to HTML rendering via pulldown-cmark with syntax highlighting.
|
//! Markdown to HTML rendering via pulldown-cmark with syntax highlighting.
|
||||||
|
|
||||||
use crate::highlight::{highlight_code, Language};
|
use crate::highlight::{Language, highlight_code};
|
||||||
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag, TagEnd};
|
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag, TagEnd};
|
||||||
|
|
||||||
/// Render markdown content to HTML with syntax highlighting.
|
/// Render markdown content to HTML with syntax highlighting.
|
||||||
@@ -8,7 +8,8 @@ pub fn markdown_to_html(markdown: &str) -> String {
|
|||||||
let options = Options::ENABLE_TABLES
|
let options = Options::ENABLE_TABLES
|
||||||
| Options::ENABLE_FOOTNOTES
|
| Options::ENABLE_FOOTNOTES
|
||||||
| Options::ENABLE_STRIKETHROUGH
|
| Options::ENABLE_STRIKETHROUGH
|
||||||
| Options::ENABLE_TASKLISTS;
|
| Options::ENABLE_TASKLISTS
|
||||||
|
| Options::ENABLE_MATH;
|
||||||
|
|
||||||
let parser = Parser::new_ext(markdown, options);
|
let parser = Parser::new_ext(markdown, options);
|
||||||
let mut html_output = String::new();
|
let mut html_output = String::new();
|
||||||
@@ -107,9 +108,28 @@ pub fn markdown_to_html(markdown: &str) -> String {
|
|||||||
};
|
};
|
||||||
html_output.push_str(checkbox);
|
html_output.push_str(checkbox);
|
||||||
}
|
}
|
||||||
Event::InlineMath(_) | Event::DisplayMath(_) => {
|
Event::InlineMath(latex) => match crate::math::render_math(&latex, false) {
|
||||||
// Future: KaTeX integration
|
Ok(rendered) => html_output.push_str(&rendered),
|
||||||
}
|
Err(e) => {
|
||||||
|
eprintln!("math render error: {e}");
|
||||||
|
html_output.push_str("<code class=\"math-error\">");
|
||||||
|
html_output.push_str(&html_escape(&latex));
|
||||||
|
html_output.push_str("</code>");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Event::DisplayMath(latex) => match crate::math::render_math(&latex, true) {
|
||||||
|
Ok(rendered) => {
|
||||||
|
html_output.push_str("<div class=\"math-display\">\n");
|
||||||
|
html_output.push_str(&rendered);
|
||||||
|
html_output.push_str("\n</div>\n");
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("math render error: {e}");
|
||||||
|
html_output.push_str("<pre class=\"math-error\">");
|
||||||
|
html_output.push_str(&html_escape(&latex));
|
||||||
|
html_output.push_str("</pre>\n");
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user