summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-06-29 23:10:56 +0200
committerSamy Pessé <samypesse@gmail.com>2016-12-22 15:01:00 +0100
commita76a9d8dc376479128360999980847003c2eb95e (patch)
tree511689d98ab85ada257d5554d840b8a700107244
parentba25a60e913466cee035a63752380baba32229a6 (diff)
downloadgitbook-a76a9d8dc376479128360999980847003c2eb95e.zip
gitbook-a76a9d8dc376479128360999980847003c2eb95e.tar.gz
gitbook-a76a9d8dc376479128360999980847003c2eb95e.tar.bz2
Adapt annotating code blocks
-rw-r--r--packages/gitbook-markdown/lib/page.js49
-rw-r--r--packages/gitbook-markdown/test/page.js29
2 files changed, 43 insertions, 35 deletions
diff --git a/packages/gitbook-markdown/lib/page.js b/packages/gitbook-markdown/lib/page.js
index 12848ed..2cf0941 100644
--- a/packages/gitbook-markdown/lib/page.js
+++ b/packages/gitbook-markdown/lib/page.js
@@ -1,10 +1,10 @@
var _ = require('lodash');
var MarkupIt = require('markup-it');
-var gitbookSyntax = require('markup-it/syntaxes/gitbook');
+var gitbookSyntax = require('markup-it/syntaxes/markdown');
var RAW_START = '{% raw %}';
var RAW_END = '{% endraw %}';
-var gitbook = new MarkupIt(gitbookSyntax);
+var markdown = new MarkupIt(gitbookSyntax);
/**
* Escape a code block's content using raw blocks
@@ -26,31 +26,40 @@ function escape(str) {
*/
function preparePage(src) {
var levelRaw = 0;
+ var content = markdown.toContent(src, {
+ math: true,
+ template: true
+ });
- return gitbook.annotate(src, function(token) {
- var tokenType = token.getType();
-
- if (tokenType === MarkupIt.BLOCKS.TEMPLATE && tokenType === MarkupIt.STYLES.TEMPLATE) {
- var expr = token.getData().get('type');
- if (expr === 'raw') {
- levelRaw = levelRaw + 1;
- } else if (expr == 'endraw') {
- levelRaw = 0;
- }
-
- return true;
- }
+ var textMarkdown = markdown.toText(content, {
+ annotate: function(state, raw, token) {
+ var tokenType = token.getType();
- if (token.isBlock() && )
+ if (tokenType === MarkupIt.ENTITIES.TEMPLATE) {
+ var type = token.getData().get('type');
+ var expr = token.getAsPlainText();
+ if (type === 'expr') {
+ if (expr === 'raw') {
+ levelRaw = levelRaw + 1;
+ } else if (expr == 'endraw') {
+ levelRaw = 0;
+ }
+ }
+ }
+ if (
+ (tokenType === MarkupIt.BLOCKS.CODE || tokenType === MarkupIt.STYLES.CODE)
+ && levelRaw === 0
+ ) {
+ return escape(raw);
+ }
- if (tokenType !== MarkupIt.BLOCKS.CODE && tokenType !== MarkupIt.STYLES.CODE && levelRaw == 0) {
- return;
+ return raw;
}
-
- return escape(token.getRaw());
});
+
+ return textMarkdown;
}
module.exports = {
diff --git a/packages/gitbook-markdown/test/page.js b/packages/gitbook-markdown/test/page.js
index fac5ec4..a2e21d1 100644
--- a/packages/gitbook-markdown/test/page.js
+++ b/packages/gitbook-markdown/test/page.js
@@ -4,7 +4,6 @@ var assert = require('assert');
var page = require('../').page;
-
describe('Page parsing', function() {
var LEXED;
@@ -23,53 +22,53 @@ describe('Page parsing', 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```\n{% endraw %}");
- assert.equal(page.prepare("Hello\n```\ntest\n\tworld\n\ttest\n```"), "Hello\n{% raw %}```\ntest\n\tworld\n\ttest\n```{% endraw %}");
+ assert.equal(page.prepare("Hello `world`"), 'Hello {% raw %}`world`{% endraw %}\n\n');
+ assert.equal(page.prepare("Hello `world test`"), 'Hello {% raw %}`world test`{% endraw %}\n\n');
+ assert.equal(page.prepare("Hello ```world test```"), 'Hello {% raw %}`world test`{% endraw %}\n\n');
+ assert.equal(page.prepare("Hello\n```js\nworld test\n```\n"), 'Hello\n\n{% raw %}```js\nworld test\n```\n\n{% endraw %}');
+ assert.equal(page.prepare("Hello\n```\ntest\n\tworld\n\ttest\n```"), 'Hello\n\n{% raw %}```\ntest\n world\n test\n```\n\n{% endraw %}');
});
it('should escape codeblocks in preparation (2)', function() {
assert.equal(
page.prepare("Hello\n\n\n\tworld\n\thello\n\n\ntest"),
- "Hello\n\n\n{% raw %}\tworld\n\thello\n\n\n{% endraw %}test"
+ 'Hello\n\n{% raw %}```\nworld\nhello```\n\n{% endraw %}test\n\n'
);
assert.equal(
page.prepare("Hello\n\n\n\tworld\n\thello\n\n\n"),
- "Hello\n\n\n{% raw %}\tworld\n\thello\n\n\n{% endraw %}"
+ 'Hello\n\n{% raw %}```\nworld\nhello```\n\n{% endraw %}'
);
});
it('should escape codeblocks with nunjucks tags', function() {
assert.equal(
page.prepare('Hello {{ "Bonjour" }} ```test```'),
- 'Hello {{ "Bonjour" }} {% raw %}```test```{% endraw %}'
+ 'Hello {{ "Bonjour" }} {% raw %}`test`{% endraw %}\n\n'
);
});
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 %}'
+ '{% raw %}Hello {{ "Bonjour" }} `test`{% endraw %}\n\n'
);
assert.equal(
page.prepare('{% raw %}Hello {{ "Bonjour" }} {% raw %}{% endraw %}```test```'),
- '{% raw %}Hello {{ "Bonjour" }} {% raw %}{% endraw %}{% raw %}```test```{% endraw %}'
+ '{% raw %}Hello {{ "Bonjour" }} {% raw %}{% endraw %}{% raw %}`test`{% endraw %}\n\n'
);
assert.equal(
page.prepare('```{% raw %}Hello {{ "Bonjour" }} {% raw %}```'),
- '{% raw %}```{% raw %}Hello {{ "Bonjour" }} {% raw %}```{% endraw %}'
+ '{% raw %}`{% raw %}Hello {{ "Bonjour" }} {% raw %}`{% endraw %}\n\n'
);
assert.equal(
page.prepare('```\ntest\n```\n\n\n### Test'),
- '{% raw %}```\ntest\n```\n{% endraw %}\n\n### Test'
+ '{% raw %}```\ntest\n```\n\n{% endraw %}### Test\n\n'
);
});
it('should not process math', function() {
- assert.equal(page.prepare("Hello $world$"), "Hello $world$");
- assert.equal(page.prepare("Hello $$world$$"), "Hello $$world$$");
+ assert.equal(page.prepare("Hello $world$"), 'Hello $world$\n\n');
+ assert.equal(page.prepare("Hello $$world$$"), 'Hello $$world$$\n\n');
});
});