feat(math): add katex-rs math rendering module

Add src/math.rs with render_math() wrapper around katex-rs for
build-time LaTeX to HTML conversion. Wire module into main.rs.

- Use KatexContext::default() for standard LaTeX function registry
- Settings: display_mode toggle, throw_on_error=false for graceful
  degradation
- Include unit tests for inline/display math rendering
This commit is contained in:
Timothy DeHerrera
2026-01-27 00:29:03 -07:00
parent a7338f5418
commit ebe1fd3b6e
4 changed files with 266 additions and 12 deletions

View File

@@ -8,6 +8,7 @@ mod css;
mod error;
mod feed;
mod highlight;
mod math;
mod render;
mod templates;

48
src/math.rs Normal file
View File

@@ -0,0 +1,48 @@
//! Math rendering via katex-rs.
//!
//! Converts LaTeX math expressions to HTML at build-time.
use katex::{render_to_string, KatexContext, Settings};
/// Render a LaTeX math expression to HTML.
///
/// # Arguments
/// * `latex` - The LaTeX source string
/// * `display_mode` - `true` for block equations, `false` for inline
///
/// # Returns
/// The rendered HTML string, or an error message on failure.
pub fn render_math(latex: &str, display_mode: bool) -> Result<String, String> {
let ctx = KatexContext::default();
let settings = Settings::builder()
.display_mode(display_mode)
.throw_on_error(false)
.build();
render_to_string(&ctx, latex, &settings).map_err(|e| e.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_inline_math() {
let result = render_math("x^2", false).unwrap();
assert!(result.contains("<span"));
}
#[test]
fn test_display_math() {
let result = render_math(r"\sum_{i=1}^n i", true).unwrap();
assert!(result.contains("<span"));
}
#[test]
fn test_invalid_latex_no_panic() {
// Should not panic, returns error or graceful fallback
let result = render_math(r"\invalidcommand", false);
// katex-rs with throw_on_error=false returns error markup
assert!(result.is_ok() || result.is_err());
}
}