From 69149681f7de931eb2fc23b4f1ef6a235489f77c Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 21 Apr 2023 18:58:36 +0200 Subject: [PATCH] pass mermaid/syntax metadata --- src/misc/markdown.rs | 48 ++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/src/misc/markdown.rs b/src/misc/markdown.rs index f88b5f5..4a67efa 100644 --- a/src/misc/markdown.rs +++ b/src/misc/markdown.rs @@ -75,12 +75,13 @@ pub fn read(raw_text: &str) -> File { let mut html = vec![]; format_html(root, &options, &mut html).unwrap(); + let mermaid_name = "mermaid"; + File { metadata: Metadata { info: metadata, - math: false, - mermaid: false, - syntax_highlight: false, + mermaid: check_mermaid(root, mermaid_name.to_string()), + syntax_highlight: check_code(root, &[mermaid_name.to_string()]), }, content: String::from_utf8(html).unwrap(), } @@ -110,40 +111,25 @@ pub fn get_metadata<'a>(root: &'a AstNode<'a>) -> FileMetadata { } } -/* /// Check recursively if maths is in the AST -fn check_math(vec: &[Node]) -> bool { - vec.iter().any(|x| { - matches!(x, Node::Math(_) | Node::InlineMath(_)) - || match x.children() { - Some(children) => check_math(children), - None => false, - } - }) -} - /// Recursively check whether mermaid diagrams are in the AST -fn check_mermaid(vec: &[Node], mermaid_str: String) -> bool { - vec.iter().any(|x| match x { - Node::Code(code) => code.lang == Some(mermaid_str.clone()), +fn check_mermaid<'a>(root: &'a AstNode<'a>, mermaid_str: String) -> bool { + root.children().any(|node| match &node.data.borrow().value { + // Check if code of block define a mermaid diagram + NodeValue::CodeBlock(code_block) => code_block.info == mermaid_str, _ => false, - } || match x.children() { - Some(children) => check_mermaid(children, mermaid_str.clone()), - None => false }) } /// Recursively check if code is in the AST -fn check_code(vec: &[Node], blacklist: Vec) -> bool { - vec.iter().any(|x| match x { - Node::InlineCode(_) => true, - Node::Code(code) => match &code.lang { - Some(lang) => !blacklist.contains(lang), - None => true, - }, +fn check_code<'a>(root: &'a AstNode<'a>, blacklist: &[String]) -> bool { + root.children().any(|node| match &node.data.borrow().value { + // Detect code in paragraph + /* NodeValue::Paragraph => match &node.children().next() { + Some(child) => matches!(child.data.borrow().value, NodeValue::Code(_)), + None => false, + }, */ + // Detect blocks of code where the lang isn't in the blacklist + NodeValue::CodeBlock(code_block) => !blacklist.contains(&code_block.info), _ => false, - } || match x.children() { - Some(children) => check_code(children, blacklist.clone()), - None => false }) } - */