From 25864fa4676d54263b385ecf824bbb6fc8e9aa2a Mon Sep 17 00:00:00 2001 From: Noah Hellman Date: Sat, 28 Jan 2023 18:38:29 +0100 Subject: [PATCH] fixup! block: add hierarchical heading sections --- src/block.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/src/block.rs b/src/block.rs index 9018683..59d648e 100644 --- a/src/block.rs +++ b/src/block.rs @@ -287,8 +287,7 @@ impl<'s> TreeParser<'s> { let first_close = self .open_sections .iter() - .rev() - .rposition(|l| *l > level) + .rposition(|l| *l < level) .map_or(0, |i| i + 1); self.open_sections.drain(first_close..).for_each(|_| { self.tree.exit(); // section @@ -920,6 +919,60 @@ mod test { ); } + #[test] + fn parse_section() { + test_parse!( + concat!( + "# a\n", + "\n", + "## aa\n", + "\n", + "#### aaaa\n", + "\n", + "## ab\n", + "\n", + "### aba\n", + "\n", + "# b\n", + ), + (Enter(Container(Section)), "#"), + (Enter(Leaf(Heading)), "#"), + (Inline, "a"), + (Exit(Leaf(Heading)), "#"), + (Atom(Blankline), "\n"), + (Enter(Container(Section)), "##"), + (Enter(Leaf(Heading)), "##"), + (Inline, "aa"), + (Exit(Leaf(Heading)), "##"), + (Atom(Blankline), "\n"), + (Enter(Container(Section)), "####"), + (Enter(Leaf(Heading)), "####"), + (Inline, "aaaa"), + (Exit(Leaf(Heading)), "####"), + (Atom(Blankline), "\n"), + (Exit(Container(Section)), "####"), + (Exit(Container(Section)), "##"), + (Enter(Container(Section)), "##"), + (Enter(Leaf(Heading)), "##"), + (Inline, "ab"), + (Exit(Leaf(Heading)), "##"), + (Atom(Blankline), "\n"), + (Enter(Container(Section)), "###"), + (Enter(Leaf(Heading)), "###"), + (Inline, "aba"), + (Exit(Leaf(Heading)), "###"), + (Atom(Blankline), "\n"), + (Exit(Container(Section)), "###"), + (Exit(Container(Section)), "##"), + (Exit(Container(Section)), "#"), + (Enter(Container(Section)), "#"), + (Enter(Leaf(Heading)), "#"), + (Inline, "b"), + (Exit(Leaf(Heading)), "#"), + (Exit(Container(Section)), "#"), + ); + } + #[test] fn parse_blockquote() { test_parse!(