From 13cf914b5162fb12c842e983538b42d4b60cdcb8 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 1 Aug 2022 13:06:18 +0200 Subject: Fix bug on empty heading (atx) --- src/construct/heading_atx.rs | 60 +++++++++++++++++++++++--------------------- tests/heading_atx.rs | 8 +++++- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/construct/heading_atx.rs b/src/construct/heading_atx.rs index aa388ee..d432b6c 100644 --- a/src/construct/heading_atx.rs +++ b/src/construct/heading_atx.rs @@ -193,7 +193,7 @@ fn data(tokenizer: &mut Tokenizer) -> State { /// Resolve heading (atx). pub fn resolve(tokenizer: &mut Tokenizer) { let mut index = 0; - let mut heading_start: Option = None; + let mut heading_inside = false; let mut data_start: Option = None; let mut data_end: Option = None; @@ -202,41 +202,43 @@ pub fn resolve(tokenizer: &mut Tokenizer) { if event.token_type == Token::HeadingAtx { if event.event_type == EventType::Enter { - heading_start = Some(index); - } else if let Some(start) = data_start { - // If `start` is some, `end` is too. - let end = data_end.unwrap(); + heading_inside = true; + } else { + if let Some(start) = data_start { + // If `start` is some, `end` is too. + let end = data_end.unwrap(); - tokenizer.map.add( - start, - 0, - vec![Event { - event_type: EventType::Enter, - token_type: Token::HeadingAtxText, - point: tokenizer.events[start].point.clone(), - link: None, - }], - ); + tokenizer.map.add( + start, + 0, + vec![Event { + event_type: EventType::Enter, + token_type: Token::HeadingAtxText, + point: tokenizer.events[start].point.clone(), + link: None, + }], + ); - // Remove everything between the start and the end. - tokenizer.map.add(start + 1, end - start - 1, vec![]); + // Remove everything between the start and the end. + tokenizer.map.add(start + 1, end - start - 1, vec![]); - tokenizer.map.add( - end + 1, - 0, - vec![Event { - event_type: EventType::Exit, - token_type: Token::HeadingAtxText, - point: tokenizer.events[end].point.clone(), - link: None, - }], - ); + tokenizer.map.add( + end + 1, + 0, + vec![Event { + event_type: EventType::Exit, + token_type: Token::HeadingAtxText, + point: tokenizer.events[end].point.clone(), + link: None, + }], + ); + } - heading_start = None; + heading_inside = false; data_start = None; data_end = None; } - } else if heading_start.is_some() && event.token_type == Token::Data { + } else if heading_inside && event.token_type == Token::Data { if event.event_type == EventType::Enter { if data_start.is_none() { data_start = Some(index); diff --git a/tests/heading_atx.rs b/tests/heading_atx.rs index 22c9f82..f785ca6 100644 --- a/tests/heading_atx.rs +++ b/tests/heading_atx.rs @@ -179,7 +179,13 @@ fn heading_atx() { assert_eq!( micromark("## \n#\n### ###"), "

\n

\n

", - "should support empty atx headings" + "should support empty atx headings (1)" + ); + + assert_eq!( + micromark("#\na\n# b"), + "

\n

a

\n

b

", + "should support empty atx headings (2)" ); assert_eq!( -- cgit