From 7ce78f95fa13dece2fd6686227273c235936c6d7 Mon Sep 17 00:00:00 2001 From: Noah Hellman Date: Thu, 19 Jan 2023 22:58:33 +0100 Subject: [PATCH] block: trim inline indentation --- src/block.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/block.rs b/src/block.rs index 201de9c..8109169 100644 --- a/src/block.rs +++ b/src/block.rs @@ -153,8 +153,21 @@ impl<'s> TreeParser<'s> { Block::Leaf(l) => { self.tree.enter(Node::Leaf(l), span); - // trim starting whitespace of the block contents - lines[0] = lines[0].trim_start(self.src); + if matches!(l, Leaf::CodeBlock) { + 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) let lines = if lines[0].is_empty() { @@ -163,15 +176,6 @@ impl<'s> TreeParser<'s> { 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)); self.tree.exit(); } @@ -462,7 +466,7 @@ mod test { (Atom(Blankline), "\n"), (Enter(Leaf(Heading)), "#"), (Inline, "8\n"), - (Inline, " 12\n"), + (Inline, "12\n"), (Inline, "15"), (Exit(Leaf(Heading)), "#"), );