From ead17d0a3a20bfb67043a076c061b35ae6b6ddea Mon Sep 17 00:00:00 2001 From: Sam Wilson <57262657+SamWilsn@users.noreply.github.com> Date: Wed, 25 Sep 2024 13:55:49 -0400 Subject: [PATCH] Allow treating a missing highlight language as error (#2642) --- components/config/src/config/markup.rs | 3 +++ components/markdown/src/codeblock/mod.rs | 18 ++++++++++++------ components/markdown/src/markdown.rs | 8 +++++++- .../getting-started/configuration.md | 3 +++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/components/config/src/config/markup.rs b/components/config/src/config/markup.rs index 580a665ae6..9ea5b2337b 100644 --- a/components/config/src/config/markup.rs +++ b/components/config/src/config/markup.rs @@ -27,6 +27,8 @@ pub struct ThemeCss { pub struct Markdown { /// Whether to highlight all code blocks found in markdown files. Defaults to false pub highlight_code: bool, + /// Emit an error for missing highlight languages. Defaults to false + pub error_on_missing_highlight: bool, /// Which themes to use for code highlighting. See Readme for supported themes /// Defaults to "base16-ocean-dark" pub highlight_theme: String, @@ -198,6 +200,7 @@ impl Default for Markdown { fn default() -> Markdown { Markdown { highlight_code: false, + error_on_missing_highlight: false, highlight_theme: DEFAULT_HIGHLIGHT_THEME.to_owned(), highlight_themes_css: Vec::new(), render_emoji: false, diff --git a/components/markdown/src/codeblock/mod.rs b/components/markdown/src/codeblock/mod.rs index 5e297926ff..fd0c1d93f6 100644 --- a/components/markdown/src/codeblock/mod.rs +++ b/components/markdown/src/codeblock/mod.rs @@ -3,6 +3,7 @@ mod highlight; use std::ops::RangeInclusive; +use errors::{bail, Result}; use libs::syntect::util::LinesWithEndings; use crate::codeblock::highlight::SyntaxHighlighter; @@ -75,14 +76,19 @@ impl<'config> CodeBlock<'config> { config: &'config Config, // path to the current file if there is one, to point where the error is path: Option<&'config str>, - ) -> (Self, String) { + ) -> Result<(Self, String)> { let syntax_and_theme = resolve_syntax_and_theme(fence.language, config); if syntax_and_theme.source == HighlightSource::NotFound && config.markdown.highlight_code { let lang = fence.language.unwrap(); - if let Some(p) = path { - eprintln!("Warning: Highlight language {} not found in {}", lang, p); + let msg = if let Some(p) = path { + format!("Highlight language {} not found in {}", lang, p) } else { - eprintln!("Warning: Highlight language {} not found", lang); + format!("Highlight language {} not found", lang) + }; + if config.markdown.error_on_missing_highlight { + bail!(msg); + } else { + eprintln!("Warning: {}", msg); } } let highlighter = SyntaxHighlighter::new(config.markdown.highlight_code, syntax_and_theme); @@ -93,7 +99,7 @@ impl<'config> CodeBlock<'config> { highlighter.pre_class(), fence.line_numbers, ); - ( + Ok(( Self { highlighter, line_numbers: fence.line_numbers, @@ -102,7 +108,7 @@ impl<'config> CodeBlock<'config> { hide_lines: fence.hide_lines, }, html_start, - ) + )) } pub fn highlight(&mut self, content: &str) -> String { diff --git a/components/markdown/src/markdown.rs b/components/markdown/src/markdown.rs index 41a59a7795..16210b3841 100644 --- a/components/markdown/src/markdown.rs +++ b/components/markdown/src/markdown.rs @@ -559,7 +559,13 @@ pub fn markdown_to_html( cmark::CodeBlockKind::Fenced(fence_info) => FenceSettings::new(fence_info), _ => FenceSettings::new(""), }; - let (block, begin) = CodeBlock::new(fence, context.config, path); + let (block, begin) = match CodeBlock::new(fence, context.config, path) { + Ok(cb) => cb, + Err(e) => { + error = Some(e); + break; + } + }; code_block = Some(block); events.push(Event::Html(begin.into())); } diff --git a/docs/content/documentation/getting-started/configuration.md b/docs/content/documentation/getting-started/configuration.md index 7b5f439bf6..0591b021d5 100644 --- a/docs/content/documentation/getting-started/configuration.md +++ b/docs/content/documentation/getting-started/configuration.md @@ -114,6 +114,9 @@ generate_robots_txt = true # When set to "true", all code blocks are highlighted. highlight_code = false +# When set to "true", missing highlight languages are treated as errors. Defaults to false. +error_on_missing_highlight = false + # A list of directories used to search for additional `.sublime-syntax` and `.tmTheme` files. extra_syntaxes_and_themes = []