forked from hexojs/hexo-renderer-marked
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
194 lines (157 loc) · 5.59 KB
/
index.js
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
190
191
192
193
194
'use strict';
const should = require('chai').should(); // eslint-disable-line
const util = require('hexo-util');
describe('Marked renderer', () => {
const ctx = {
config: {
marked: {}
}
};
const r = require('../lib/renderer').bind(ctx);
it('default', () => {
const code = 'console.log("Hello world");';
const body = [
'# Hello world',
'',
'```',
code,
'```',
'',
'## Hello world',
'',
'hello'
].join('\n');
const result = r({text: body});
result.should.eql([
'<h1 id="Hello-world"><a href="#Hello-world" class="headerlink" title="Hello world"></a>Hello world</h1>',
'<pre><code>' + util.highlight(code, {gutter: false, wrap: false}) + '</code></pre>',
'<h2 id="Hello-world-1"><a href="#Hello-world-1" class="headerlink" title="Hello world"></a>Hello world</h2>',
'<p>hello</p>'
].join('') + '\n');
});
it('should render headings with links', () => {
const body = [
'## [hexo-server]',
'',
'[hexo-server]: https://github.com/hexojs/hexo-server'
].join('\n');
const result = r({text: body});
result.should.eql([
'<h2 id="hexo-server"><a href="#hexo-server" class="headerlink" title="hexo-server"></a>',
'<a href="https://github.com/hexojs/hexo-server">hexo-server</a></h2>'
].join(''));
});
it('should handle chinese headers properly', () => {
const body = '# 中文';
const result = r({text: body});
result.should.eql('<h1 id="中文"><a href="#中文" class="headerlink" title="中文"></a>中文</h1>');
});
it('should render headings without headerIds when disabled', () => {
const body = '## hexo-server';
ctx.config.marked.headerIds = false;
const result = r({text: body});
result.should.eql([
'<h2>hexo-server</h2>'
].join(''));
});
// Description List tests
it('should render description lists with a single space after the colon', () => {
const result = r({text: 'Description Term<br>: This is the Description'});
result.should.eql('<dl><dt>Description Term</dt><dd>This is the Description</dd></dl>');
});
it('should render description lists with multiple spaces after the colon', () => {
const result = r({text: 'Description Term<br>: This is the Description'});
result.should.eql('<dl><dt>Description Term</dt><dd>This is the Description</dd></dl>');
});
it('should render description lists with a tab after the colon', () => {
const result = r({text: 'Description Term<br>: This is the Description'});
result.should.eql('<dl><dt>Description Term</dt><dd>This is the Description</dd></dl>');
});
it('should render description lists with a carriage return after the colon', () => {
const result = r({text: 'Description Term<br>:\nThis is the Description'});
result.should.eql('<dl><dt>Description Term</dt><dd>This is the Description</dd></dl>');
});
it('should not render regular paragraphs as description lists', () => {
const result = r({text: 'Description Term<br>:This is the Description'});
result.should.eql('<p>Description Term<br>:This is the Description</p>\n');
});
describe('autolink option tests', () => {
const ctx = {
config: {
marked: {
autolink: true
}
}
};
const renderer = require('../lib/renderer');
const body = [
'Great website http://hexo.io',
'',
'[Hexo](http://hexo.io)'
].join('\n');
it('autolink enabled', () => {
const r = renderer.bind(ctx);
const result = r({text: body});
result.should.eql([
'<p>Great website <a href="http://hexo.io">http://hexo.io</a></p>\n',
'<p><a href="http://hexo.io">Hexo</a></p>\n'
].join(''));
});
it('autolink disabled', () => {
ctx.config.marked.autolink = false;
const r = renderer.bind(ctx);
const result = r({text: body});
result.should.eql([
'<p>Great website http://hexo.io</p>\n',
'<p><a href="http://hexo.io">Hexo</a></p>\n'
].join(''));
});
});
describe('modifyAnchors option tests', () => {
const body = [
'- [Example](#example)',
'',
'# Example'
].join('\n');
const renderer = require('../lib/renderer');
const ctx = {
config: {
marked: {
modifyAnchors: ''
}
}
};
it('should not modify anchors with default options', () => {
const r = renderer.bind(ctx);
const result = r({text: body});
result.should.eql([
'<ul>',
'<li><a href=\"#example\">Example</a></li>',
'</ul>',
'<h1 id=\"Example\"><a href=\"#Example\" class=\"headerlink\" title=\"Example\"></a>Example</h1>'
].join('\n'));
});
it('should set anchors to upperCase in case of modifyAnchors option is 2', () => {
ctx.config.marked.modifyAnchors = 2;
const r = renderer.bind(ctx);
const result = r({text: body});
result.should.eql([
'<ul>',
'<li><a href=\"#example\">Example</a></li>',
'</ul>',
'<h1 id=\"EXAMPLE\"><a href=\"#EXAMPLE\" class=\"headerlink\" title=\"Example\"></a>Example</h1>'
].join('\n'));
});
it('should set anchors to lowerCase in case of modifyAnchors option is 1', () => {
ctx.config.marked.modifyAnchors = 1;
const r = renderer.bind(ctx);
const result = r({text: body});
result.should.eql([
'<ul>',
'<li><a href=\"#example\">Example</a></li>',
'</ul>',
'<h1 id=\"example\"><a href=\"#example\" class=\"headerlink\" title=\"Example\"></a>Example</h1>'
].join('\n'));
});
});
});