From d8a1693dd7356ccbf56992255d5fc170042f8854 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 19 Apr 2023 18:36:52 +0200 Subject: [PATCH] * Doesnt convert in Rust the latex * add math detection * fix mermaid and code detection --- Cargo.lock | 7 ----- Cargo.toml | 1 - src/template.rs | 72 +++++++++++++++++++++++++++++++++++++------------ 3 files changed, 55 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 11e848f..3ed4cf5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -597,7 +597,6 @@ dependencies = [ "actix-web", "cached", "glob", - "latex2mathml", "markdown", "minify-html", "ramhorns", @@ -958,12 +957,6 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388" -[[package]] -name = "latex2mathml" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "678cf5bdb3ba63a264e6e0c9eee36538ca1d2da0afa4dd801c1f96309e710765" - [[package]] name = "lazy_static" version = "1.4.0" diff --git a/Cargo.toml b/Cargo.toml index 1be210d..e9612eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,5 +20,4 @@ serde_yaml = "0.9" minify-html = "0.10.8" glob = "0.3.1" markdown = "1.0.0-alpha.7" -latex2mathml = "0.2.3" reqwest = { version = "0.11", features = ["json"] } diff --git a/src/template.rs b/src/template.rs index bd7b560..7c06d9e 100644 --- a/src/template.rs +++ b/src/template.rs @@ -1,3 +1,4 @@ +use markdown::mdast::Node; use ramhorns::{Content, Ramhorns}; use serde::Deserialize; use std::fs; @@ -61,6 +62,7 @@ pub struct FileMetadata { #[derive(Content)] pub struct Metadata { pub info: FileMetadata, + pub math: bool, pub mermaid: bool, pub syntax_highlight: bool, } @@ -80,12 +82,11 @@ pub fn read_md_file(filename: &str) -> Option { } pub fn read_md(raw_text: &str) -> File { - // Transform LaTeX to MathML - let text = latex2mathml::replace(raw_text).unwrap_or(raw_text.to_string()); - let parse_option = markdown::ParseOptions { constructs: markdown::Constructs { frontmatter: true, + math_text: true, + math_flow: true, ..markdown::Constructs::gfm() }, ..markdown::ParseOptions::gfm() @@ -96,40 +97,39 @@ pub fn read_md(raw_text: &str) -> File { ..markdown::CompileOptions::gfm() }; - let md_tree = markdown::to_mdast(&text, &parse_option).unwrap(); + let md_tree = markdown::to_mdast(raw_text, &parse_option).unwrap(); let md_nodes = md_tree.children().unwrap(); let metadata; let presence_mermaid; + let presence_math; let presence_code; if md_nodes.is_empty() { metadata = FileMetadata::default(); presence_mermaid = false; + presence_math = false; presence_code = false; } else { metadata = match &md_nodes[0] { - markdown::mdast::Node::Yaml(v) => FrontMatter::Yaml(&v.value).parse(), - markdown::mdast::Node::Toml(v) => FrontMatter::Toml(&v.value).parse(), - markdown::mdast::Node::MdxjsEsm(v) => FrontMatter::Json(&v.value).parse(), + Node::Yaml(v) => FrontMatter::Yaml(&v.value).parse(), + Node::Toml(v) => FrontMatter::Toml(&v.value).parse(), + Node::MdxjsEsm(v) => FrontMatter::Json(&v.value).parse(), _ => FileMetadata::default(), }; + // Find if document contains maths + presence_math = check_math(md_nodes); + // Find if document contains mermaid diagram - let mermaid = Some(String::from("mermaid")); - presence_mermaid = md_nodes.iter().any(|x| match x { - markdown::mdast::Node::Code(code) => code.lang == mermaid, - _ => false, - }); + let mermaid = String::from("mermaid"); + presence_mermaid = check_mermaid(md_nodes, mermaid.clone()); // Find if document contains code to highlight - presence_code = md_nodes.iter().any(|x| match x { - markdown::mdast::Node::Code(code) => code.lang != mermaid, - _ => false, - }); + presence_code = check_code(md_nodes, vec![mermaid]); } // Convert to HTML let html = markdown::to_html_with_options( - &text, + raw_text, &markdown::Options { parse: parse_option, compile: compile_option, @@ -140,9 +140,47 @@ pub fn read_md(raw_text: &str) -> File { File { metadata: Metadata { info: metadata, + math: presence_math, mermaid: presence_mermaid, syntax_highlight: presence_code, }, content: html, } } + +/// Check recursively if maths is in the AST +fn check_math(vec: &[Node]) -> bool { + vec.iter().any(|x| { + matches!(x, Node::Math(_) | Node::InlineMath(_)) + || match x.children() { + Some(children) => check_math(children), + None => false, + } + }) +} + +/// Recursively check whether mermaid diagrams are in the AST +fn check_mermaid(vec: &[Node], mermaid_str: String) -> bool { + vec.iter().any(|x| match x { + Node::Code(code) => code.lang == Some(mermaid_str.clone()), + _ => false, + } || match x.children() { + Some(children) => check_mermaid(children, mermaid_str.clone()), + None => false + }) +} + +/// Recursively check if code is in the AST +fn check_code(vec: &[Node], blacklist: Vec) -> bool { + vec.iter().any(|x| match x { + Node::InlineCode(_) => true, + Node::Code(code) => match &code.lang { + Some(lang) => !blacklist.contains(lang), + None => true, + }, + _ => false, + } || match x.children() { + Some(children) => check_code(children, blacklist.clone()), + None => false + }) +}