diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-04-02 16:55:37 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-12-22 15:00:44 +0100 |
commit | d32e72acd2ffe36488c2f6b458725999149ca6ca (patch) | |
tree | d114adce55230297e6c5aa9ee07f5482a8a81f2e /packages/gitbook-markdown | |
parent | d9bbc2fe0a280d05795594e49c22413a0b5b71c4 (diff) | |
download | gitbook-d32e72acd2ffe36488c2f6b458725999149ca6ca.zip gitbook-d32e72acd2ffe36488c2f6b458725999149ca6ca.tar.gz gitbook-d32e72acd2ffe36488c2f6b458725999149ca6ca.tar.bz2 |
Improve escaping for code blocks
Diffstat (limited to 'packages/gitbook-markdown')
-rw-r--r-- | packages/gitbook-markdown/lib/annotate_blocks.js | 2 | ||||
-rw-r--r-- | packages/gitbook-markdown/lib/annotate_inline.js | 2 | ||||
-rw-r--r-- | packages/gitbook-markdown/lib/page.js | 28 | ||||
-rw-r--r-- | packages/gitbook-markdown/test/page.js | 4 |
4 files changed, 18 insertions, 18 deletions
diff --git a/packages/gitbook-markdown/lib/annotate_blocks.js b/packages/gitbook-markdown/lib/annotate_blocks.js index 88653ad..cb8ada8 100644 --- a/packages/gitbook-markdown/lib/annotate_blocks.js +++ b/packages/gitbook-markdown/lib/annotate_blocks.js @@ -27,7 +27,7 @@ var rules = { newline: /^\n+/, // List of all the regexes we want to run var ruleTypes = [ -'newline', 'rawStart', 'rawEnd', 'code', 'fences', 'footnote', 'math', 'heading', +'rawStart', 'rawEnd', 'newline', '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 649cec1..376b092 100644 --- a/packages/gitbook-markdown/lib/annotate_inline.js +++ b/packages/gitbook-markdown/lib/annotate_inline.js @@ -18,7 +18,7 @@ var rules = { text: /^[\s\S]+?(?=[\\<!\[_*`$~]|https?:\/\/| {2,}\n|$)/, math: /^\$\$\s*([\s\S]*?[^\$])\s*\$\$(?!\$)/, rawStart: /^{%([\s]*)raw([\s]*)%}/, - rawEnd: /^{%([\s]*)endraw([\s]*)%}/ + rawEnd: /[\s\S]*{%([\s]*)endraw([\s]*)%}/ // /^{%([\s]*)endraw([\s]*)%}/ //_inside: /(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/, //_href: /\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/ }; diff --git a/packages/gitbook-markdown/lib/page.js b/packages/gitbook-markdown/lib/page.js index 9b0f37f..1cd55a8 100644 --- a/packages/gitbook-markdown/lib/page.js +++ b/packages/gitbook-markdown/lib/page.js @@ -15,8 +15,6 @@ function combine(nodes) { return _.pluck(nodes, 'raw').join(''); } - - function preparePage(src) { var lexed = annotate.blocks(src); var levelRaw = 0; @@ -26,31 +24,29 @@ function preparePage(src) { el.raw = escape(el.raw); } else if (el.type == 'rawStart') { levelRaw = levelRaw + 1; - } else if (el.type == 'rawStart') { - levelRaw = levelRaw - 1; + } else if (el.type == 'rawEnd') { + levelRaw = 0; } return el; }; var escaped = lexed - // Escape code blocks - .map(escapeCodeElement) - - // Escape inline code blocks .map(function(el) { // Only escape paragraphs and headings - if(!(el.type == 'paragraph' || el.type == 'heading')) { - return el; - } + if(el.type == 'paragraph' || el.type == 'heading') { + var line = annotate.inline(el.raw); - // Escape inline code blocks - var newInline = annotate.inline(el.raw).map(escapeCodeElement); + // Escape inline code blocks + line = line.map(escapeCodeElement); - // Change raw source code - el.raw = combine(newInline); + // Change raw source code + el.raw = combine(line); - return el; + return el; + } else { + return escapeCodeElement(el); + } }); return combine(escaped); diff --git a/packages/gitbook-markdown/test/page.js b/packages/gitbook-markdown/test/page.js index 6ed6d96..c7384b6 100644 --- a/packages/gitbook-markdown/test/page.js +++ b/packages/gitbook-markdown/test/page.js @@ -51,6 +51,10 @@ describe('Page parsing', function() { page.prepare('{% raw %}Hello {{ "Bonjour" }} ```test```{% endraw %}'), '{% raw %}Hello {{ "Bonjour" }} ```test```{% endraw %}' ); + assert.equal( + page.prepare('{% raw %}Hello {{ "Bonjour" }} {% raw %}{% endraw %}```test```'), + '{% raw %}Hello {{ "Bonjour" }} {% raw %}{% endraw %}{% raw %}```test```{% endraw %}' + ); }); it('should not process math', function() { |