block: store level in event

do not use span length as level
This commit is contained in:
Noah Hellman 2023-04-29 18:34:23 +02:00
parent 6abe9e44c7
commit bc4dd794bc
2 changed files with 187 additions and 30 deletions

View file

@ -58,7 +58,7 @@ pub enum Leaf {
/// Span is `#` characters. /// Span is `#` characters.
/// Each inline is a line. /// Each inline is a line.
Heading { has_section: bool }, Heading { level: u16, has_section: bool },
/// Span is empty. /// Span is empty.
DescriptionTerm, DescriptionTerm,
@ -941,7 +941,8 @@ impl Kind {
match self { match self {
Self::Atom(a) => Block::Atom(*a), Self::Atom(a) => Block::Atom(*a),
Self::Paragraph => Block::Leaf(Paragraph), Self::Paragraph => Block::Leaf(Paragraph),
Self::Heading { .. } => Block::Leaf(Heading { Self::Heading { level } => Block::Leaf(Heading {
level: (*level).try_into().unwrap(),
has_section: top_level, has_section: top_level,
}), }),
Self::Fenced { Self::Fenced {
@ -1087,13 +1088,37 @@ mod test {
"## b\n", // "## b\n", //
), ),
(Enter(Container(Section)), "#"), (Enter(Container(Section)), "#"),
(Enter(Leaf(Heading { has_section: true })), "#"), (
Enter(Leaf(Heading {
level: 1,
has_section: true
})),
"#"
),
(Inline, "a"), (Inline, "a"),
(Exit(Leaf(Heading { has_section: true })), "#"), (
Exit(Leaf(Heading {
level: 1,
has_section: true
})),
"#"
),
(Enter(Container(Section)), "##"), (Enter(Container(Section)), "##"),
(Enter(Leaf(Heading { has_section: true })), "##"), (
Enter(Leaf(Heading {
level: 2,
has_section: true
})),
"##"
),
(Inline, "b"), (Inline, "b"),
(Exit(Leaf(Heading { has_section: true })), "##"), (
Exit(Leaf(Heading {
level: 2,
has_section: true
})),
"##"
),
(Exit(Container(Section)), "##"), (Exit(Container(Section)), "##"),
(Exit(Container(Section)), "#"), (Exit(Container(Section)), "#"),
); );
@ -1107,9 +1132,21 @@ mod test {
"heading\n", // "heading\n", //
), ),
(Enter(Container(Section)), "#"), (Enter(Container(Section)), "#"),
(Enter(Leaf(Heading { has_section: true })), "#"), (
Enter(Leaf(Heading {
level: 1,
has_section: true
})),
"#"
),
(Inline, "heading"), (Inline, "heading"),
(Exit(Leaf(Heading { has_section: true })), "#"), (
Exit(Leaf(Heading {
level: 1,
has_section: true
})),
"#"
),
(Exit(Container(Section)), "#"), (Exit(Container(Section)), "#"),
); );
} }
@ -1125,17 +1162,41 @@ mod test {
"15\n", // "15\n", //
), ),
(Enter(Container(Section)), "#"), (Enter(Container(Section)), "#"),
(Enter(Leaf(Heading { has_section: true })), "#"), (
Enter(Leaf(Heading {
level: 1,
has_section: true
})),
"#"
),
(Inline, "2"), (Inline, "2"),
(Exit(Leaf(Heading { has_section: true })), "#"), (
Exit(Leaf(Heading {
level: 1,
has_section: true
})),
"#"
),
(Atom(Blankline), "\n"), (Atom(Blankline), "\n"),
(Exit(Container(Section)), "#"), (Exit(Container(Section)), "#"),
(Enter(Container(Section)), "#"), (Enter(Container(Section)), "#"),
(Enter(Leaf(Heading { has_section: true })), "#"), (
Enter(Leaf(Heading {
level: 1,
has_section: true
})),
"#"
),
(Inline, "8\n"), (Inline, "8\n"),
(Inline, "12\n"), (Inline, "12\n"),
(Inline, "15"), (Inline, "15"),
(Exit(Leaf(Heading { has_section: true })), "#"), (
Exit(Leaf(Heading {
level: 1,
has_section: true
})),
"#"
),
(Exit(Container(Section)), "#"), (Exit(Container(Section)), "#"),
); );
} }
@ -1149,11 +1210,23 @@ mod test {
"c\n", // "c\n", //
), ),
(Enter(Container(Section)), "#"), (Enter(Container(Section)), "#"),
(Enter(Leaf(Heading { has_section: true })), "#"), (
Enter(Leaf(Heading {
level: 1,
has_section: true
})),
"#"
),
(Inline, "a\n"), (Inline, "a\n"),
(Inline, "b\n"), (Inline, "b\n"),
(Inline, "c"), (Inline, "c"),
(Exit(Leaf(Heading { has_section: true })), "#"), (
Exit(Leaf(Heading {
level: 1,
has_section: true
})),
"#"
),
(Exit(Container(Section)), "#"), (Exit(Container(Section)), "#"),
); );
} }
@ -1175,39 +1248,111 @@ mod test {
"# b\n", "# b\n",
), ),
(Enter(Container(Section)), "#"), (Enter(Container(Section)), "#"),
(Enter(Leaf(Heading { has_section: true })), "#"), (
Enter(Leaf(Heading {
level: 1,
has_section: true
})),
"#"
),
(Inline, "a"), (Inline, "a"),
(Exit(Leaf(Heading { has_section: true })), "#"), (
Exit(Leaf(Heading {
level: 1,
has_section: true
})),
"#"
),
(Atom(Blankline), "\n"), (Atom(Blankline), "\n"),
(Enter(Container(Section)), "##"), (Enter(Container(Section)), "##"),
(Enter(Leaf(Heading { has_section: true })), "##"), (
Enter(Leaf(Heading {
level: 2,
has_section: true
})),
"##"
),
(Inline, "aa"), (Inline, "aa"),
(Exit(Leaf(Heading { has_section: true })), "##"), (
Exit(Leaf(Heading {
level: 2,
has_section: true
})),
"##"
),
(Atom(Blankline), "\n"), (Atom(Blankline), "\n"),
(Enter(Container(Section)), "####"), (Enter(Container(Section)), "####"),
(Enter(Leaf(Heading { has_section: true })), "####"), (
Enter(Leaf(Heading {
level: 4,
has_section: true
})),
"####"
),
(Inline, "aaaa"), (Inline, "aaaa"),
(Exit(Leaf(Heading { has_section: true })), "####"), (
Exit(Leaf(Heading {
level: 4,
has_section: true
})),
"####"
),
(Atom(Blankline), "\n"), (Atom(Blankline), "\n"),
(Exit(Container(Section)), "####"), (Exit(Container(Section)), "####"),
(Exit(Container(Section)), "##"), (Exit(Container(Section)), "##"),
(Enter(Container(Section)), "##"), (Enter(Container(Section)), "##"),
(Enter(Leaf(Heading { has_section: true })), "##"), (
Enter(Leaf(Heading {
level: 2,
has_section: true
})),
"##"
),
(Inline, "ab"), (Inline, "ab"),
(Exit(Leaf(Heading { has_section: true })), "##"), (
Exit(Leaf(Heading {
level: 2,
has_section: true
})),
"##"
),
(Atom(Blankline), "\n"), (Atom(Blankline), "\n"),
(Enter(Container(Section)), "###"), (Enter(Container(Section)), "###"),
(Enter(Leaf(Heading { has_section: true })), "###"), (
Enter(Leaf(Heading {
level: 3,
has_section: true
})),
"###"
),
(Inline, "aba"), (Inline, "aba"),
(Exit(Leaf(Heading { has_section: true })), "###"), (
Exit(Leaf(Heading {
level: 3,
has_section: true
})),
"###"
),
(Atom(Blankline), "\n"), (Atom(Blankline), "\n"),
(Exit(Container(Section)), "###"), (Exit(Container(Section)), "###"),
(Exit(Container(Section)), "##"), (Exit(Container(Section)), "##"),
(Exit(Container(Section)), "#"), (Exit(Container(Section)), "#"),
(Enter(Container(Section)), "#"), (Enter(Container(Section)), "#"),
(Enter(Leaf(Heading { has_section: true })), "#"), (
Enter(Leaf(Heading {
level: 1,
has_section: true
})),
"#"
),
(Inline, "b"), (Inline, "b"),
(Exit(Leaf(Heading { has_section: true })), "#"), (
Exit(Leaf(Heading {
level: 1,
has_section: true,
})),
"#"
),
(Exit(Container(Section)), "#"), (Exit(Container(Section)), "#"),
); );
} }
@ -1245,9 +1390,21 @@ mod test {
(Inline, "a"), (Inline, "a"),
(Exit(Leaf(Paragraph)), ""), (Exit(Leaf(Paragraph)), ""),
(Atom(Blankline), "\n"), (Atom(Blankline), "\n"),
(Enter(Leaf(Heading { has_section: false })), "##"), (
Enter(Leaf(Heading {
level: 2,
has_section: false,
})),
"##"
),
(Inline, "hl"), (Inline, "hl"),
(Exit(Leaf(Heading { has_section: false })), "##"), (
Exit(Leaf(Heading {
level: 2,
has_section: false,
})),
"##"
),
(Atom(Blankline), "\n"), (Atom(Blankline), "\n"),
(Enter(Leaf(Paragraph)), ""), (Enter(Leaf(Paragraph)), ""),
(Inline, "para"), (Inline, "para"),

View file

@ -881,8 +881,8 @@ impl<'s> Parser<'s> {
self.inline_parser.reset(); self.inline_parser.reset();
match l { match l {
block::Leaf::Paragraph => Container::Paragraph, block::Leaf::Paragraph => Container::Paragraph,
block::Leaf::Heading { has_section } => Container::Heading { block::Leaf::Heading { level, has_section } => Container::Heading {
level: content.len().try_into().unwrap(), level,
has_section, has_section,
id: self id: self
.pre_pass .pre_pass