diff --git a/Cargo.lock b/Cargo.lock index 7489246..fe6508e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -463,6 +463,8 @@ dependencies = [ "actix-files", "actix-web", "glob", + "latex2mathml", + "markdown", "minify-html", "ramhorns", "serde", @@ -664,6 +666,12 @@ 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" @@ -736,6 +744,15 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "markdown" +version = "1.0.0-alpha.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98de49c677e95e00eaa74c42a0b07ea55e1e0b1ebca5b2cbc7657f288cd714eb" +dependencies = [ + "unicode-id", +] + [[package]] name = "memchr" version = "2.5.0" @@ -1342,6 +1359,12 @@ version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" +[[package]] +name = "unicode-id" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d70b6494226b36008c8366c288d77190b3fad2eb4c10533139c1c1f461127f1a" + [[package]] name = "unicode-ident" version = "1.0.8" diff --git a/Cargo.toml b/Cargo.toml index 8ab6a74..8632d66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,5 @@ toml = "0.7.3" serde = { version = "1.0.159", features = ["derive"] } minify-html = "0.10.8" glob = "0.3.1" +markdown = "1.0.0-alpha.7" +latex2mathml = "0.2.3" diff --git a/src/template.rs b/src/template.rs index 1c8e015..9f30c98 100644 --- a/src/template.rs +++ b/src/template.rs @@ -13,3 +13,62 @@ impl Template { tplt.render(&data) } } + +enum FrontMatter<'a> { + Yaml(&'a str), + Toml(&'a str), + Json(&'a str), +} + +impl FrontMatter<'_> { + fn parse(&self) -> String { + match self { + Self::Yaml(_val) => todo!(), + Self::Toml(_val) => todo!(), + Self::Json(_val) => todo!(), + } + } +} + +pub fn read_md(filename: &str) -> String { + // Read markdown file + let mut text = std::fs::read_to_string(filename).unwrap(); + + // Transform LaTeX to MathML + text = latex2mathml::replace(&text).unwrap(); + + let parse_option = markdown::ParseOptions { + constructs: markdown::Constructs { + frontmatter: true, + ..markdown::Constructs::gfm() + }, + ..markdown::ParseOptions::gfm() + }; + + let compile_option = markdown::CompileOptions { + allow_dangerous_html: true, + gfm_footnote_label: Some("Notes de bas de page".into()), + gfm_footnote_back_label: Some("Arrière".into()), + ..markdown::CompileOptions::gfm() + }; + + let md_tree = markdown::to_mdast(&text, &parse_option).unwrap(); + let frontmatter = match &md_tree.children().unwrap()[0] { + markdown::mdast::Node::Yaml(v) => Some(FrontMatter::Yaml(&v.value).parse()), + markdown::mdast::Node::Toml(v) => Some(FrontMatter::Toml(&v.value).parse()), + markdown::mdast::Node::MdxjsEsm(v) => Some(FrontMatter::Json(&v.value).parse()), + _ => None, + }; + println!("{:#?}\nVS", &md_tree.children().unwrap()[0]); + println!("{:#?}", frontmatter); + + // Convert to HTML + markdown::to_html_with_options( + &text, + &markdown::Options { + parse: parse_option, + compile: compile_option, + }, + ) + .unwrap() +}