fix: csharp and php do not render correctly

This commit is contained in:
sigoden 2023-03-09 22:46:33 +08:00
parent 553d0fe55b
commit 89eaf1b653

View File

@ -1,4 +1,6 @@
use crossterm::style::{Color, Stylize};
use lazy_static::lazy_static;
use std::collections::HashMap;
use syntect::highlighting::{Color as SyntectColor, FontStyle, Style, Theme};
use syntect::parsing::SyntaxSet;
use syntect::{easy::HighlightLines, parsing::SyntaxReference};
@ -8,6 +10,15 @@ const MD_THEME: &[u8] = include_bytes!("../../assets/monokai-extended.theme.bin"
/// Comes from https://github.com/sharkdp/bat/raw/5e77ca37e89c873e4490b42ff556370dc5c6ba4f/assets/syntaxes.bin
const SYNTAXES: &[u8] = include_bytes!("../../assets/syntaxes.bin");
lazy_static! {
static ref LANGE_MAPS: HashMap<String, String> = {
let mut m = HashMap::new();
m.insert("csharp".into(), "C#".into());
m.insert("php".into(), "PHP Source".into());
m
};
}
pub struct MarkdownRender {
syntax_set: SyntaxSet,
md_theme: Theme,
@ -83,6 +94,11 @@ impl MarkdownRender {
self.render_line_inner(line, &self.md_syntax)
}
LineType::CodeBegin => {
if self.code_syntax.is_none() {
if let Some(syntax) = self.syntax_set.find_syntax_by_first_line(line) {
self.code_syntax = Some(syntax.clone());
}
}
self.prev_line_type = LineType::CodeInner;
self.render_code_line(line)
}
@ -109,11 +125,15 @@ impl MarkdownRender {
}
fn find_syntax(&self, lang: &str) -> Option<&SyntaxReference> {
if let Some(new_lang) = LANGE_MAPS.get(&lang.to_ascii_lowercase()) {
self.syntax_set.find_syntax_by_name(new_lang)
} else {
self.syntax_set
.find_syntax_by_token(lang)
.or_else(|| self.syntax_set.find_syntax_by_extension(lang))
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LineType {
@ -200,4 +220,10 @@ mod tests {
let md_theme: Theme = bincode::deserialize_from(MD_THEME).expect("invalid md_theme binary");
assert_eq!(md_theme.name, Some("Monokai Extended".into()));
}
#[test]
fn test_render() {
let render = MarkdownRender::new();
assert!(render.find_syntax("csharp").is_some());
}
}