Skip to content

Commit

Permalink
Reduce code repetition in CodeBlock::highlight (#1697)
Browse files Browse the repository at this point in the history
  • Loading branch information
meithecatte authored and Keats committed Jan 23, 2022
1 parent c5aa1a2 commit 4d14c46
Showing 1 changed file with 12 additions and 21 deletions.
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

0 comments on commit 4d14c46

Please sign in to comment.