fix math integration
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending approval

This commit is contained in:
Mylloon 2024-03-31 18:05:31 +02:00
parent 7d71b600e4
commit 8583520baf
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 30 additions and 12 deletions

View file

@ -1,10 +1,12 @@
use crate::misc::date::Date; use crate::misc::date::Date;
use comrak::nodes::{AstNode, NodeValue}; 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, HtmlRewriter, RewriteStrSettings, Settings};
use ramhorns::Content; use ramhorns::Content;
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
use std::fs; use std::fs;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
/// Metadata for blog posts /// Metadata for blog posts
#[derive(Content, Debug, Default, Deserialize)] #[derive(Content, Debug, Default, Deserialize)]
@ -322,9 +324,25 @@ fn check_code<'a>(root: &'a AstNode<'a>, blacklist: &[String]) -> bool {
}) })
} }
/// Check if html can contains maths /// Check if html contains maths
fn check_math(html: &str) -> bool { fn check_math(html: &str) -> bool {
html.contains('$') let math_detected = Arc::new(AtomicBool::new(false));
let mut output = vec![];
let _ = HtmlRewriter::new(
Settings {
element_content_handlers: vec![element!("span[data-math-style]", |_| {
math_detected.store(true, Ordering::SeqCst);
Ok(())
})],
..Settings::default()
},
|c: &[u8]| output.extend_from_slice(c),
)
.write(html.as_bytes());
math_detected.load(Ordering::SeqCst)
} }
/// Change class of languages for hljs detection /// Change class of languages for hljs detection

View file

@ -16,13 +16,13 @@ window.addEventListener("load", () => {
macros[`\\${item[0]}`] = `\\${item[1]}`; macros[`\\${item[0]}`] = `\\${item[1]}`;
} }
renderMathInElement(document.body, { const attribute = "data-math-style";
delimiters: [ for (let element of document.querySelectorAll(`span[${attribute}]`)) {
{ left: "$$", right: "$$", display: true }, katex.render(element.textContent, element, {
{ left: "$", right: "$", display: false },
],
throwOnError: false, throwOnError: false,
displayMode: element.getAttribute(attribute) === "display",
macros, macros,
output: "mathml", output: "mathml",
}); });
}
}); });