Bladeren bron

fix(HTMLParser): fix code tags parsing

Closes #231
Estevão Soares dos Santos 9 jaren geleden
bovenliggende
commit
71a5873902

+ 23 - 3
dist/showdown.js

@@ -963,6 +963,7 @@ showdown.Converter = function (converterOptions) {
     });
 
     // run the sub parsers
+    text = showdown.subParser('hashPreCodeTags')(text, options, globals);
     text = showdown.subParser('githubCodeBlocks')(text, options, globals);
     text = showdown.subParser('hashHTMLBlocks')(text, options, globals);
     text = showdown.subParser('hashHTMLSpans')(text, options, globals);
@@ -1772,6 +1773,22 @@ showdown.subParser('unhashHTMLSpans', function (text, config, globals) {
   return text;
 });
 
+/**
+ * Hash span elements that should not be parsed as markdown
+ */
+showdown.subParser('hashPreCodeTags', function (text, config, globals) {
+  'use strict';
+
+  var repFunc = function (wholeMatch, match, left, right) {
+    // encode html entities
+    var codeblock = left + showdown.subParser('encodeCode')(match) + right;
+    return '\n\n~G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: codeblock}) - 1) + 'G\n\n';
+  };
+
+  text = showdown.helper.replaceRecursiveRegExp(text, repFunc, '^(?: |\\t){0,3}<pre\\b[^>]*>\\s*<code\\b[^>]*>', '^(?: |\\t){0,3}</code>\\s*</pre>', 'gim');
+  return text;
+});
+
 showdown.subParser('headers', function (text, options, globals) {
   'use strict';
 
@@ -2164,7 +2181,6 @@ showdown.subParser('paragraphs', function (text, options, globals) {
   for (i = 0; i < end; i++) {
     var blockText = '',
         grafsOutIt = grafsOut[i],
-        child = false,
         codeFlag = false;
     // if this is a marker for an html block...
     while (grafsOutIt.search(/~(K|G)(\d+)\1/) >= 0) {
@@ -2175,7 +2191,12 @@ showdown.subParser('paragraphs', function (text, options, globals) {
         blockText = globals.gHtmlBlocks[num];
       } else {
         // we need to check if ghBlock is a false positive
-        blockText = (codeFlag) ? globals.ghCodeBlocks[num].text : globals.ghCodeBlocks[num].codeblock;
+        if (codeFlag) {
+          // use encoded version of all text
+          blockText = showdown.subParser('encodeCode')(globals.ghCodeBlocks[num].text);
+        } else {
+          blockText = globals.ghCodeBlocks[num].codeblock;
+        }
       }
       blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs
 
@@ -2184,7 +2205,6 @@ showdown.subParser('paragraphs', function (text, options, globals) {
       if (/^<pre\b[^>]*>\s*<code\b[^>]*>/.test(grafsOutIt)) {
         codeFlag = true;
       }
-      child = true;
     }
     grafsOut[i] = grafsOutIt;
   }

File diff suppressed because it is too large
+ 0 - 0
dist/showdown.js.map


File diff suppressed because it is too large
+ 0 - 0
dist/showdown.min.js


File diff suppressed because it is too large
+ 0 - 0
dist/showdown.min.js.map


+ 1 - 0
src/converter.js

@@ -276,6 +276,7 @@ showdown.Converter = function (converterOptions) {
     });
 
     // run the sub parsers
+    text = showdown.subParser('hashPreCodeTags')(text, options, globals);
     text = showdown.subParser('githubCodeBlocks')(text, options, globals);
     text = showdown.subParser('hashHTMLBlocks')(text, options, globals);
     text = showdown.subParser('hashHTMLSpans')(text, options, globals);

+ 15 - 0
src/subParsers/hashPreCodeTags.js

@@ -0,0 +1,15 @@
+/**
+ * Hash span elements that should not be parsed as markdown
+ */
+showdown.subParser('hashPreCodeTags', function (text, config, globals) {
+  'use strict';
+
+  var repFunc = function (wholeMatch, match, left, right) {
+    // encode html entities
+    var codeblock = left + showdown.subParser('encodeCode')(match) + right;
+    return '\n\n~G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: codeblock}) - 1) + 'G\n\n';
+  };
+
+  text = showdown.helper.replaceRecursiveRegExp(text, repFunc, '^(?: |\\t){0,3}<pre\\b[^>]*>\\s*<code\\b[^>]*>', '^(?: |\\t){0,3}</code>\\s*</pre>', 'gim');
+  return text;
+});

+ 6 - 3
src/subParsers/paragraphs.js

@@ -31,7 +31,6 @@ showdown.subParser('paragraphs', function (text, options, globals) {
   for (i = 0; i < end; i++) {
     var blockText = '',
         grafsOutIt = grafsOut[i],
-        child = false,
         codeFlag = false;
     // if this is a marker for an html block...
     while (grafsOutIt.search(/~(K|G)(\d+)\1/) >= 0) {
@@ -42,7 +41,12 @@ showdown.subParser('paragraphs', function (text, options, globals) {
         blockText = globals.gHtmlBlocks[num];
       } else {
         // we need to check if ghBlock is a false positive
-        blockText = (codeFlag) ? globals.ghCodeBlocks[num].text : globals.ghCodeBlocks[num].codeblock;
+        if (codeFlag) {
+          // use encoded version of all text
+          blockText = showdown.subParser('encodeCode')(globals.ghCodeBlocks[num].text);
+        } else {
+          blockText = globals.ghCodeBlocks[num].codeblock;
+        }
       }
       blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs
 
@@ -51,7 +55,6 @@ showdown.subParser('paragraphs', function (text, options, globals) {
       if (/^<pre\b[^>]*>\s*<code\b[^>]*>/.test(grafsOutIt)) {
         codeFlag = true;
       }
-      child = true;
     }
     grafsOut[i] = grafsOutIt;
   }

+ 7 - 0
test/cases/pre-code-tags-inside-code-block.html

@@ -0,0 +1,7 @@
+<p>code inception</p>
+
+<pre><code>&lt;pre&gt;&lt;code&gt;
+&lt;div&gt;some html code inside code html tags inside a fenced code block&lt;/div&gt;
+&lt;/code&gt;&lt;/pre&gt;
+</code></pre>
+

+ 8 - 0
test/cases/pre-code-tags-inside-code-block.md

@@ -0,0 +1,8 @@
+code inception
+
+```
+<pre><code>
+<div>some html code inside code html tags inside a fenced code block</div>
+</code></pre>
+```
+

+ 16 - 0
test/cases/pre-code-tags.html

@@ -0,0 +1,16 @@
+<pre>
+<code>
+foobar
+</code>
+</pre>
+
+<p>blabla</p>
+
+<pre nhaca="zulu"><code bla="bla">
+foobar
+</code>
+</pre>
+
+<pre><code>
+&lt;div&gt;some html code&lt;/div&gt;
+</code></pre>

+ 17 - 0
test/cases/pre-code-tags.md

@@ -0,0 +1,17 @@
+<pre>
+<code>
+foobar
+</code>
+</pre>
+
+blabla
+
+<pre nhaca="zulu"><code bla="bla">
+foobar
+</code>
+</pre>
+
+<pre><code>
+<div>some html code</div>
+</code></pre>
+

+ 6 - 6
test/issues/#229.2.code-being-parsed-inside-HTML-code-tags.html

@@ -1,12 +1,12 @@
 <pre lang="no-highlight"><code>
-    foo
+foo
 
-    ```javascript
-    var s = "JavaScript syntax highlighting";
-    alert(s);
-    ```
+```javascript
+var s = "JavaScript syntax highlighting";
+alert(s);
+```
 
-    bar
+bar
 </code></pre>
 
 <p>this is a long paragraph</p>

+ 1 - 1
test/issues/#229.code-being-parsed-inside-HTML-code-tags.html

@@ -11,6 +11,6 @@ print s
 
 ```
 No language indicated, so no syntax highlighting.
-But let's throw in a <b>tag</b>.
+But let's throw in a &lt;b&gt;tag&lt;/b&gt;.
 ```
 </code></pre>

+ 12 - 0
test/issues/#230.paragraphs-are-ignored-between-code-tags.html

@@ -0,0 +1,12 @@
+<pre lang="no-highlight"><code>```python
+var s;
+```
+</code></pre>
+
+<p>this is a long paragraph</p>
+
+<pre lang="no-highlight"><code>
+```javascript
+var s;
+```
+</code></pre>

+ 12 - 0
test/issues/#230.paragraphs-are-ignored-between-code-tags.md

@@ -0,0 +1,12 @@
+<pre lang="no-highlight"><code>```python
+var s;
+```
+</code></pre>
+
+this is a long paragraph
+
+<pre lang="no-highlight"><code>
+```javascript
+var s;
+```
+</code></pre>

Some files were not shown because too many files changed in this diff