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

Reduce code repetition in CodeBlock::highlight #1697

Merged
merged 1 commit into from
Dec 19, 2021
Merged
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
33 changes: 12 additions & 21 deletions components/rendering/src/codeblock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,41 +136,32 @@ impl<'config> CodeBlock<'config> {
}
}

if self.line_numbers {
buffer.push_str("<tr><td>");
let num = format!("{}", self.line_number_start + i);
let maybe_mark = |buffer: &mut String, s: &str| {
if is_higlighted {
buffer.push_str("<mark");
if let Some(ref s) = mark_style {
if let Some(ref style) = mark_style {
buffer.push_str(" style=\"");
buffer.push_str(s);
buffer.push_str(style);
buffer.push_str("\">");
} else {
buffer.push('>')
}
buffer.push_str(&num);
buffer.push_str(s);
buffer.push_str("</mark>");
} else {
buffer.push_str(&num);
buffer.push_str(s);
}
};

if self.line_numbers {
buffer.push_str("<tr><td>");
let num = format!("{}", self.line_number_start + i);
maybe_mark(&mut buffer, &num);
buffer.push_str("</td><td>");
}

let highlighted_line = self.highlighter.highlight_line(line);
if is_higlighted {
buffer.push_str("<mark");
if let Some(ref s) = mark_style {
buffer.push_str(" style=\"");
buffer.push_str(s);
buffer.push_str("\">");
} else {
buffer.push('>')
}
buffer.push_str(&highlighted_line);
buffer.push_str("</mark>");
} else {
buffer.push_str(&highlighted_line);
}
maybe_mark(&mut buffer, &highlighted_line);
}

if let Some(rest) = self.highlighter.finalize() {
Expand Down