block: trim inline indentation

This commit is contained in:
Noah Hellman 2023-01-19 22:58:33 +01:00
parent b2d39e3ded
commit 7ce78f95fa

View file

@ -153,8 +153,21 @@ impl<'s> TreeParser<'s> {
Block::Leaf(l) => { Block::Leaf(l) => {
self.tree.enter(Node::Leaf(l), span); self.tree.enter(Node::Leaf(l), span);
// trim starting whitespace of the block contents if matches!(l, Leaf::CodeBlock) {
lines[0] = lines[0].trim_start(self.src); lines[0] = lines[0].trim_start(self.src);
} else {
// trim starting whitespace of each inline
for line in lines.iter_mut() {
*line = line.trim_start(self.src);
}
// trim ending whitespace of block
let l = lines.len();
if l > 0 {
let last = &mut lines[l - 1];
*last = last.trim_end(self.src);
}
}
// skip first inline if empty (e.g. code block) // skip first inline if empty (e.g. code block)
let lines = if lines[0].is_empty() { let lines = if lines[0].is_empty() {
@ -163,15 +176,6 @@ impl<'s> TreeParser<'s> {
lines lines
}; };
// trim ending whitespace of block if not verbatim
if !matches!(l, Leaf::CodeBlock) {
let l = lines.len();
if l > 0 {
let last = &mut lines[l - 1];
*last = last.trim_end(self.src);
}
}
lines.iter().for_each(|line| self.tree.inline(*line)); lines.iter().for_each(|line| self.tree.inline(*line));
self.tree.exit(); self.tree.exit();
} }
@ -462,7 +466,7 @@ mod test {
(Atom(Blankline), "\n"), (Atom(Blankline), "\n"),
(Enter(Leaf(Heading)), "#"), (Enter(Leaf(Heading)), "#"),
(Inline, "8\n"), (Inline, "8\n"),
(Inline, " 12\n"), (Inline, "12\n"),
(Inline, "15"), (Inline, "15"),
(Exit(Leaf(Heading)), "#"), (Exit(Leaf(Heading)), "#"),
); );