summaryrefslogtreecommitdiffstats
path: root/test/page.js
blob: 52e6d4e5878da5d152d1fbaa94aabce5ba0414f5 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var mock = require('./mock');
var Output = require('../lib/output/base');

describe('Page', function() {
    var book, output;

    before(function() {
        return mock.setupDefaultBook({
            'heading.md': '# Hello\n\n## World',
            'links.md': '[link](hello.md) [link 2](variables/page/next.md) [readme](README.md)',

            'codes/simple.md': '```hello world```',
            'codes/lang.md': '```js\nhello world\n```',
            'codes/lang.adoc': '```js\nhello world\n```',

            'folder/paths.md': '',

            'variables/file/mtime.md': '{{ file.mtime }}',
            'variables/file/path.md': '{{ file.path }}',
            'variables/page/title.md': '{{ page.title }}',
            'variables/page/previous.md': '{{ page.previous.title }} {{ page.previous.path }}',
            'variables/page/next.md': '{{ page.next.title }} {{ page.next.path }}'
        }, [
            {
                title: 'Test page.next',
                path: 'variables/page/next.md'
            },
            {
                title: 'Test Variables',
                path: 'variables/page/title.md'
            },
            {
                title: 'Test page.previous',
                path: 'variables/page/previous.md'
            }
        ])
        .then(function(_book) {
            book = _book;
            output = new Output(book);

            return book.parse();
        });
    });

    describe('.resolveLocal', function() {
        it('should correctly resolve path to file', function() {
            var page = book.addPage('heading.md');

            page.resolveLocal('test.png').should.equal('test.png');
            page.resolveLocal('/test.png').should.equal('test.png');
            page.resolveLocal('test/hello.png').should.equal('test/hello.png');
            page.resolveLocal('/test/hello.png').should.equal('test/hello.png');
        });

        it('should correctly resolve path to file (2)', function() {
            var page = book.addPage('folder/paths.md');

            page.resolveLocal('test.png').should.equal('folder/test.png');
            page.resolveLocal('/test.png').should.equal('test.png');
            page.resolveLocal('test/hello.png').should.equal('folder/test/hello.png');
            page.resolveLocal('/test/hello.png').should.equal('test/hello.png');
        });
    });

    describe('.relative', function() {
        it('should correctly resolve absolute path in the book', function() {
            var page = book.addPage('heading.md');
            var page2 = book.addPage('folder/paths.md');

            page.relative('/test.png').should.equal('test.png');
            page.relative('test.png').should.equal('test.png');
            page2.relative('/test.png').should.equal('../test.png');
            page2.relative('test.png').should.equal('test.png');
        });
    });

    describe('Headings', function() {
        it('should add a default ID to headings', function() {
            var page = book.addPage('heading.md');

            return page.toHTML(output)
            .then(function() {
                page.content.should.be.html({
                    'h1#hello': {
                        count: 1
                    },
                    'h2#world': {
                        count: 1
                    }
                });
            });
        });
    });

    describe('Code Blocks', function() {
        var page;

        before(function() {
            output.template.addBlock('code', function(blk) {
                return (blk.kwargs.language || '') + blk.body + 'test';
            });
        });

        it('should apply "code" block', function() {
            page = book.addPage('codes/simple.md');
            return page.toHTML(output)
                .should.be.fulfilledWith('<p><code>hello worldtest</code></p>\n');
        });

        it('should add language as kwargs', function() {
            page = book.addPage('codes/lang.md');
            return page.toHTML(output)
                .should.be.fulfilledWith('<pre><code class="lang-js">jshello world\ntest</code></pre>\n');
        });

        it('should add language as kwargs (asciidoc)', function() {
            page = book.addPage('codes/lang.adoc');
            return page.toHTML(output)
                .should.be.fulfilledWith('<div class="listingblock">\n<div class="content">\n<pre class="highlight"><code class="language-js" data-lang="js">jshello worldtest</code></pre>\n</div>\n</div>');
        });
    });

    describe('Links', function() {
        var page;

        before(function() {
            page = book.addPage('links.md');
            return page.toHTML(output);
        });

        it('should replace links to page to .html', function() {
            page.content.should.be.html({
                'a[href="./"]': {
                    count: 1
                }
            });
        });

        it('should use directory urls when file is a README', function() {
            page.content.should.be.html({
                'a[href="./"]': {
                    count: 1
                }
            });
        });

        it('should not replace links to file not in SUMMARY', function() {
            page.content.should.be.html({
                'a[href="hello.md"]': {
                    count: 1
                }
            });
        });
    });

    describe('Templating Context', function() {
        it('should set file.mtime', function() {
            var page = book.addPage('variables/file/mtime.md');
            return page.toHTML(output)
            .then(function(content) {
                content.should.endWith('(CET)</p>\n');
            });
        });

        it('should set file.path', function() {
            var page = book.addPage('variables/file/path.md');
            return page.toHTML(output)
            .should.be.fulfilledWith('<p>variables/file/path.md</p>\n');
        });

        it('should set page.title when page is in summary', function() {
            var page = book.getPage('variables/page/title.md');
            return page.toHTML(output)
            .should.be.fulfilledWith('<p>Test Variables</p>\n');
        });

        it('should set page.previous when possible', function() {
            var page = book.getPage('variables/page/previous.md');
            return page.toHTML(output)
            .should.be.fulfilledWith('<p>Test Variables variables/page/title.md</p>\n');
        });

        it('should set page.next when possible', function() {
            var page = book.getPage('variables/page/next.md');
            return page.toHTML(output)
            .should.be.fulfilledWith('<p>Test Variables variables/page/title.md</p>\n');
        });
    });
});