reduce hljs warnings
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-04-24 13:12:21 +02:00
parent 468b5ecc70
commit 930d639da4
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 21 additions and 3 deletions

View file

@ -72,17 +72,19 @@ pub fn read(raw_text: &str) -> File {
// Find metadata // Find metadata
let metadata = get_metadata(root); let metadata = get_metadata(root);
let mermaid_name = "mermaid";
hljs_replace(root, mermaid_name);
// Convert to HTML // Convert to HTML
let mut html = vec![]; let mut html = vec![];
format_html(root, &options, &mut html).unwrap(); format_html(root, &options, &mut html).unwrap();
let mermaid_name = "mermaid";
let html_content = String::from_utf8(html).unwrap(); let html_content = String::from_utf8(html).unwrap();
File { File {
metadata: Metadata { metadata: Metadata {
info: metadata, info: metadata,
mermaid: check_mermaid(root, mermaid_name.to_owned()), mermaid: check_mermaid(root, mermaid_name),
syntax_highlight: check_code(root, &[mermaid_name.to_owned()]), syntax_highlight: check_code(root, &[mermaid_name.to_owned()]),
math: check_math(&html_content), math: check_math(&html_content),
}, },
@ -115,7 +117,7 @@ pub fn get_metadata<'a>(root: &'a AstNode<'a>) -> FileMetadata {
} }
/// Check whether mermaid diagrams are in the AST /// Check whether mermaid diagrams are in the AST
fn check_mermaid<'a>(root: &'a AstNode<'a>, mermaid_str: String) -> bool { fn check_mermaid<'a>(root: &'a AstNode<'a>, mermaid_str: &str) -> bool {
root.children().any(|node| match &node.data.borrow().value { root.children().any(|node| match &node.data.borrow().value {
// Check if code of block define a mermaid diagram // Check if code of block define a mermaid diagram
NodeValue::CodeBlock(code_block) => code_block.info == mermaid_str, NodeValue::CodeBlock(code_block) => code_block.info == mermaid_str,
@ -141,3 +143,14 @@ fn check_code<'a>(root: &'a AstNode<'a>, blacklist: &[String]) -> bool {
fn check_math(html: &str) -> bool { fn check_math(html: &str) -> bool {
html.contains('$') html.contains('$')
} }
/// Change class of languages for hljs detection
fn hljs_replace<'a>(root: &'a AstNode<'a>, mermaid_str: &str) {
root.children().for_each(|node| {
if let NodeValue::CodeBlock(ref mut block) = &mut node.data.borrow_mut().value {
if block.info != mermaid_str {
block.info = format!("hljs-{}", block.info);
}
}
});
}

View file

@ -1,6 +1,11 @@
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
<script src="//unpkg.com/highlightjs-copy/dist/highlightjs-copy.min.js"></script> <script src="//unpkg.com/highlightjs-copy/dist/highlightjs-copy.min.js"></script>
<script> <script>
hljs.configure({
noHighlightRe: /^$/i,
languageDetectRe: /\blanguage-hljs-([\w-]+)\b/i,
});
hljs.addPlugin(new CopyButtonPlugin()); hljs.addPlugin(new CopyButtonPlugin());
hljs.highlightAll(); hljs.highlightAll();
</script> </script>