From 9b9065eb1cb857cb90d3e6025786bc3ab43295bd Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 21 Apr 2023 22:31:20 +0200 Subject: [PATCH] replace latex with mathml --- src/misc/markdown.rs | 48 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/misc/markdown.rs b/src/misc/markdown.rs index 0e84049..439b6cb 100644 --- a/src/misc/markdown.rs +++ b/src/misc/markdown.rs @@ -52,9 +52,9 @@ pub fn get_options() -> ComrakOptions { hardbreaks: false, // could be true? change by metadata could be good for compatibility github_pre_lang: false, full_info_string: true, - width: 0, // 0 mean disabled? - unsafe_: true, - escape: false, // may change in the future? + width: 0, // 0 mean disabled? + unsafe_: false, // could be true? change by metadata could be good for compatibility + escape: false, // may change in the future? list_style: ListStyleType::Dash, sourcepos: false, }, @@ -64,11 +64,11 @@ pub fn get_options() -> ComrakOptions { pub fn read(raw_text: &str) -> File { let arena = Arena::new(); - // LaTeX conversion to MathML - let text = latex2mathml::replace(raw_text).unwrap_or_default(); - let options = get_options(); - let root = parse_document(&arena, &text, &options); + let root = parse_document(&arena, raw_text, &options); + + // LaTeX conversion to MathML + latex_replace(root); // Find metadata let metadata = get_metadata(root); @@ -135,3 +135,37 @@ fn check_code<'a>(root: &'a AstNode<'a>, blacklist: &[String]) -> bool { _ => false, }) } + +/// Call the `latex2mathml::replace` function when LaTeX formula can appears +/// without breaking things +fn latex_replace<'a>(root: &'a AstNode<'a>) { + root.children() + .for_each(|node| match &mut node.data.borrow_mut().value { + // Detect in blocks + NodeValue::Paragraph + | NodeValue::BlockQuote + | NodeValue::TableCell + + // Detect in inlines + | NodeValue::Emph + | NodeValue::Strong + | NodeValue::Strikethrough + | NodeValue::Superscript => { + latex_replace(node) + } + + // Contains text + NodeValue::Text(ref mut text) => { + *text = latex2mathml::replace(text).unwrap_or(text.to_owned()) + }, + NodeValue::Link(ref mut link) | NodeValue::Image(ref mut link) => { + let mut clone = link.clone(); + clone.title = latex2mathml::replace(&link.title).unwrap_or(clone.title); + + *link = clone; + }, + + // Everything not captured can't contains math + _ => (), + }); +}