-
-
Notifications
You must be signed in to change notification settings - Fork 10k
/
mobiledoc-converter_spec.js
169 lines (159 loc) · 6.61 KB
/
mobiledoc-converter_spec.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
const should = require('should');
const converter = require('../../../../../server/lib/mobiledoc/converters/mobiledoc-converter');
describe('Mobiledoc converter', function () {
describe('default', function () {
it('renders all default cards and atoms', function () {
let mobiledoc = {
version: '0.3.1',
atoms: [
['soft-return', '', {}]
],
cards: [
['markdown', {
markdown: '# Markdown card\nSome markdown'
}],
['hr', {}],
['image', {
cardWidth: 'wide',
src: '/content/images/2018/04/NatGeo06.jpg',
caption: 'Birdies'
}],
['html', {
html: '<h2>HTML card</h2>\n<div><p>Some HTML</p></div>'
}],
['embed', {
html: '<h2>Embed card</h2>'
}],
['gallery', {
images: [{
fileName: 'test.png',
src: '/test.png',
width: 1000,
height: 500
}]
}]
],
markups: [],
sections: [
[1, 'p', [
[0, [], 0, 'One'],
[1, [], 0, 0],
[0, [], 0, 'Two']
]],
[10, 0],
[1, 'p', [
[0, [], 0, 'Three']
]],
[10, 1],
[10, 2],
[1, 'p', [
[0, [], 0, 'Four']
]],
[10, 3],
[10, 4],
[10, 5],
[1, 'p', []],
]
};
converter.render(mobiledoc, 2).should.eql('<p>One<br>Two</p><h1 id="markdowncard">Markdown card</h1>\n<p>Some markdown</p>\n<p>Three</p><hr><figure class="kg-card kg-image-card kg-width-wide"><img src="/content/images/2018/04/NatGeo06.jpg" class="kg-image"><figcaption>Birdies</figcaption></figure><p>Four</p><h2>HTML card</h2>\n<div><p>Some HTML</p></div><figure class="kg-card kg-embed-card"><h2>Embed card</h2></figure><figure class="kg-card kg-gallery-card kg-width-wide"><div class="kg-gallery-container"></div></figure>');
});
it('removes final blank paragraph', function () {
let mobiledoc = {
version: '0.3.1',
atoms: [],
cards: [],
markups: [],
sections: [
[1, 'p', [
[0, [], 0, 'Test']
]],
[1, 'p', []]
]
};
converter.render(mobiledoc, 2).should.eql('<p>Test</p>');
});
it('adds id attributes to headings', function () {
let mobiledoc = {
version: '0.3.1',
atoms: [],
cards: [],
markups: [
['a', ['href', 'http://example.com']]
],
sections: [
[1, 'h1', [
[0, [], 0, 'Heading One']
]],
[1, 'h2', [
[0, [], 0, 'Heading Two']
]],
[1, 'h3', [
[0, [], 0, 'Heading Three']
]],
[1, 'h4', [
[0, [], 0, 'Heading Four']
]],
[1, 'h5', [
[0, [], 0, 'Heading Five']
]],
[1, 'h6', [
[0, [], 0, 'Heading Six']
]],
// duplicate text
[1, 'h1', [
[0, [], 0, 'Heading One']
]],
[1, 'h3', [
[0, [], 0, 'Heading One']
]],
// invalid attr chars
[1, 'h1', [
[0, [], 0, '< left < arrow <']
]],
[1, 'h1', [
[0, [], 0, '> right > arrow >']
]],
[1, 'h1', [
[0, [], 0, '"quote" "test"']
]],
[1, 'h1', [
[0, [], 0, '? question?']
]],
[1, 'h1', [
[0, [], 0, '& ampersand&']
]],
// trailing link
[1, 'h1', [
[0, [], 0, 'trailing '],
[0, [0], 1, 'link']
]],
// preceding link
[1, 'h1', [
[0, [0], 1, 'preceding'],
[0, [], 0, ' link']
]]
]
};
let output = converter.render(mobiledoc, 2);
// normal headings
output.should.match(/<h1 id="heading-one">Heading One<\/h1>/);
output.should.match(/<h2 id="heading-two">Heading Two<\/h2>/);
output.should.match(/<h3 id="heading-three">Heading Three<\/h3>/);
output.should.match(/<h4 id="heading-four">Heading Four<\/h4>/);
output.should.match(/<h5 id="heading-five">Heading Five<\/h5>/);
output.should.match(/<h6 id="heading-six">Heading Six<\/h6>/);
// duplicate heading text
output.should.match(/<h1 id="heading-one-1">Heading One<\/h1>/);
output.should.match(/<h3 id="heading-one-2">Heading One<\/h3>/);
// invalid ID/hash-url chars
output.should.match(/<h1 id="left-arrow">< left < arrow <<\/h1>/);
output.should.match(/<h1 id="right-arrow">> right > arrow ><\/h1>/);
output.should.match(/<h1 id="quote-test">"quote" "test"<\/h1>/);
output.should.match(/<h1 id="question">\? question\?<\/h1>/);
output.should.match(/<h1 id="ampersand">& ampersand&<\/h1>/);
// heading with link
output.should.match(/<h1 id="trailing-link">trailing <a href="http:\/\/example\.com">link<\/a><\/h1>/);
output.should.match(/<h1 id="preceding-link"><a href="http:\/\/example\.com">preceding<\/a> link<\/h1>/);
});
});
});