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
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-12-10 23:23:27 +01:00
parent 2cb1e664fe
commit ecbbe85844
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 43 additions and 14 deletions

1
Cargo.lock generated
View file

@ -927,6 +927,7 @@ dependencies = [
"minify-html", "minify-html",
"minify-js 0.5.6", "minify-js 0.5.6",
"ramhorns", "ramhorns",
"regex",
"reqwest", "reqwest",
"rss", "rss",
"serde", "serde",

View file

@ -27,3 +27,4 @@ chrono = { version = "0.4.30", default-features = false, features = ["clock"]}
chrono-tz = "0.8" chrono-tz = "0.8"
rss = { version = "2.0", features = ["atom"] } rss = { version = "2.0", features = ["atom"] }
lol_html = "1.2" lol_html = "1.2"
regex = "1.10"

View file

@ -3,6 +3,7 @@ use comrak::nodes::{AstNode, NodeValue};
use comrak::{format_html, parse_document, Arena, ComrakOptions, ListStyleType}; use comrak::{format_html, parse_document, Arena, ComrakOptions, ListStyleType};
use lol_html::{element, rewrite_str, RewriteStrSettings}; use lol_html::{element, rewrite_str, RewriteStrSettings};
use ramhorns::Content; use ramhorns::Content;
use regex::{Captures, Regex};
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
use std::fs; use std::fs;
@ -170,12 +171,35 @@ fn custom_img_size(html: String) -> String {
.unwrap() .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!("<span data-katex=\"{}\"></span>", m.as_str()))
})
.collect::<Vec<String>>()
.join("")
})
.to_string()
}
/// Transform markdown string to File structure /// Transform markdown string to File structure
fn read(raw_text: &str, metadata_type: TypeFileMetadata) -> File { fn read(raw_text: &str, metadata_type: TypeFileMetadata) -> File {
let arena = Arena::new(); 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 options = get_options();
let root = parse_document(&arena, raw_text, &options); let root = parse_document(&arena, &text, &options);
// Find metadata // Find metadata
let metadata = get_metadata(root, metadata_type); 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(); format_html(root, &options, &mut html).unwrap();
let mut html_content = String::from_utf8(html).unwrap(); let mut html_content = String::from_utf8(html).unwrap();
/* println!("{}", html_content); */
html_content = custom_img_size(html_content); html_content = custom_img_size(html_content);

View file

@ -16,12 +16,20 @@ window.addEventListener("load", () => {
macros[`\\${item[0]}`] = `\\${item[1]}`; macros[`\\${item[0]}`] = `\\${item[1]}`;
} }
renderMathInElement(document.body, { document.querySelectorAll("span[data-katex]").forEach((element) => {
delimiters: [ const rawLaTeXFormula = element.getAttribute("data-katex");
{ left: "$$", right: "$$", display: true }, const displayMode = rawLaTeXFormula.startsWith("$$");
{ left: "$", right: "$", display: false }, const strip = displayMode ? 2 : 1;
],
katex.render(
rawLaTeXFormula.slice(strip, rawLaTeXFormula.length - strip),
element,
{
throwOnError: false, throwOnError: false,
macros, macros,
displayMode,
output: "mathml",
}
);
}); });
}); });

View file

@ -4,10 +4,4 @@
integrity="sha384-j/ZricySXBnNMJy9meJCtyXTKMhIJ42heyr7oAdxTDBy/CYA9hzpMo+YTNV5C+1X" integrity="sha384-j/ZricySXBnNMJy9meJCtyXTKMhIJ42heyr7oAdxTDBy/CYA9hzpMo+YTNV5C+1X"
crossorigin="anonymous" crossorigin="anonymous"
></script> ></script>
<script
defer
src="//cdn.jsdelivr.net/npm/katex@0.16.6/dist/contrib/auto-render.min.js"
integrity="sha384-+VBxd3r6XgURycqtZ117nYw44OOcIax56Z4dCRWbxyPt0Koah1uHoK0o4+/RRE05"
crossorigin="anonymous"
></script>
<script src="/js/libs/katex.js"></script> <script src="/js/libs/katex.js"></script>