11use crate :: web:: highlight;
22use comrak:: {
3- adapters:: SyntaxHighlighterAdapter , ComrakExtensionOptions , ComrakOptions , ComrakPlugins ,
4- ComrakRenderPlugins ,
3+ adapters:: SyntaxHighlighterAdapter , ExtensionOptions , Options , Plugins , RenderPlugins ,
54} ;
65use std:: collections:: HashMap ;
76
87#[ derive( Debug ) ]
98struct CodeAdapter < F > ( F ) ;
109
11- impl < F : Fn ( Option < & str > , & str ) -> String > SyntaxHighlighterAdapter for CodeAdapter < F > {
10+ impl < F : Fn ( Option < & str > , & str ) -> String + Send + Sync > SyntaxHighlighterAdapter
11+ for CodeAdapter < F >
12+ {
1213 fn write_highlighted (
1314 & self ,
1415 output : & mut dyn std:: io:: Write ,
@@ -66,28 +67,29 @@ fn write_opening_tag(
6667
6768fn render_with_highlighter (
6869 text : & str ,
69- highlighter : impl Fn ( Option < & str > , & str ) -> String ,
70+ highlighter : impl Fn ( Option < & str > , & str ) -> String + Send + Sync ,
7071) -> String {
71- comrak:: markdown_to_html_with_plugins (
72- text,
73- & ComrakOptions {
74- extension : ComrakExtensionOptions {
75- superscript : true ,
76- table : true ,
77- autolink : true ,
78- tasklist : true ,
79- strikethrough : true ,
80- ..ComrakExtensionOptions :: default ( )
81- } ,
82- ..ComrakOptions :: default ( )
83- } ,
84- & ComrakPlugins {
85- render : ComrakRenderPlugins {
86- codefence_syntax_highlighter : Some ( & CodeAdapter ( highlighter) ) ,
87- ..Default :: default ( )
88- } ,
89- } ,
90- )
72+ let mut extension = ExtensionOptions :: default ( ) ;
73+ extension. superscript = true ;
74+ extension. table = true ;
75+ extension. autolink = true ;
76+ extension. tasklist = true ;
77+ extension. strikethrough = true ;
78+
79+ let options = Options {
80+ extension,
81+ ..Default :: default ( )
82+ } ;
83+
84+ let code_adapter = CodeAdapter ( highlighter) ;
85+
86+ let mut render = RenderPlugins :: default ( ) ;
87+ render. codefence_syntax_highlighter = Some ( & code_adapter) ;
88+
89+ let mut plugins = Plugins :: default ( ) ;
90+ plugins. render = render;
91+
92+ comrak:: markdown_to_html_with_plugins ( text, & options, & plugins)
9193}
9294
9395/// Wrapper around the Markdown parser and renderer to render markdown
@@ -99,11 +101,11 @@ pub fn render(text: &str) -> String {
99101mod test {
100102 use super :: render_with_highlighter;
101103 use indoc:: indoc;
102- use std:: cell :: RefCell ;
104+ use std:: sync :: Mutex ;
103105
104106 #[ test]
105107 fn ignore_info_string_attributes ( ) {
106- let highlighted = RefCell :: new ( vec ! [ ] ) ;
108+ let highlighted = Mutex :: new ( vec ! [ ] ) ;
107109
108110 let output = render_with_highlighter (
109111 indoc ! { "
@@ -116,16 +118,16 @@ mod test {
116118 ```
117119 " } ,
118120 |lang, code| {
119- highlighted
120- . borrow_mut ( )
121- . push ( ( lang. map ( str:: to_owned) , code. to_owned ( ) ) ) ;
121+ let mut highlighted = highlighted. lock ( ) . unwrap ( ) ;
122+ highlighted. push ( ( lang. map ( str:: to_owned) , code. to_owned ( ) ) ) ;
122123 code. to_owned ( )
123124 } ,
124125 ) ;
125126
126127 assert ! ( output. matches( r#"<code class="language-rust">"# ) . count( ) == 2 ) ;
128+ let highlighted = highlighted. lock ( ) . unwrap ( ) ;
127129 assert_eq ! (
128- highlighted. borrow ( ) . as_slice( ) ,
130+ highlighted. as_slice( ) ,
129131 [
130132 ( Some ( "rust" . into( ) ) , "ignore::commas();\n " . into( ) ) ,
131133 ( Some ( "rust" . into( ) ) , "ignore::spaces();\n " . into( ) )
0 commit comments