Fix post-blank for headlines containing only whitespace.

This commit is contained in:
Tom Alexander 2023-12-15 17:59:47 -05:00
parent 9690545901
commit c28d8ccea4
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 16 additions and 2 deletions

View File

@ -67,6 +67,7 @@ fn _heading<'b, 'g, 'r, 's>(
let section_matcher = bind_context!(section, context);
let heading_matcher = bind_context!(heading(pre_headline.star_count), context);
let (contents_begin, _) = opt(many0(blank_line))(remaining)?;
let maybe_post_blank = get_consumed(remaining, contents_begin);
let (remaining, maybe_section) =
opt(map(section_matcher, DocumentElement::Section))(remaining)?;
let (remaining, _ws) = opt(tuple((start_of_line, many0(blank_line))))(remaining)?;
@ -83,7 +84,8 @@ fn _heading<'b, 'g, 'r, 's>(
}
children.insert(0, section);
}
let remaining = if children.is_empty() {
let has_children = !children.is_empty();
let remaining = if !has_children {
// Support empty headings
let (remain, _ws) = many0(blank_line)(remaining)?;
remain
@ -119,6 +121,11 @@ fn _heading<'b, 'g, 'r, 's>(
} else {
None
},
post_blank: if has_children {
None
} else {
Some(Into::<&str>::into(maybe_post_blank))
},
},
))
}

View File

@ -36,6 +36,7 @@ pub struct Heading<'s> {
pub deadline: Option<Timestamp<'s>>,
pub closed: Option<Timestamp<'s>>,
pub contents: Option<&'s str>,
pub post_blank: Option<&'s str>,
}
#[derive(Debug)]
@ -105,7 +106,13 @@ impl<'s> StandardProperties<'s> for Heading<'s> {
self.children
.last()
.map(|child| child.get_post_blank())
.unwrap_or(0)
.unwrap_or(
self.post_blank
.map(|text| text.lines().count())
.unwrap_or(0)
.try_into()
.expect("Too much post-blank to fit into a PostBlank."),
)
}
}