diff --git a/dist/showdown.js b/dist/showdown.js index dd24910..2ef0d1b 100644 Binary files a/dist/showdown.js and b/dist/showdown.js differ diff --git a/dist/showdown.js.map b/dist/showdown.js.map index c7fb291..b5388a9 100644 Binary files a/dist/showdown.js.map and b/dist/showdown.js.map differ diff --git a/dist/showdown.min.js b/dist/showdown.min.js index 1fd5960..771109d 100644 Binary files a/dist/showdown.min.js and b/dist/showdown.min.js differ diff --git a/dist/showdown.min.js.map b/dist/showdown.min.js.map index be86859..4d91466 100644 Binary files a/dist/showdown.min.js.map and b/dist/showdown.min.js.map differ diff --git a/src/converter.js b/src/converter.js index 1003e98..57d0a23 100644 --- a/src/converter.js +++ b/src/converter.js @@ -242,7 +242,8 @@ showdown.Converter = function (converterOptions) { hashLinkCounts: {}, langExtensions: langExtensions, outputModifiers: outputModifiers, - converter: this + converter: this, + ghCodeBlocks: [] }; // attacklab: Replace ~ with ~T diff --git a/src/subParsers/githubCodeBlocks.js b/src/subParsers/githubCodeBlocks.js index 79832c1..81eadf1 100644 --- a/src/subParsers/githubCodeBlocks.js +++ b/src/subParsers/githubCodeBlocks.js @@ -23,6 +23,7 @@ showdown.subParser('githubCodeBlocks', function (text, options, globals) { text = text.replace(/(?:^|\n)```(.*)\n([\s\S]*?)\n```/g, function (wholeMatch, language, codeblock) { var end = (options.omitExtraWLInCodeBlocks) ? '' : '\n'; + // First parse the github code block codeblock = showdown.subParser('encodeCode')(codeblock); codeblock = showdown.subParser('detab')(codeblock); codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines @@ -30,13 +31,16 @@ showdown.subParser('githubCodeBlocks', function (text, options, globals) { codeblock = '
' + codeblock + end + '
'; - return showdown.subParser('hashBlock')(codeblock, options, globals); + codeblock = showdown.subParser('hashBlock')(codeblock, options, globals); + + // Since GHCodeblocks can be false positives, we need to + // store the primitive text and the parsed text in a global var, + // and then return a token + return '\n\n~G' + (globals.ghCodeBlocks.push({text: wholeMatch, codeblock: codeblock}) - 1) + 'G\n\n'; }); // attacklab: strip sentinel text = text.replace(/~0/, ''); - text = globals.converter._dispatch('githubCodeBlocks.after', text, options); - - return text; + return globals.converter._dispatch('githubCodeBlocks.after', text, options); }); diff --git a/src/subParsers/paragraphs.js b/src/subParsers/paragraphs.js index f8b7ea5..8a2973e 100644 --- a/src/subParsers/paragraphs.js +++ b/src/subParsers/paragraphs.js @@ -15,11 +15,10 @@ showdown.subParser('paragraphs', function (text, options, globals) { for (var i = 0; i < end; i++) { var str = grafs[i]; - // if this is an HTML marker, copy it - if (str.search(/~K(\d+)K/g) >= 0) { + if (str.search(/~(K|G)(\d+)\1/g) >= 0) { grafsOut.push(str); - } else if (str.search(/\S/) >= 0) { + } else { str = showdown.subParser('spanGamut')(str, options, globals); str = str.replace(/^([ \t]*)/g, '

'); str += '

'; @@ -29,16 +28,37 @@ showdown.subParser('paragraphs', function (text, options, globals) { /** Unhashify HTML blocks */ end = grafsOut.length; + console.log(text); for (i = 0; i < end; i++) { - var blockText = ''; + var blockText = '', + grafsOutIt = grafsOut[i], + child = false, + codeFlag = false; // if this is a marker for an html block... - while (grafsOut[i].search(/~K(\d+)K/) >= 0) { - blockText = globals.gHtmlBlocks[RegExp.$1]; - blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs - grafsOut[i] = grafsOut[i].replace(/~K\d+K/, blockText); - } - } + while (grafsOutIt.search(/~(K|G)(\d+)\1/) >= 0) { + var delim = RegExp.$1, + num = RegExp.$2; - text = globals.converter._dispatch('paragraphs.after', text, options); - return grafsOut.join('\n\n'); + if (delim === 'K') { + 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; + } + blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs + + grafsOutIt = grafsOutIt.replace(/(\n\n)?~(K|G)\d+\2(\n\n)?/, blockText); + // Check if grafsOutIt is a pre->code + if (/^]*>\s*]*>/.test(grafsOutIt)) { + codeFlag = true; + } + child = true; + } + grafsOut[i] = grafsOutIt; + } + text = grafsOut.join('\n\n'); + // Strip leading and trailing lines: + text = text.replace(/^\n+/g, ''); + text = text.replace(/\n+$/g, ''); + return globals.converter._dispatch('paragraphs.after', text, options); }); diff --git a/test/cases/list-with-code.html b/test/cases/list-with-code.html index a4c4b35..4fb69c5 100644 --- a/test/cases/list-with-code.html +++ b/test/cases/list-with-code.html @@ -2,6 +2,5 @@
  • A list item with code:

    alert('Hello world!');
    -    
    -
  • + diff --git a/test/issues/#183.gh-code-blocks-within-lists-do-not-render-properly.html b/test/issues/#183.gh-code-blocks-within-lists-do-not-render-properly.html index 97b4432..9359dec 100644 --- a/test/issues/#183.gh-code-blocks-within-lists-do-not-render-properly.html +++ b/test/issues/#183.gh-code-blocks-within-lists-do-not-render-properly.html @@ -5,14 +5,12 @@
    $ git clone thing.git
     
     dfgdfg
    -        
    - +
  • I am another thing!

    $ git clone other-thing.git
     
     foobar
    -        
    -
  • + diff --git a/test/issues/#229.2.code-being-parsed-inside-HTML-code-tags.html b/test/issues/#229.2.code-being-parsed-inside-HTML-code-tags.html new file mode 100644 index 0000000..4f53c72 --- /dev/null +++ b/test/issues/#229.2.code-being-parsed-inside-HTML-code-tags.html @@ -0,0 +1,25 @@ +
    
    +    foo
    +
    +    ```javascript
    +    var s = "JavaScript syntax highlighting";
    +    alert(s);
    +    ```
    +
    +    bar
    +
    + +

    this is a long paragraph

    + +

    this is another long paragraph

    + +
    ```javascript
    +var s = "JavaScript syntax highlighting";
    +alert(s);
    +```
    +
    +```python
    +s = "Python syntax highlighting"
    +print s
    +```
    +
    diff --git a/test/issues/#229.2.code-being-parsed-inside-HTML-code-tags.md b/test/issues/#229.2.code-being-parsed-inside-HTML-code-tags.md new file mode 100644 index 0000000..d234cf0 --- /dev/null +++ b/test/issues/#229.2.code-being-parsed-inside-HTML-code-tags.md @@ -0,0 +1,25 @@ +
    
    +foo
    +
    +```javascript
    +var s = "JavaScript syntax highlighting";
    +alert(s);
    +```
    +
    +bar
    +
    + +this is a long paragraph + +this is another long paragraph + +
    ```javascript
    +var s = "JavaScript syntax highlighting";
    +alert(s);
    +```
    +
    +```python
    +s = "Python syntax highlighting"
    +print s
    +```
    +
    diff --git a/test/issues/#229.code-being-parsed-inside-HTML-code-tags.html b/test/issues/#229.code-being-parsed-inside-HTML-code-tags.html new file mode 100644 index 0000000..5a148ce --- /dev/null +++ b/test/issues/#229.code-being-parsed-inside-HTML-code-tags.html @@ -0,0 +1,16 @@ +
    
    +```javascript
    +var s = "JavaScript syntax highlighting";
    +alert(s);
    +```
    +
    +```python
    +s = "Python syntax highlighting"
    +print s
    +```
    +
    +```
    +No language indicated, so no syntax highlighting.
    +But let's throw in a tag.
    +```
    +
    diff --git a/test/issues/#229.code-being-parsed-inside-HTML-code-tags.md b/test/issues/#229.code-being-parsed-inside-HTML-code-tags.md new file mode 100644 index 0000000..5a148ce --- /dev/null +++ b/test/issues/#229.code-being-parsed-inside-HTML-code-tags.md @@ -0,0 +1,16 @@ +
    
    +```javascript
    +var s = "JavaScript syntax highlighting";
    +alert(s);
    +```
    +
    +```python
    +s = "Python syntax highlighting"
    +print s
    +```
    +
    +```
    +No language indicated, so no syntax highlighting.
    +But let's throw in a tag.
    +```
    +
    diff --git a/test/karlcow/list-code.html b/test/karlcow/list-code.html index 26f0be5..938f671 100644 --- a/test/karlcow/list-code.html +++ b/test/karlcow/list-code.html @@ -1,8 +1,8 @@ \ No newline at end of file