From 14366dafc649f54b5c4407c0b36505c5180a58d4 Mon Sep 17 00:00:00 2001 From: southerntofu <52931252+southerntofu@users.noreply.github.com> Date: Mon, 18 Jan 2021 08:35:17 +0000 Subject: [PATCH] Fix fallback to syntax highlighting theme (closes #1309) (#1312) * Highlight fallback from extra syntaxes to the theme (close #1309) * Warning when codeblock language is unknown and cannot be highlighted * page/section path in codeblock language missing warning Co-authored-by: southerntofu --- components/config/src/highlighting.rs | 39 +++++++++++-------- components/rendering/src/markdown.rs | 1 + .../rendering/src/markdown/codeblock.rs | 25 ++++++++---- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/components/config/src/highlighting.rs b/components/config/src/highlighting.rs index 2d733d10..bfc0245b 100644 --- a/components/config/src/highlighting.rs +++ b/components/config/src/highlighting.rs @@ -16,29 +16,34 @@ lazy_static! { from_binary(include_bytes!("../../../sublime/themes/all.themedump")); } +pub enum HighlightSource { + Theme, + Extra, + Plain, + NotFound, +} + /// Returns the highlighter and whether it was found in the extra or not -pub fn get_highlighter(language: Option<&str>, config: &Config) -> (HighlightLines<'static>, bool) { +pub fn get_highlighter(language: Option<&str>, config: &Config) -> (HighlightLines<'static>, HighlightSource) { let theme = &THEME_SET.themes[config.highlight_theme()]; - let mut in_extra = false; if let Some(ref lang) = language { - let syntax = if let Some(ref extra) = config.markdown.extra_syntax_set { - let s = extra.find_syntax_by_token(lang); - if s.is_some() { - in_extra = true; + if let Some(ref extra_syntaxes) = config.markdown.extra_syntax_set { + if let Some(syntax) = extra_syntaxes.find_syntax_by_token(lang) { + return (HighlightLines::new(syntax, theme), HighlightSource::Extra); } - s - } else { - // The JS syntax hangs a lot... the TS syntax is probably better anyway. - // https://github.com/getzola/zola/issues/1241 - // https://github.com/getzola/zola/issues/1211 - // https://github.com/getzola/zola/issues/1174 - let hacked_lang = if *lang == "js" || *lang == "javascript" { "ts" } else { lang }; - SYNTAX_SET.find_syntax_by_token(hacked_lang) } - .unwrap_or_else(|| SYNTAX_SET.find_syntax_plain_text()); - (HighlightLines::new(syntax, theme), in_extra) + // The JS syntax hangs a lot... the TS syntax is probably better anyway. + // https://github.com/getzola/zola/issues/1241 + // https://github.com/getzola/zola/issues/1211 + // https://github.com/getzola/zola/issues/1174 + let hacked_lang = if *lang == "js" || *lang == "javascript" { "ts" } else { lang }; + if let Some(syntax) = SYNTAX_SET.find_syntax_by_token(hacked_lang) { + (HighlightLines::new(syntax, theme), HighlightSource::Theme) + } else { + (HighlightLines::new(SYNTAX_SET.find_syntax_plain_text(), theme), HighlightSource::NotFound) + } } else { - (HighlightLines::new(SYNTAX_SET.find_syntax_plain_text(), theme), false) + (HighlightLines::new(SYNTAX_SET.find_syntax_plain_text(), theme), HighlightSource::Plain) } } diff --git a/components/rendering/src/markdown.rs b/components/rendering/src/markdown.rs index 760e64be..96bcba4a 100644 --- a/components/rendering/src/markdown.rs +++ b/components/rendering/src/markdown.rs @@ -242,6 +242,7 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result { } impl<'config> CodeBlock<'config> { - pub fn new(fence_info: &str, config: &'config Config, background: IncludeBackground) -> Self { + pub fn new(fence_info: &str, config: &'config Config, background: IncludeBackground, path: Option<&'config str>) -> Self { let fence_info = FenceSettings::new(fence_info); let theme = &THEME_SET.themes[config.highlight_theme()]; - let (highlighter, in_extra) = get_highlighter(fence_info.language, config); + let (highlighter, highlight_source) = get_highlighter(fence_info.language, config); + let extra_syntax_set = match highlight_source { + HighlightSource::Extra => config.markdown.extra_syntax_set.as_ref(), + HighlightSource::NotFound => { + // Language was not found, so it exists (safe unwrap) + let lang = fence_info.language.unwrap(); + if let Some(path) = path { + eprintln!("Warning: Highlight language {} not found in {}", lang, path); + } else { + eprintln!("Warning: Highlight language {} not found", lang); + } + None + }, + _ => None, + }; Self { highlighter, - extra_syntax_set: match in_extra { - true => config.markdown.extra_syntax_set.as_ref(), - false => None, - }, + extra_syntax_set, background, theme,