* Doesnt convert in Rust the latex
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

* add math detection
* fix mermaid and code detection
This commit is contained in:
Mylloon 2023-04-19 18:36:52 +02:00
parent e65f106303
commit d8a1693dd7
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 55 additions and 25 deletions

7
Cargo.lock generated
View file

@ -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"

View file

@ -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"] }

View file

@ -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<File> {
}
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<String>) -> 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
})
}