|
| 1 | +use crate::error::Result; |
| 2 | +use comrak::{ |
| 3 | + adapters::SyntaxHighlighterAdapter, ComrakExtensionOptions, ComrakOptions, ComrakPlugins, |
| 4 | + ComrakRenderPlugins, |
| 5 | +}; |
| 6 | +use once_cell::sync::Lazy; |
| 7 | +use std::collections::HashMap; |
| 8 | +use std::fmt::Write; |
| 9 | + |
| 10 | +#[derive(Debug)] |
| 11 | +struct CodeAdapter; |
| 12 | + |
| 13 | +impl SyntaxHighlighterAdapter for CodeAdapter { |
| 14 | + fn highlight(&self, lang: Option<&str>, code: &str) -> String { |
| 15 | + highlight_code(lang, code) |
| 16 | + } |
| 17 | + |
| 18 | + fn build_pre_tag(&self, attributes: &HashMap<String, String>) -> String { |
| 19 | + build_opening_tag("pre", attributes) |
| 20 | + } |
| 21 | + |
| 22 | + fn build_code_tag(&self, attributes: &HashMap<String, String>) -> String { |
| 23 | + build_opening_tag("code", attributes) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +fn build_opening_tag(tag: &str, attributes: &HashMap<String, String>) -> String { |
| 28 | + let mut tag_parts = format!("<{tag}"); |
| 29 | + for (attr, val) in attributes { |
| 30 | + write!(tag_parts, " {attr}=\"{val}\"").unwrap(); |
| 31 | + } |
| 32 | + tag_parts.push('>'); |
| 33 | + tag_parts |
| 34 | +} |
| 35 | + |
| 36 | +pub fn try_highlight_code(lang: Option<&str>, code: &str) -> Result<String> { |
| 37 | + use syntect::{ |
| 38 | + html::{ClassStyle, ClassedHTMLGenerator}, |
| 39 | + parsing::SyntaxSet, |
| 40 | + util::LinesWithEndings, |
| 41 | + }; |
| 42 | + |
| 43 | + static SYNTAX_DATA: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/syntect.packdump")); |
| 44 | + static SYNTAXES: Lazy<SyntaxSet> = Lazy::new(|| { |
| 45 | + let syntaxes: SyntaxSet = syntect::dumps::from_uncompressed_data(SYNTAX_DATA).unwrap(); |
| 46 | + let names = syntaxes |
| 47 | + .syntaxes() |
| 48 | + .iter() |
| 49 | + .map(|s| &s.name) |
| 50 | + .collect::<Vec<_>>(); |
| 51 | + log::debug!("known syntaxes {names:?}"); |
| 52 | + syntaxes |
| 53 | + }); |
| 54 | + |
| 55 | + let syntax = lang |
| 56 | + .and_then(|lang| SYNTAXES.find_syntax_by_token(lang)) |
| 57 | + .or_else(|| SYNTAXES.find_syntax_by_first_line(code)) |
| 58 | + .unwrap_or_else(|| SYNTAXES.find_syntax_plain_text()); |
| 59 | + |
| 60 | + log::trace!("Using syntax {:?} for language {lang:?}", syntax.name); |
| 61 | + |
| 62 | + let mut html_generator = ClassedHTMLGenerator::new_with_class_style( |
| 63 | + syntax, |
| 64 | + &SYNTAXES, |
| 65 | + ClassStyle::SpacedPrefixed { prefix: "syntax-" }, |
| 66 | + ); |
| 67 | + |
| 68 | + for line in LinesWithEndings::from(code) { |
| 69 | + html_generator.parse_html_for_line_which_includes_newline(line)?; |
| 70 | + } |
| 71 | + |
| 72 | + Ok(html_generator.finalize()) |
| 73 | +} |
| 74 | + |
| 75 | +pub fn highlight_code(lang: Option<&str>, code: &str) -> String { |
| 76 | + match try_highlight_code(lang, code) { |
| 77 | + Ok(highlighted) => highlighted, |
| 78 | + Err(err) => { |
| 79 | + log::error!("failed while highlighting code: {err:?}"); |
| 80 | + code.to_owned() |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// Wrapper around the Markdown parser and renderer to render markdown |
| 86 | +pub(crate) fn render(text: &str) -> String { |
| 87 | + comrak::markdown_to_html_with_plugins( |
| 88 | + text, |
| 89 | + &ComrakOptions { |
| 90 | + extension: ComrakExtensionOptions { |
| 91 | + superscript: true, |
| 92 | + table: true, |
| 93 | + autolink: true, |
| 94 | + tasklist: true, |
| 95 | + strikethrough: true, |
| 96 | + ..ComrakExtensionOptions::default() |
| 97 | + }, |
| 98 | + ..ComrakOptions::default() |
| 99 | + }, |
| 100 | + &ComrakPlugins { |
| 101 | + render: ComrakRenderPlugins { |
| 102 | + codefence_syntax_highlighter: Some(&CodeAdapter), |
| 103 | + }, |
| 104 | + }, |
| 105 | + ) |
| 106 | +} |
0 commit comments