diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-04-02 15:01:49 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-12-22 15:00:44 +0100 |
commit | d9bbc2fe0a280d05795594e49c22413a0b5b71c4 (patch) | |
tree | 5bcbf1a33a75666a21c89a5b646d9b55eec6258f | |
parent | e572570c43beee7b2df5aa562662290010bf2ab7 (diff) | |
download | gitbook-d9bbc2fe0a280d05795594e49c22413a0b5b71c4.zip gitbook-d9bbc2fe0a280d05795594e49c22413a0b5b71c4.tar.gz gitbook-d9bbc2fe0a280d05795594e49c22413a0b5b71c4.tar.bz2 |
Don't escape code block if already in escaped block
-rw-r--r-- | packages/gitbook-markdown/lib/annotate_blocks.js | 4 | ||||
-rw-r--r-- | packages/gitbook-markdown/lib/annotate_inline.js | 4 | ||||
-rw-r--r-- | packages/gitbook-markdown/lib/page.js | 20 | ||||
-rw-r--r-- | packages/gitbook-markdown/test/page.js | 7 |
4 files changed, 27 insertions, 8 deletions
diff --git a/packages/gitbook-markdown/lib/annotate_blocks.js b/packages/gitbook-markdown/lib/annotate_blocks.js index d414e6d..88653ad 100644 --- a/packages/gitbook-markdown/lib/annotate_blocks.js +++ b/packages/gitbook-markdown/lib/annotate_blocks.js @@ -17,6 +17,8 @@ var rules = { newline: /^\n+/, paragraph: /^((?:[^\n]+\n?(?! *(`{3,}|~{3,}) *(\S+)? *\n([\s\S]+?)\s*\2 *(?:\n+|$)|( *)((?:[*+-]|\d+\.)) [\s\S]+?(?:\n+(?=\3?(?:[-*_] *){3,}(?:\n+|$))|\n+(?= *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$))|\n{2,}(?! )(?!\1(?:[*+-]|\d+\.) )\n*|\s*$)|( *[-*_]){3,} *(?:\n+|$)| *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)|([^\n]+)\n *(=|-){2,} *(?:\n+|$)|( *>[^\n]+(\n(?! *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$))[^\n]+)*\n*)+|<(?!(?:a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)\b)\w+(?!:\/|[^\w\s@]*@)\b| *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)| *(\${2,}) *([\s\S]+?)\s*\1 *(?:\n+|$)))+)\n*/, text: /^[^\n]+/, math: /^ *(\${2,}) *([\s\S]+?)\s*\1 *(?:\n+|$)/, + rawStart: /^{%([\s]*)raw([\s]*)%}/, + rawEnd: /^{%([\s]*)endraw([\s]*)%}/ // These are lower level, ignore them //bullet: /(?:[*+-]|\d+\.)/, //item: /^( *)((?:[*+-]|\d+\.)) [^\n]*(?:\n(?!\1(?:[*+-]|\d+\.) )[^\n]*)*/gm, @@ -25,7 +27,7 @@ var rules = { newline: /^\n+/, // List of all the regexes we want to run var ruleTypes = [ -'newline', 'code', 'fences', 'footnote', 'math', 'heading', +'newline', 'rawStart', 'rawEnd', 'code', 'fences', 'footnote', 'math', 'heading', 'nptable', 'lheading', 'hr', 'blockquote', 'list', 'html', 'def', 'table', 'paragraph', 'text', ]; diff --git a/packages/gitbook-markdown/lib/annotate_inline.js b/packages/gitbook-markdown/lib/annotate_inline.js index d2e8f89..649cec1 100644 --- a/packages/gitbook-markdown/lib/annotate_inline.js +++ b/packages/gitbook-markdown/lib/annotate_inline.js @@ -17,13 +17,15 @@ var rules = { del: /^~~(?=\S)([\s\S]*?\S)~~/, text: /^[\s\S]+?(?=[\\<!\[_*`$~]|https?:\/\/| {2,}\n|$)/, math: /^\$\$\s*([\s\S]*?[^\$])\s*\$\$(?!\$)/, + rawStart: /^{%([\s]*)raw([\s]*)%}/, + rawEnd: /^{%([\s]*)endraw([\s]*)%}/ //_inside: /(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/, //_href: /\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/ }; // List of all the regexes we want to run var ruleTypes = [ -'escape', 'autolink', 'url', 'tag', 'link', 'reflink', +'escape', 'rawStart', 'rawEnd', 'autolink', 'url', 'tag', 'link', 'reflink', 'nolink', 'reffn', 'strong', 'em', 'code', 'br', 'del', 'text', 'math' ]; diff --git a/packages/gitbook-markdown/lib/page.js b/packages/gitbook-markdown/lib/page.js index 742d8f9..9b0f37f 100644 --- a/packages/gitbook-markdown/lib/page.js +++ b/packages/gitbook-markdown/lib/page.js @@ -15,20 +15,28 @@ function combine(nodes) { return _.pluck(nodes, 'raw').join(''); } -function escapeCodeElement(el) { - if(el.type == 'code') { - el.raw = escape(el.raw); - } - return el; -} + function preparePage(src) { var lexed = annotate.blocks(src); + var levelRaw = 0; + var escapeCodeElement = function(el) { + if (el.type == 'code' && levelRaw == 0) { + el.raw = escape(el.raw); + } else if (el.type == 'rawStart') { + levelRaw = levelRaw + 1; + } else if (el.type == 'rawStart') { + levelRaw = levelRaw - 1; + } + return el; + }; var escaped = lexed + // Escape code blocks .map(escapeCodeElement) + // Escape inline code blocks .map(function(el) { // Only escape paragraphs and headings diff --git a/packages/gitbook-markdown/test/page.js b/packages/gitbook-markdown/test/page.js index 8e253f6..6ed6d96 100644 --- a/packages/gitbook-markdown/test/page.js +++ b/packages/gitbook-markdown/test/page.js @@ -46,6 +46,13 @@ describe('Page parsing', function() { ); }); + it('should escape codeblocks with nunjucks tags in {% raw %} tags', function() { + assert.equal( + page.prepare('{% raw %}Hello {{ "Bonjour" }} ```test```{% endraw %}'), + '{% raw %}Hello {{ "Bonjour" }} ```test```{% endraw %}' + ); + }); + it('should not process math', function() { assert.equal(page.prepare("Hello $world$"), "Hello $world$"); assert.equal(page.prepare("Hello $$world$$"), "Hello $$world$$"); |