From ecbbe8584406967725c69a6c5b830b1ccb62aad4 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 10 Dec 2023 23:23:27 +0100 Subject: [PATCH] closest i can get with stupid regex, currently not working since the regex is eating a character after a match, also its not a proprer parsing so any dollar inside a formula will probably break the whole thing --- Cargo.lock | 1 + Cargo.toml | 1 + src/misc/markdown.rs | 27 ++++++++++++++++++++++++++- static/js/libs/katex.js | 22 +++++++++++++++------- templates/libs/katex_footer.html | 6 ------ 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c69c76b..1da51cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -927,6 +927,7 @@ dependencies = [ "minify-html", "minify-js 0.5.6", "ramhorns", + "regex", "reqwest", "rss", "serde", diff --git a/Cargo.toml b/Cargo.toml index ae424b5..afc8f36 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,3 +27,4 @@ chrono = { version = "0.4.30", default-features = false, features = ["clock"]} chrono-tz = "0.8" rss = { version = "2.0", features = ["atom"] } lol_html = "1.2" +regex = "1.10" diff --git a/src/misc/markdown.rs b/src/misc/markdown.rs index bba0e42..9ad3d8f 100644 --- a/src/misc/markdown.rs +++ b/src/misc/markdown.rs @@ -3,6 +3,7 @@ use comrak::nodes::{AstNode, NodeValue}; use comrak::{format_html, parse_document, Arena, ComrakOptions, ListStyleType}; use lol_html::{element, rewrite_str, RewriteStrSettings}; use ramhorns::Content; +use regex::{Captures, Regex}; use serde::{Deserialize, Deserializer}; use std::fs; @@ -170,12 +171,35 @@ fn custom_img_size(html: String) -> String { .unwrap() } +fn math_processing(regex: &str, source: &str) -> String { + Regex::new(regex) + .unwrap() + .replace_all(source, |captures: &Captures| { + captures + .iter() + .skip(1) + .filter_map(|capture| { + capture.map(|m| format!("", m.as_str())) + }) + .collect::>() + .join("") + }) + .to_string() +} + /// Transform markdown string to File structure fn read(raw_text: &str, metadata_type: TypeFileMetadata) -> File { let arena = Arena::new(); + // Transform LaTeX formulas + let text = math_processing( + r"(?U)(\$[^\$|\n]+\$)[^\$]", + &math_processing(r"(?U)(\$\$[^\$|\n]+\$\$)", raw_text), + ); + println!("{}", text); + let options = get_options(); - let root = parse_document(&arena, raw_text, &options); + let root = parse_document(&arena, &text, &options); // Find metadata let metadata = get_metadata(root, metadata_type); @@ -188,6 +212,7 @@ fn read(raw_text: &str, metadata_type: TypeFileMetadata) -> File { format_html(root, &options, &mut html).unwrap(); let mut html_content = String::from_utf8(html).unwrap(); + /* println!("{}", html_content); */ html_content = custom_img_size(html_content); diff --git a/static/js/libs/katex.js b/static/js/libs/katex.js index 9964bba..8436a0e 100644 --- a/static/js/libs/katex.js +++ b/static/js/libs/katex.js @@ -16,12 +16,20 @@ window.addEventListener("load", () => { macros[`\\${item[0]}`] = `\\${item[1]}`; } - renderMathInElement(document.body, { - delimiters: [ - { left: "$$", right: "$$", display: true }, - { left: "$", right: "$", display: false }, - ], - throwOnError: false, - macros, + document.querySelectorAll("span[data-katex]").forEach((element) => { + const rawLaTeXFormula = element.getAttribute("data-katex"); + const displayMode = rawLaTeXFormula.startsWith("$$"); + const strip = displayMode ? 2 : 1; + + katex.render( + rawLaTeXFormula.slice(strip, rawLaTeXFormula.length - strip), + element, + { + throwOnError: false, + macros, + displayMode, + output: "mathml", + } + ); }); }); diff --git a/templates/libs/katex_footer.html b/templates/libs/katex_footer.html index 2b2a92b..abd7a9d 100644 --- a/templates/libs/katex_footer.html +++ b/templates/libs/katex_footer.html @@ -4,10 +4,4 @@ integrity="sha384-j/ZricySXBnNMJy9meJCtyXTKMhIJ42heyr7oAdxTDBy/CYA9hzpMo+YTNV5C+1X" crossorigin="anonymous" > -