From 9f7fd1313e0694f0328f54b917c57061a7f85748 Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Tue, 3 Oct 2023 11:10:22 +0200 Subject: [PATCH] upgrade comrak to 0.19.0 --- Cargo.lock | 77 +++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 2 +- src/web/markdown.rs | 62 ++++++++++++++++++------------------ 3 files changed, 108 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 70b0125f8..c1c6be5ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1052,10 +1052,11 @@ dependencies = [ [[package]] name = "comrak" -version = "0.18.0" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "482aa5695bca086022be453c700a40c02893f1ba7098a2c88351de55341ae894" +checksum = "82c995deda3bfdebd07d0e2af79e9da13e4b1be652b21a746f3f5b24bf0a49ef" dependencies = [ + "derive_builder", "entities", "memchr", "once_cell", @@ -1322,6 +1323,41 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "darling" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 1.0.109", +] + +[[package]] +name = "darling_macro" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e" +dependencies = [ + "darling_core", + "quote", + "syn 1.0.109", +] + [[package]] name = "dashmap" version = "5.5.3" @@ -1351,6 +1387,37 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2696e8a945f658fd14dc3b87242e6b80cd0f36ff04ea560fa39082368847946" +[[package]] +name = "derive_builder" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d67778784b508018359cbc8696edb3db78160bab2c2a28ba7f56ef6932997f8" +dependencies = [ + "derive_builder_macro", +] + +[[package]] +name = "derive_builder_core" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c11bdc11a0c47bc7d37d582b5285da6849c96681023680b906673c5707af7b0f" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "derive_builder_macro" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebcda35c7a396850a55ffeac740804b40ffec779b98fffbb1738f4033f0ee79e" +dependencies = [ + "derive_builder_core", + "syn 1.0.109", +] + [[package]] name = "derive_more" version = "0.99.17" @@ -2949,6 +3016,12 @@ dependencies = [ "cc", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 7d5977c5b..7ea8589a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,7 +47,7 @@ anyhow = { version = "1.0.42", features = ["backtrace"]} backtrace = "0.3.61" failure = "0.1.8" thiserror = "1.0.26" -comrak = { version = "0.18.0", default-features = false } +comrak = { version = "0.19.0", default-features = false } syntect = { version = "5.0.0", default-features = false, features = ["parsing", "html", "dump-load", "regex-onig"] } toml = "0.8.0" schemamama = "0.3" diff --git a/src/web/markdown.rs b/src/web/markdown.rs index 3a107d7a2..d8a994819 100644 --- a/src/web/markdown.rs +++ b/src/web/markdown.rs @@ -1,14 +1,15 @@ use crate::web::highlight; use comrak::{ - adapters::SyntaxHighlighterAdapter, ComrakExtensionOptions, ComrakOptions, ComrakPlugins, - ComrakRenderPlugins, + adapters::SyntaxHighlighterAdapter, ExtensionOptions, Options, Plugins, RenderPlugins, }; use std::collections::HashMap; #[derive(Debug)] struct CodeAdapter(F); -impl, &str) -> String> SyntaxHighlighterAdapter for CodeAdapter { +impl, &str) -> String + Send + Sync> SyntaxHighlighterAdapter + for CodeAdapter +{ fn write_highlighted( &self, output: &mut dyn std::io::Write, @@ -66,28 +67,29 @@ fn write_opening_tag( fn render_with_highlighter( text: &str, - highlighter: impl Fn(Option<&str>, &str) -> String, + highlighter: impl Fn(Option<&str>, &str) -> String + Send + Sync, ) -> String { - comrak::markdown_to_html_with_plugins( - text, - &ComrakOptions { - extension: ComrakExtensionOptions { - superscript: true, - table: true, - autolink: true, - tasklist: true, - strikethrough: true, - ..ComrakExtensionOptions::default() - }, - ..ComrakOptions::default() - }, - &ComrakPlugins { - render: ComrakRenderPlugins { - codefence_syntax_highlighter: Some(&CodeAdapter(highlighter)), - ..Default::default() - }, - }, - ) + let mut extension = ExtensionOptions::default(); + extension.superscript = true; + extension.table = true; + extension.autolink = true; + extension.tasklist = true; + extension.strikethrough = true; + + let options = Options { + extension, + ..Default::default() + }; + + let code_adapter = CodeAdapter(highlighter); + + let mut render = RenderPlugins::default(); + render.codefence_syntax_highlighter = Some(&code_adapter); + + let mut plugins = Plugins::default(); + plugins.render = render; + + comrak::markdown_to_html_with_plugins(text, &options, &plugins) } /// Wrapper around the Markdown parser and renderer to render markdown @@ -99,11 +101,11 @@ pub fn render(text: &str) -> String { mod test { use super::render_with_highlighter; use indoc::indoc; - use std::cell::RefCell; + use std::sync::Mutex; #[test] fn ignore_info_string_attributes() { - let highlighted = RefCell::new(vec![]); + let highlighted = Mutex::new(vec![]); let output = render_with_highlighter( indoc! {" @@ -116,16 +118,16 @@ mod test { ``` "}, |lang, code| { - highlighted - .borrow_mut() - .push((lang.map(str::to_owned), code.to_owned())); + let mut highlighted = highlighted.lock().unwrap(); + highlighted.push((lang.map(str::to_owned), code.to_owned())); code.to_owned() }, ); assert!(output.matches(r#""#).count() == 2); + let highlighted = highlighted.lock().unwrap(); assert_eq!( - highlighted.borrow().as_slice(), + highlighted.as_slice(), [ (Some("rust".into()), "ignore::commas();\n".into()), (Some("rust".into()), "ignore::spaces();\n".into())