11use crate :: web:: highlight;
2- use comrak:: {
3- ExtensionOptions , Options , Plugins , RenderPlugins , adapters :: SyntaxHighlighterAdapter ,
4- } ;
5- use std:: { collections :: HashMap , fmt } ;
2+ use comrak:: { Options , adapters :: SyntaxHighlighterAdapter , options } ;
3+ use std :: { borrow :: Cow , collections :: HashMap , fmt } ;
4+
5+ type Attributes < ' s > = HashMap < & ' static str , std:: borrow :: Cow < ' s , str > > ;
66
77#[ derive( Debug ) ]
88struct CodeAdapter < F > ( F , Option < & ' static str > ) ;
@@ -22,41 +22,42 @@ impl<F: Fn(Option<&str>, &str, Option<&str>) -> String + Send + Sync> SyntaxHigh
2222 write ! ( output, "{}" , ( self . 0 ) ( lang, code, self . 1 ) )
2323 }
2424
25- fn write_pre_tag (
25+ fn write_pre_tag < ' s > (
2626 & self ,
2727 output : & mut dyn fmt:: Write ,
28- attributes : HashMap < String , String > ,
29- ) -> Result < ( ) , fmt:: Error > {
28+ attributes : Attributes < ' s > ,
29+ ) -> fmt:: Result {
3030 write_opening_tag ( output, "pre" , & attributes)
3131 }
3232
33- fn write_code_tag (
33+ fn write_code_tag < ' s > (
3434 & self ,
3535 output : & mut dyn fmt:: Write ,
36- attributes : HashMap < String , String > ,
37- ) -> Result < ( ) , fmt:: Error > {
36+ attributes : Attributes < ' s > ,
37+ ) -> fmt:: Result {
3838 // similarly to above, since comrak does not treat `,` as an info-string delimiter it will
3939 // try to apply `class="language-rust,ignore"` for the info-string `rust,ignore`, so we
4040 // have to detect that case and fixup the class here
4141 // TODO: https://github.com/kivikakk/comrak/issues/246
4242 let mut attributes = attributes;
4343 if let Some ( classes) = attributes. get_mut ( "class" ) {
44- * classes = classes
44+ let mut updated : String = classes
4545 . split ( ' ' )
4646 . flat_map ( |class| [ class. split ( ',' ) . next ( ) . unwrap_or ( class) , " " ] )
4747 . collect ( ) ;
4848 // remove trailing ' '
4949 // TODO: https://github.com/rust-lang/rust/issues/79524 or itertools
50- classes. pop ( ) ;
50+ updated. pop ( ) ;
51+ * classes = Cow :: Owned ( updated) ;
5152 }
5253 write_opening_tag ( output, "code" , & attributes)
5354 }
5455}
5556
56- fn write_opening_tag (
57+ fn write_opening_tag < ' s > (
5758 output : & mut dyn fmt:: Write ,
5859 tag : & str ,
59- attributes : & HashMap < String , String > ,
60+ attributes : & Attributes < ' s > ,
6061) -> Result < ( ) , fmt:: Error > {
6162 write ! ( output, "<{tag}" ) ?;
6263 for ( attr, val) in attributes {
@@ -75,7 +76,7 @@ fn render_with_highlighter(
7576 comrak:: markdown_to_html_with_plugins (
7677 text,
7778 & Options {
78- extension : ExtensionOptions {
79+ extension : options :: Extension {
7980 superscript : true ,
8081 table : true ,
8182 autolink : true ,
@@ -85,8 +86,8 @@ fn render_with_highlighter(
8586 } ,
8687 ..Default :: default ( )
8788 } ,
88- & Plugins {
89- render : RenderPlugins {
89+ & options :: Plugins {
90+ render : options :: RenderPlugins {
9091 codefence_syntax_highlighter : Some ( & code_adapter) ,
9192 ..Default :: default ( )
9293 } ,
0 commit comments