From b150a72975d6e75b96298b3d405afe070271d78b Mon Sep 17 00:00:00 2001
From: Titus Wormer <tituswormer@gmail.com>
Date: Wed, 15 Jun 2022 19:20:43 +0200
Subject: Add support for text in heading (atx)

---
 readme.md                    |  2 +-
 src/compiler.rs              | 10 ++++++----
 src/construct/heading_atx.rs |  2 ++
 tests/heading_atx.rs         | 32 +++++++++++++++-----------------
 4 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/readme.md b/readme.md
index 07b224e..a17e603 100644
--- a/readme.md
+++ b/readme.md
@@ -68,7 +68,6 @@ cargo doc --document-private-items
 
 ### Small things
 
-- [ ] (1) text in heading
 - [ ] (1) Add docs to subtokenize
 - [ ] (1) Add module docs to content
 - [ ] (1) Add module docs to parser
@@ -167,6 +166,7 @@ cargo doc --document-private-items
 - [x] (1) Reorganize to split util
 - [x] (1) Add examples to `CompileOptions` docs
 - [x] (3) Fix deep subtokenization
+- [x] (1) text in heading
 
 ### Extensions
 
diff --git a/src/compiler.rs b/src/compiler.rs
index d3d935b..6127231 100644
--- a/src/compiler.rs
+++ b/src/compiler.rs
@@ -117,7 +117,9 @@ pub fn compile(events: &[Event], codes: &[Code], options: &CompileOptions) -> St
                     buf_tail_mut(buffers).push("<pre><code".to_string());
                     code_fenced_fences_count = Some(0);
                 }
-                TokenType::CodeFencedFenceInfo | TokenType::CodeFencedFenceMeta => {
+                TokenType::AtxHeadingText
+                | TokenType::CodeFencedFenceInfo
+                | TokenType::CodeFencedFenceMeta => {
                     buffer(buffers);
                 }
                 TokenType::HtmlFlow => {
@@ -139,7 +141,6 @@ pub fn compile(events: &[Event], codes: &[Code], options: &CompileOptions) -> St
                 | TokenType::AtxHeading
                 | TokenType::AtxHeadingSequence
                 | TokenType::AtxHeadingWhitespace
-                | TokenType::AtxHeadingText
                 | TokenType::LineEnding
                 | TokenType::ThematicBreak
                 | TokenType::ThematicBreakSequence
@@ -297,6 +298,8 @@ pub fn compile(events: &[Event], codes: &[Code], options: &CompileOptions) -> St
                     }
                 }
                 TokenType::AtxHeadingText => {
+                    let result = resume(buffers);
+
                     if let Some(ref buf) = atx_heading_buffer {
                         if !buf.is_empty() {
                             buf_tail_mut(buffers).push(encode(buf));
@@ -306,8 +309,7 @@ pub fn compile(events: &[Event], codes: &[Code], options: &CompileOptions) -> St
                         atx_heading_buffer = Some("".to_string());
                     }
 
-                    let slice = encode(&serialize(codes, &from_exit_event(events, index), false));
-                    buf_tail_mut(buffers).push(slice);
+                    buf_tail_mut(buffers).push(encode(&result));
                 }
                 TokenType::AtxHeading => {
                     let rank = atx_opening_sequence_size
diff --git a/src/construct/heading_atx.rs b/src/construct/heading_atx.rs
index 3be8c99..1a9ed03 100644
--- a/src/construct/heading_atx.rs
+++ b/src/construct/heading_atx.rs
@@ -117,6 +117,7 @@ fn at_break(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult {
         }
         Code::Char(_) => {
             tokenizer.enter(TokenType::AtxHeadingText);
+            tokenizer.enter(TokenType::ChunkText);
             data(tokenizer, code)
         }
     }
@@ -165,6 +166,7 @@ fn data(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult {
     match code {
         // Note: `#` for closing sequence must be preceded by whitespace, otherwise it’s just text.
         Code::None | Code::CarriageReturnLineFeed | Code::Char('\t' | '\n' | '\r' | ' ') => {
+            tokenizer.exit(TokenType::ChunkText);
             tokenizer.exit(TokenType::AtxHeadingText);
             at_break(tokenizer, code)
         }
diff --git a/tests/heading_atx.rs b/tests/heading_atx.rs
index 20da860..67351ec 100644
--- a/tests/heading_atx.rs
+++ b/tests/heading_atx.rs
@@ -148,24 +148,23 @@ fn heading_atx() {
         "should not support a closing sequence w/o whitespace before it"
     );
 
-    // Phrasing.
-    // assert_eq!(
-    //     micromark("### foo \\###"),
-    //     "<h3>foo ###</h3>",
-    //     "should not support an “escaped” closing sequence (1)"
-    // );
+    assert_eq!(
+        micromark("### foo \\###"),
+        "<h3>foo ###</h3>",
+        "should not support an “escaped” closing sequence (1)"
+    );
 
-    // assert_eq!(
-    //     micromark("## foo #\\##"),
-    //     "<h2>foo ###</h2>",
-    //     "should not support an “escaped” closing sequence (2)"
-    // );
+    assert_eq!(
+        micromark("## foo #\\##"),
+        "<h2>foo ###</h2>",
+        "should not support an “escaped” closing sequence (2)"
+    );
 
-    // assert_eq!(
-    //     micromark("# foo \\#"),
-    //     "<h1>foo #</h1>",
-    //     "should not support an “escaped” closing sequence (3)"
-    // );
+    assert_eq!(
+        micromark("# foo \\#"),
+        "<h1>foo #</h1>",
+        "should not support an “escaped” closing sequence (3)"
+    );
 
     assert_eq!(
         micromark("****\n## foo\n****"),
@@ -179,7 +178,6 @@ fn heading_atx() {
         "should support atx headings interrupting paragraphs"
     );
 
-    // Line endings.
     assert_eq!(
         micromark("## \n#\n### ###"),
         "<h2></h2>\n<h1></h1>\n<h3></h3>",
-- 
cgit