1
- use crate :: error :: Result ;
1
+ use crate :: web :: highlight ;
2
2
use comrak:: {
3
3
adapters:: SyntaxHighlighterAdapter , ComrakExtensionOptions , ComrakOptions , ComrakPlugins ,
4
4
ComrakRenderPlugins ,
5
5
} ;
6
- use once_cell:: sync:: Lazy ;
7
6
use std:: { collections:: HashMap , fmt:: Write } ;
8
7
9
8
#[ derive( Debug ) ]
@@ -49,55 +48,6 @@ fn build_opening_tag(tag: &str, attributes: &HashMap<String, String>) -> String
49
48
tag_parts
50
49
}
51
50
52
- pub fn try_highlight_code ( lang : Option < & str > , code : & str ) -> Result < String > {
53
- use syntect:: {
54
- html:: { ClassStyle , ClassedHTMLGenerator } ,
55
- parsing:: SyntaxSet ,
56
- util:: LinesWithEndings ,
57
- } ;
58
-
59
- static SYNTAX_DATA : & [ u8 ] = include_bytes ! ( concat!( env!( "OUT_DIR" ) , "/syntect.packdump" ) ) ;
60
- static SYNTAXES : Lazy < SyntaxSet > = Lazy :: new ( || {
61
- let syntaxes: SyntaxSet = syntect:: dumps:: from_uncompressed_data ( SYNTAX_DATA ) . unwrap ( ) ;
62
- let names = syntaxes
63
- . syntaxes ( )
64
- . iter ( )
65
- . map ( |s| & s. name )
66
- . collect :: < Vec < _ > > ( ) ;
67
- log:: debug!( "known syntaxes {names:?}" ) ;
68
- syntaxes
69
- } ) ;
70
-
71
- let syntax = lang
72
- . and_then ( |lang| SYNTAXES . find_syntax_by_token ( lang) )
73
- . or_else ( || SYNTAXES . find_syntax_by_first_line ( code) )
74
- . unwrap_or_else ( || SYNTAXES . find_syntax_plain_text ( ) ) ;
75
-
76
- log:: trace!( "Using syntax {:?} for language {lang:?}" , syntax. name) ;
77
-
78
- let mut html_generator = ClassedHTMLGenerator :: new_with_class_style (
79
- syntax,
80
- & SYNTAXES ,
81
- ClassStyle :: SpacedPrefixed { prefix : "syntax-" } ,
82
- ) ;
83
-
84
- for line in LinesWithEndings :: from ( code) {
85
- html_generator. parse_html_for_line_which_includes_newline ( line) ?;
86
- }
87
-
88
- Ok ( html_generator. finalize ( ) )
89
- }
90
-
91
- pub fn highlight_code ( lang : Option < & str > , code : & str ) -> String {
92
- match try_highlight_code ( lang, code) {
93
- Ok ( highlighted) => highlighted,
94
- Err ( err) => {
95
- log:: error!( "failed while highlighting code: {err:?}" ) ;
96
- code. to_owned ( )
97
- }
98
- }
99
- }
100
-
101
51
fn render_with_highlighter (
102
52
text : & str ,
103
53
highlighter : impl Fn ( Option < & str > , & str ) -> String ,
@@ -125,12 +75,12 @@ fn render_with_highlighter(
125
75
126
76
/// Wrapper around the Markdown parser and renderer to render markdown
127
77
pub fn render ( text : & str ) -> String {
128
- render_with_highlighter ( text, highlight_code )
78
+ render_with_highlighter ( text, highlight :: with_lang )
129
79
}
130
80
131
81
#[ cfg( test) ]
132
82
mod test {
133
- use super :: { highlight_code , render_with_highlighter} ;
83
+ use super :: render_with_highlighter;
134
84
use indoc:: indoc;
135
85
use std:: cell:: RefCell ;
136
86
@@ -152,7 +102,7 @@ mod test {
152
102
highlighted
153
103
. borrow_mut ( )
154
104
. push ( ( lang. map ( str:: to_owned) , code. to_owned ( ) ) ) ;
155
- highlight_code ( lang , code)
105
+ code. to_owned ( )
156
106
} ,
157
107
) ;
158
108
0 commit comments