vertically aligned code comments

This commit is contained in:
りき萌 2025-08-30 13:13:29 +02:00
parent ee0a95974b
commit 408b984266
6 changed files with 83 additions and 12 deletions

View file

@ -2,6 +2,7 @@
//! Made concrete to avoid generic hell, with added treehouse-specific features.
use std::fmt::Write;
use std::mem;
use std::ops::Range;
use codespan_reporting::diagnostic::Diagnostic;
@ -42,6 +43,7 @@ impl Renderer<'_> {
renderer: self,
raw: Raw::None,
code_block: None,
code_block_text: String::new(),
img_alt_text: 0,
list_tightness: vec![],
not_first_line: false,
@ -88,6 +90,7 @@ struct Writer<'a> {
raw: Raw,
code_block: Option<CodeBlock<'a>>,
code_block_text: String,
img_alt_text: usize,
list_tightness: Vec<bool>,
not_first_line: bool,
@ -445,6 +448,17 @@ impl<'a> Writer<'a> {
Container::CodeBlock { language } => {
let code_block = self.code_block.take().unwrap();
let rendered =
if let Some(syntax) = self.renderer.config.syntaxes.get(*language) {
let mut rendered = String::new();
highlight(&mut rendered, syntax, &self.code_block_text);
self.code_block_text.clear();
rendered
} else {
mem::take(&mut self.code_block_text)
};
out.push_str(&rendered);
out.push_str(match &code_block.kind {
CodeBlockKind::PlainText | CodeBlockKind::SyntaxHighlight => {
"</code></pre>"
@ -507,11 +521,8 @@ impl<'a> Writer<'a> {
Event::Str(s) => match self.raw {
Raw::None if self.img_alt_text > 0 => write_attr(s, out),
Raw::None => {
let syntax = self.code_block.as_ref().and_then(|code_block| {
self.renderer.config.syntaxes.get(code_block.language)
});
if let Some(syntax) = syntax {
highlight(out, syntax, s);
if self.code_block.is_some() {
self.code_block_text.push_str(s);
} else {
write_text(s, out);
}