From 329207689bee1bff326ae7dda968f71755430786 Mon Sep 17 00:00:00 2001 From: Noah Hellman Date: Mon, 8 May 2023 21:19:47 +0200 Subject: [PATCH] prepass: fix lookup of headings bug caused by confusing binary_search_by_key to be element instead of index, both of which were usize --- src/lib.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3f1bb62..c30bbd8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -734,7 +734,7 @@ impl<'s> PrePass<'s> { self.headings_lex .binary_search_by_key(&tag, |i| &self.headings[*i].text) .ok() - .map(|i| self.heading_id(i)) + .map(|i| self.heading_id(self.headings_lex[i])) } } @@ -1204,6 +1204,65 @@ mod test { id: "Some-Section".into() }), ); + test_parse!( + concat!( + "[a][]\n", // + "[b][]\n", // + "\n", // + "# b\n", // + "\n", // + "# a\n", // + ), + Start(Paragraph, Attributes::new()), + Start( + Link("#a".into(), LinkType::Span(SpanLinkType::Reference)), + Attributes::new() + ), + Str("a".into()), + End(Link("#a".into(), LinkType::Span(SpanLinkType::Reference))), + Softbreak, + Start( + Link("#b".into(), LinkType::Span(SpanLinkType::Reference)), + Attributes::new() + ), + Str("b".into()), + End(Link("#b".into(), LinkType::Span(SpanLinkType::Reference))), + End(Paragraph), + Blankline, + Start(Section { id: "b".into() }, Attributes::new()), + Start( + Heading { + level: 1, + has_section: true, + id: "b".into(), + }, + Attributes::new(), + ), + Str("b".into()), + End(Heading { + level: 1, + has_section: true, + id: "b".into(), + }), + Blankline, + End(Section { id: "b".into() }), + Start(Section { id: "a".into() }, Attributes::new()), + Start( + Heading { + level: 1, + has_section: true, + id: "a".into(), + }, + Attributes::new(), + ), + Str("a".into()), + End(Heading { + level: 1, + has_section: true, + id: "a".into(), + }), + End(Section { id: "a".into() }), + ); } #[test]