Continue highlights across code block lines.

This commit is contained in:
Tom Alexander 2025-02-22 18:39:41 -05:00
parent 75a763569b
commit 41927764fc
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 21 additions and 2 deletions

View File

@ -1,4 +1,4 @@
* Things to do [3/14]
* Things to do [4/14]
** DONE If the paragraph only contains an image, text-align center
** DONE Syntax highlighting for code blocks
** TODO Render gnuplot
@ -14,4 +14,4 @@
** TODO Add highlighting for languages [0/2]
*** TODO bash
*** TODO gnuplot
** TODO Bug: carry over highlight starts when breaking lines
** DONE Bug: carry over highlight starts when breaking lines

View File

@ -169,6 +169,7 @@ where
let mut highlighted_text: Vec<ISrcLine> = Vec::with_capacity(lines.len());
let mut current_line = ISrcLine::new();
let mut highlight_stack: Vec<&str> = Vec::new();
for event in highlights {
match event.unwrap() {
HighlightEvent::Source { start, end } => {
@ -178,8 +179,22 @@ where
current_line
.children
.push(ISrcSegment::RawText(first_line.to_owned()));
current_line.children.extend(
highlight_stack
.iter()
.map(|_name| ISrcSegment::HighlightEnd),
);
highlighted_text.push(current_line);
current_line = ISrcLine::new();
current_line
.children
.extend(
highlight_stack
.iter()
.map(|name| ISrcSegment::HighlightStart {
name: (*name).into(),
}),
);
span = &span[(line_break_index + 1)..];
}
if !span.is_empty() {
@ -189,16 +204,20 @@ where
}
}
HighlightEvent::HighlightStart(s) => {
highlight_stack.push(highlight_names[s.0]);
current_line.children.push(ISrcSegment::HighlightStart {
name: highlight_names[s.0].into(),
});
}
HighlightEvent::HighlightEnd => {
highlight_stack.pop();
current_line.children.push(ISrcSegment::HighlightEnd);
}
}
}
debug_assert!(highlight_stack.is_empty());
Ok(highlighted_text)
}