Basic cours support #44

Merged
Anri merged 67 commits from cours into main 2024-04-01 18:11:49 +02:00
2 changed files with 30 additions and 12 deletions
Showing only changes of commit 8583520baf - Show all commits

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",
}); });
}
}); });