summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-markdown/test/page.js
blob: a2e21d1b0d3e219c4ed5366ff894c38f78f929e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var fs = require('fs');
var path = require('path');
var assert = require('assert');

var page = require('../').page;

describe('Page parsing', function() {
    var LEXED;

    before(function() {
        var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/PAGE.md'), 'utf8');
        LEXED = page(CONTENT);
    });

    it('should gen content', function() {
        assert(LEXED.content);
    });

    it('should not add id to headings', function() {
        assert.equal(page('# Hello').content, '<h1>Hello</h1>');
        assert.equal(page('# Hello {#test}').content, '<h1 id="test">Hello</h1>');
    });

    it('should escape codeblocks in preparation (1)', function() {
        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{% 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{% 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 %}\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 %}\n\n'
        );
        assert.equal(
            page.prepare('{% raw %}Hello {{ "Bonjour" }} {% raw %}{% endraw %}```test```'),
            '{% raw %}Hello {{ "Bonjour" }} {% raw %}{% endraw %}{% raw %}`test`{% endraw %}\n\n'
        );
        assert.equal(
            page.prepare('```{% raw %}Hello {{ "Bonjour" }} {% raw %}```'),
            '{% raw %}`{% raw %}Hello {{ "Bonjour" }} {% raw %}`{% endraw %}\n\n'
        );

        assert.equal(
            page.prepare('```\ntest\n```\n\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$\n\n');
        assert.equal(page.prepare("Hello $$world$$"), 'Hello $$world$$\n\n');
    });
});