diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-03-07 14:11:11 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-12-22 15:00:37 +0100 |
commit | 2516137d4e2b59b728f8562edb0e4c929aa6c5f8 (patch) | |
tree | 46fd9d11af80aa4036d684ad79419cdbfd40a5e6 /packages/gitbook-markdown | |
parent | aca50f4d1eea75c5035ae6451294a9c01d637880 (diff) | |
download | gitbook-2516137d4e2b59b728f8562edb0e4c929aa6c5f8.zip gitbook-2516137d4e2b59b728f8562edb0e4c929aa6c5f8.tar.gz gitbook-2516137d4e2b59b728f8562edb0e4c929aa6c5f8.tar.bz2 |
Fix code fences escaping (add more tests)
Diffstat (limited to 'packages/gitbook-markdown')
-rw-r--r-- | packages/gitbook-markdown/lib/page.js | 7 | ||||
-rw-r--r-- | packages/gitbook-markdown/test/page.js | 8 |
2 files changed, 11 insertions, 4 deletions
diff --git a/packages/gitbook-markdown/lib/page.js b/packages/gitbook-markdown/lib/page.js index 3a130ea..de0ed98 100644 --- a/packages/gitbook-markdown/lib/page.js +++ b/packages/gitbook-markdown/lib/page.js @@ -8,13 +8,14 @@ var RAW_START = "{% raw %}"; var RAW_END = "{% endraw %}"; var CODEBLOCKS = { md: /(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/, - fences: /`([^`]+)`/g + fences: /(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm }; function preparePage(src) { // GFM Fences - src = src.replace(CODEBLOCKS.fences, function(all) { - return RAW_START+all+RAW_END; + src = src.replace(CODEBLOCKS.fences, function(wholeMatch, m1, m2, m3, m4) { + wholeMatch = wholeMatch.slice(m1.length); + return m1+RAW_START+wholeMatch+RAW_END; }); // Normal codeblocks diff --git a/packages/gitbook-markdown/test/page.js b/packages/gitbook-markdown/test/page.js index 8486435..67c209d 100644 --- a/packages/gitbook-markdown/test/page.js +++ b/packages/gitbook-markdown/test/page.js @@ -20,8 +20,14 @@ describe('Page parsing', function() { assert(LEXED[0].content); }); - it('should escape codeblocks in preparation', function() { + it('should escape codeblocks in preparation (1)', function() { assert.equal(page.prepare("Hello `world`"), "Hello {% raw %}`world`{% endraw %}"); + assert.equal(page.prepare("Hello `world test`"), "Hello {% raw %}`world test`{% endraw %}"); + assert.equal(page.prepare("Hello ```world test```"), "Hello {% raw %}```world test```{% endraw %}"); + assert.equal(page.prepare("Hello\n```js\nworld test\n```\n"), "Hello\n{% raw %}```js\nworld test\n```{% endraw %}\n"); + }); + + it('should escape codeblocks in preparation (2)', function() { assert.equal( page.prepare("Hello\n\n\n\tworld\n\thello\n\n\ntest"), "Hello\n{% raw %}\n\n\tworld\n\thello\n\n\n{% endraw %}test" |