diff --git a/src/block.rs b/src/block.rs index a5a499d..04ce9a6 100644 --- a/src/block.rs +++ b/src/block.rs @@ -91,11 +91,22 @@ impl<'s> Parser<'s> { match &kind { Block::Leaf(l) => { self.tree.enter(kind, span); - lines[0] = lines[0].with_start(span.end()); + let first = &mut lines[0]; + *first = first.with_start(span.end()); + // trim starting whitespace of block + *first = Span::new( + first.end() - first.of(self.src).trim_start().len(), + first.end(), + ); let len = match l { CodeBlock { .. } => len - 2, _ => len, }; + if !matches!(l, Leaf::CodeBlock { .. }) { + // trim ending whitespace of block + let last = &mut lines[len - 1]; + *last = last.with_len(last.of(self.src).trim_end().len()); + } for line in &lines[0..len] { self.tree.elem(Atom::Inline, *line); } diff --git a/src/span.rs b/src/span.rs index 35deb5a..ec5c2c3 100644 --- a/src/span.rs +++ b/src/span.rs @@ -24,6 +24,10 @@ impl Span { Self::new(start, self.end()) } + pub fn with_len(self, len: usize) -> Self { + Self::by_len(self.start(), len) + } + pub fn trim_start(self, n: usize) -> Self { Self::new(self.start().checked_add(n).unwrap(), self.end()) }