Basic cours support #44
2 changed files with 30 additions and 12 deletions
|
@ -1,10 +1,12 @@
|
|||
use crate::misc::date::Date;
|
||||
use comrak::nodes::{AstNode, NodeValue};
|
||||
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 serde::{Deserialize, Deserializer};
|
||||
use std::fs;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Metadata for blog posts
|
||||
#[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 {
|
||||
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
|
||||
|
|
|
@ -16,13 +16,13 @@ window.addEventListener("load", () => {
|
|||
macros[`\\${item[0]}`] = `\\${item[1]}`;
|
||||
}
|
||||
|
||||
renderMathInElement(document.body, {
|
||||
delimiters: [
|
||||
{ left: "$$", right: "$$", display: true },
|
||||
{ left: "$", right: "$", display: false },
|
||||
],
|
||||
const attribute = "data-math-style";
|
||||
for (let element of document.querySelectorAll(`span[${attribute}]`)) {
|
||||
katex.render(element.textContent, element, {
|
||||
throwOnError: false,
|
||||
displayMode: element.getAttribute(attribute) === "display",
|
||||
macros,
|
||||
output: "mathml",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue