Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow treating a missing highlight language as error #2642

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions components/config/src/config/markup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 12 additions & 6 deletions components/markdown/src/codeblock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod highlight;

use std::ops::RangeInclusive;

use errors::{bail, Result};
use libs::syntect::util::LinesWithEndings;

use crate::codeblock::highlight::SyntaxHighlighter;
Expand Down Expand Up @@ -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);
Expand All @@ -93,7 +99,7 @@ impl<'config> CodeBlock<'config> {
highlighter.pre_class(),
fence.line_numbers,
);
(
Ok((
Self {
highlighter,
line_numbers: fence.line_numbers,
Expand All @@ -102,7 +108,7 @@ impl<'config> CodeBlock<'config> {
hide_lines: fence.hide_lines,
},
html_start,
)
))
}

pub fn highlight(&mut self, content: &str) -> String {
Expand Down
8 changes: 7 additions & 1 deletion components/markdown/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
3 changes: 3 additions & 0 deletions docs/content/documentation/getting-started/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down