2020//! edition: Edition::Edition2015,
2121//! playground: &None,
2222//! heading_offset: HeadingOffset::H2,
23+ //! highlight_foreign_code: false,
2324//! };
2425//! let mut html = String::new();
2526//! md.write_into(&mut html).unwrap();
@@ -99,6 +100,8 @@ pub struct Markdown<'a> {
99100 /// Offset at which we render headings.
100101 /// E.g. if `heading_offset: HeadingOffset::H2`, then `# something` renders an `<h2>`.
101102 pub heading_offset : HeadingOffset ,
103+ /// Whether to syntax-highlight non-Rust code blocks using tree-sitter.
104+ pub highlight_foreign_code : bool ,
102105}
103106/// A struct like `Markdown` that renders the markdown with a table of contents.
104107pub ( crate ) struct MarkdownWithToc < ' a > {
@@ -108,6 +111,7 @@ pub(crate) struct MarkdownWithToc<'a> {
108111 pub ( crate ) error_codes : ErrorCodes ,
109112 pub ( crate ) edition : Edition ,
110113 pub ( crate ) playground : & ' a Option < Playground > ,
114+ pub ( crate ) highlight_foreign_code : bool ,
111115}
112116/// A tuple struct like `Markdown` that renders the markdown escaping HTML tags
113117/// and includes no paragraph tags.
@@ -210,6 +214,8 @@ struct CodeBlocks<'p, 'a, I: Iterator<Item = Event<'a>>> {
210214 // Information about the playground if a URL has been specified, containing an
211215 // optional crate name and the URL.
212216 playground : & ' p Option < Playground > ,
217+ // Whether to use tree-sitter highlighting for non-Rust code blocks.
218+ highlight_foreign_code : bool ,
213219}
214220
215221impl < ' p , ' a , I : Iterator < Item = Event < ' a > > > CodeBlocks < ' p , ' a , I > {
@@ -218,8 +224,15 @@ impl<'p, 'a, I: Iterator<Item = Event<'a>>> CodeBlocks<'p, 'a, I> {
218224 error_codes : ErrorCodes ,
219225 edition : Edition ,
220226 playground : & ' p Option < Playground > ,
227+ highlight_foreign_code : bool ,
221228 ) -> Self {
222- CodeBlocks { inner : iter, check_error_codes : error_codes, edition, playground }
229+ CodeBlocks {
230+ inner : iter,
231+ check_error_codes : error_codes,
232+ edition,
233+ playground,
234+ highlight_foreign_code,
235+ }
223236 }
224237}
225238
@@ -254,14 +267,17 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
254267 let lang_string = lang. map ( |l| format ! ( "language-{l}" ) ) . unwrap_or_default ( ) ;
255268 let whitespace = if added_classes. is_empty ( ) { "" } else { " " } ;
256269
257- // Try to highlight with arborium if we have a language
258- let code_html = lang
259- . and_then ( |l| {
270+ // Try to highlight with arborium if enabled and we have a language
271+ let code_html = if self . highlight_foreign_code {
272+ lang . and_then ( |l| {
260273 highlight:: highlight_foreign_code ( l, original_text. trim_suffix ( '\n' ) )
261274 } )
262275 . unwrap_or_else ( || {
263276 Escape ( original_text. trim_suffix ( '\n' ) ) . to_string ( )
264- } ) ;
277+ } )
278+ } else {
279+ Escape ( original_text. trim_suffix ( '\n' ) ) . to_string ( )
280+ } ;
265281
266282 return Some ( Event :: Html (
267283 format ! (
@@ -1354,6 +1370,7 @@ impl<'a> Markdown<'a> {
13541370 edition,
13551371 playground,
13561372 heading_offset,
1373+ highlight_foreign_code,
13571374 } = self ;
13581375
13591376 let replacer = move |broken_link : BrokenLink < ' _ > | {
@@ -1371,7 +1388,7 @@ impl<'a> Markdown<'a> {
13711388 let p = SpannedLinkReplacer :: new ( p, links) ;
13721389 let p = footnotes:: Footnotes :: new ( p, existing_footnotes) ;
13731390 let p = TableWrapper :: new ( p. map ( |( ev, _) | ev) ) ;
1374- CodeBlocks :: new ( p, codes, edition, playground)
1391+ CodeBlocks :: new ( p, codes, edition, playground, highlight_foreign_code )
13751392 } )
13761393 }
13771394
@@ -1427,8 +1444,15 @@ impl<'a> Markdown<'a> {
14271444
14281445impl MarkdownWithToc < ' _ > {
14291446 pub ( crate ) fn into_parts ( self ) -> ( Toc , String ) {
1430- let MarkdownWithToc { content : md, links, ids, error_codes : codes, edition, playground } =
1431- self ;
1447+ let MarkdownWithToc {
1448+ content : md,
1449+ links,
1450+ ids,
1451+ error_codes : codes,
1452+ edition,
1453+ playground,
1454+ highlight_foreign_code,
1455+ } = self ;
14321456
14331457 // This is actually common enough to special-case
14341458 if md. is_empty ( ) {
@@ -1452,7 +1476,7 @@ impl MarkdownWithToc<'_> {
14521476 let p = HeadingLinks :: new ( p, Some ( & mut toc) , ids, HeadingOffset :: H1 ) ;
14531477 let p = footnotes:: Footnotes :: new ( p, existing_footnotes) ;
14541478 let p = TableWrapper :: new ( p. map ( |( ev, _) | ev) ) ;
1455- let p = CodeBlocks :: new ( p, codes, edition, playground) ;
1479+ let p = CodeBlocks :: new ( p, codes, edition, playground, highlight_foreign_code ) ;
14561480 html:: push_html ( & mut s, p) ;
14571481 } ) ;
14581482
0 commit comments