Repository: raphlinus/pulldown-cmark (rust)

Issue:
[fuzz result] link definition destinations are being parsed with higher priority than setext headers
Consider this:

```markdown
[third try]:
-
```

[third try]:
-

pulldown-cmark thinks it's a link definition, but commonmark.js, commonmark-hs, and GitHub all think it's an H2

<details>

```rust
"[~]:\n-\n" -> [
]
```

Events from pandoc:

```rust
"[~]:\n-\n" -> [
  Start(Heading { level: H2, id: None, classes: [], attrs: [] })
    Text(Boxed("[~]:"))
  End(Heading(H2))
]
```

Events from commonmark.js:

```rust
"[~]:\n-\n" -> [
  Start(Heading { level: H2, id: None, classes: [], attrs: [] })
    Text(Boxed("[~]:"))
  End(Heading(H2))
]
```

</details>


Pull request:
Allow setext headers to interrupt link definitions
Fixes #779

Patch:
diff --git a/specs/regression.txt b/specs/regression.txt
index d68765b..ec36aa5 100644
--- a/specs/regression.txt
+++ b/specs/regression.txt
@@ -1703,6 +1703,19 @@ bar \
 <p>bar \</p>
 ````````````````````````````````
 
+ISSUE 779
+
+```````````````````````````````` example
+[third try]:
+-
+
+[third try]
+.
+<h2>[third try]:</h2>
+<p>[third try]</p>
+````````````````````````````````
+
+
 ISSUE 781
 
 ```````````````````````````````` example
diff --git a/src/firstpass.rs b/src/firstpass.rs
index f060621..bc2ccb3 100644
--- a/src/firstpass.rs
+++ b/src/firstpass.rs
@@ -1441,7 +1441,7 @@ impl<'a, 'b> FirstPass<'a, 'b> {
             ) == self.tree.spine_len();
             let bytes_scanned = line_start.bytes_scanned();
             let suffix = &bytes[i + bytes_scanned..];
-            if self.scan_paragraph_interrupt(suffix, current_container) {
+            if self.scan_paragraph_interrupt(suffix, current_container) || scan_setext_heading(suffix).is_some() {
                 return None;
             }
             i += bytes_scanned;


Test patch:
diff --git a/tests/suite/regression.rs b/tests/suite/regression.rs
index c4be75d..a19dea0 100644
--- a/tests/suite/regression.rs
+++ b/tests/suite/regression.rs
@@ -1995,6 +1995,20 @@ bar \
 
 #[test]
 fn regression_test_126() {
+    let original = r##"[third try]:
+-
+
+[third try]
+"##;
+    let expected = r##"<h2>[third try]:</h2>
+<p>[third try]</p>
+"##;
+  
+    test_markdown_html(original, expected, false, false, false);
+}
+  
+#[test]
+fn regression_test_127() {
     let original = r##"- [foo]: test
 bar
 
@@ -2014,7 +2028,7 @@ bar
 }
 
 #[test]
-fn regression_test_127() {
+fn regression_test_128() {
     let original = r##"> - [foo]: test
 > bar
 > 
@@ -2036,7 +2050,7 @@ fn regression_test_127() {
 }
 
 #[test]
-fn regression_test_128() {
+fn regression_test_129() {
     let original = r##"[bar]: test
 -
 "##;