Przeglądaj źródła

fix: fix link url with no closing parenthesis (#3664)

* fix: fix link url with no closing parenthesis

* add escape test

* add double escape
Tony Brix 4 miesięcy temu
rodzic
commit
72b63739f4

+ 5 - 0
src/Tokenizer.ts

@@ -636,6 +636,11 @@ export class _Tokenizer {
       } else {
         // find closing parenthesis
         const lastParenIndex = findClosingBracket(cap[2], '()');
+        if (lastParenIndex === -2) {
+          // more open parens than closed
+          return;
+        }
+
         if (lastParenIndex > -1) {
           const start = cap[0].indexOf('!') === 0 ? 5 : 4;
           const linkLen = start + cap[1].length + lastParenIndex;

+ 4 - 0
src/helpers.ts

@@ -141,5 +141,9 @@ export function findClosingBracket(str: string, b: string) {
       }
     }
   }
+  if (level > 0) {
+    return -2;
+  }
+
   return -1;
 }

+ 1 - 2
test/specs/commonmark/commonmark.0.31.2.json

@@ -3975,8 +3975,7 @@
     "example": 497,
     "start_line": 7666,
     "end_line": 7670,
-    "section": "Links",
-    "shouldFail": true
+    "section": "Links"
   },
   {
     "markdown": "[link](foo\\(and\\(bar\\))\n",

+ 1 - 2
test/specs/gfm/commonmark.0.31.2.json

@@ -3975,8 +3975,7 @@
     "example": 497,
     "start_line": 7666,
     "end_line": 7670,
-    "section": "Links",
-    "shouldFail": true
+    "section": "Links"
   },
   {
     "markdown": "[link](foo\\(and\\(bar\\))\n",

+ 7 - 0
test/specs/new/link_unbalanced.html

@@ -0,0 +1,7 @@
+<p>[link](foo(bar())</p>
+
+<p><a href="foo(bar()">link</a></p>
+
+<p><a href="foo(bar()">link</a></p>
+
+<p>[link](foo(bar\())</p>

+ 7 - 0
test/specs/new/link_unbalanced.md

@@ -0,0 +1,7 @@
+[link](foo(bar())
+
+[link](foo\(bar())
+
+[link](foo(bar\())
+
+[link](foo(bar\\())