-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
markdown-spec.js
259 lines (241 loc) · 9.23 KB
/
markdown-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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"use strict";
describe("Core - Markdown", function() {
afterAll(function(done) {
flushIframes();
done();
});
it("should process standard markdown content", function(done) {
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
"\n\nFoo\n===\n",
};
ops.config.format = "markdown";
makeRSDoc(ops, function(doc) {
Array
.from(doc.querySelectorAll(".removeOnSave"))
.forEach(function(elem){
elem.remove();
});
var foo = doc.getElementById("foo");
expect(foo).toBeTruthy();
expect(foo.textContent).toEqual("1. Foo");
}).then(done);
});
it("should process markdown inside of sections", function(done) {
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
"<section>\nFoo\n===\n</section>",
};
ops.config.format = "markdown";
makeRSDoc(ops, function(doc) {
var foo = doc.getElementById("foo");
expect(foo).toBeTruthy();
expect(foo.textContent).toEqual("1. Foo");
}).then(done);
});
it("should process markdown inside of notes, issues and reqs.", function(done) {
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
"<p class=note>_foo_</p><div class=issue>_foo_</div><ul><li class=req>\n### _foo_###\n</li></ul>",
};
ops.config.format = "markdown";
makeRSDoc(ops, function(doc) {
expect(doc.querySelector(".note em")).toBeTruthy();
expect(doc.querySelector(".issue em")).toBeTruthy();
expect(doc.querySelector(".req em")).toBeTruthy();
expect(doc.querySelector(".req h3")).toBeTruthy();
}).then(done);
});
it("should remove left padding before processing markdown content", function(done) {
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
"\n Foo\n ===\n * list item 1\n * list item 2\n * nested list item",
};
ops.config.format = "markdown";
makeRSDoc(ops, function(doc) {
expect(doc.querySelector("code")).toBeFalsy();
expect(doc.querySelector("#foo").textContent === "Foo");
var listItems = doc.querySelectorAll("section > ul:not([class=toc]) > li");
expect(listItems.length).toEqual(2);
expect(listItems[0].textContent).toEqual("list item 1");
var nestedLi = doc.querySelector("li > ul > li");
expect(nestedLi).toBeTruthy();
expect(nestedLi.textContent).toEqual("nested list item");
}).then(done);
});
it("should structure content in nested sections with appropriate titles", function(done) {
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
"\n\nFoo\n===\n\nBar\n---\n\nBaz\n---\n\n### Foobar ###\n\n#### Foobaz ####\n\nZing\n---\n\n",
};
ops.config.format = "markdown";
makeRSDoc(ops, function(doc) {
var foo = doc.getElementById("foo");
expect(foo.localName).toEqual("h2");
expect(foo.textContent).toEqual("1. Foo");
expect(foo.parentElement.localName).toEqual("section");
var bar = doc.getElementById("bar");
expect(bar.localName).toEqual("h3");
expect(bar.textContent).toEqual("1.1 Bar");
expect(bar.parentElement.localName).toEqual("section");
var baz = doc.getElementById("baz");
expect(baz.localName).toEqual("h3");
expect(baz.textContent).toEqual("1.2 Baz");
expect(baz.parentElement.localName).toEqual("section");
var foobar = doc.getElementById("foobar");
expect(foobar.localName).toEqual("h4");
expect(foobar.textContent).toEqual("1.2.1 Foobar");
expect(foobar.parentElement.localName).toEqual("section");
var foobaz = doc.getElementById("foobaz");
expect(foobaz.localName).toEqual("h5");
expect(foobaz.textContent).toEqual("1.2.1.1 Foobaz");
expect(foobaz.parentElement.localName).toEqual("section");
var zing = doc.getElementById("zing");
expect(zing.localName).toEqual("h3");
expect(zing.textContent).toEqual("1.3 Zing");
expect(zing.parentElement.localName).toEqual("section");
}).then(done);
});
it("should gracefully handle jumps in nested headers", function(done) {
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
"\n\nFoo\n===\n\nBar\n---\n\nBaz\n===\n\n### Foobar ###\n\n",
};
ops.config.format = "markdown";
makeRSDoc(ops, function(doc) {
var foo = doc.getElementById("foo");
var bar = doc.getElementById("bar");
expect(foo.parentElement.contains(bar)).toBeTruthy();
var baz = doc.getElementById("baz");
var foobar = doc.getElementById("foobar");
expect(baz.parentElement.contains(foobar)).toBeTruthy();
}).then(done);
});
it("should nest sections according to their first header, if present", function(done) {
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
"\n\nFoo\n===\n\nsome text\n\n<section>\n\nBar\n===\n</section>\n",
};
ops.config.format = "markdown";
makeRSDoc(ops, function(doc) {
var bar = doc.getElementById("bar");
expect(bar.textContent).toEqual("2. Bar");
}).then(done);
});
it("should nest sections according to their headers", function(done) {
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
"\n\nFoo\n===\n\nsome text\n\n<section>\n\nBar\n---\n</section>\n",
};
ops.config.format = "markdown";
makeRSDoc(ops, function(doc) {
var bar = doc.getElementById("bar");
expect(bar.textContent).toMatch("1.1 Bar");
var foo = doc.getElementById("foo");
expect(foo.parentElement.contains(bar)).toBeTruthy();
}).then(done);
});
it("should not nest content following a section inside of said section", function(done) {
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
"\n\nFoo\n===\n\nsome text\n\n<section>\n\nBar\n---\n</section>\n\nBaz\n===\n\nsome text\n\n",
};
ops.config.format = "markdown";
makeRSDoc(ops, function(doc) {
var baz = doc.getElementById("baz");
expect(baz.textContent).toEqual("2. Baz");
var bar = doc.getElementById("bar");
expect(bar.parentElement.contains(baz)).toBeFalsy();
expect(baz.parentElement.contains(bar)).toBeFalsy();
}).then(done);
});
it("should not nest sections with a top level header", function(done) {
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
"\n\nFoo\n---\n\nsome text\n\n<section>\n\nBar\n---\n</section>\n",
};
ops.config.format = "markdown";
makeRSDoc(ops, function(doc) {
var bar = doc.getElementById("bar");
expect(bar.textContent).toEqual("2. Bar");
expect(doc.body.contains(bar)).toBeTruthy();
}).then(done);
});
it("should not nest sections with no headers at all", function(done) {
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
"\n\nFoo\n===\n\nsome text\n\n<section id=bar>no header</section>\n",
};
ops.config.format = "markdown";
makeRSDoc(ops, function(doc) {
var foo = doc.getElementById("foo");
var bar = doc.getElementById("bar");
expect(doc.body.contains(bar)).toBeTruthy();
expect(foo.contains(bar)).toBeFalsy();
}).then(done);
});
describe("nolinks options", function() {
it("automatically links URLs in pre when missing (smoke test)", function(done) {
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() + `
<div id=testElem>
this won't link
this will link: http://no-links-foo.com
so will this: http://no-links-bar.com
</div>`
};
ops.config.format = "markdown";
makeRSDoc(ops, function(doc) {
var anchors = doc.querySelectorAll("#testElem a");
expect(anchors.length).toEqual(2);
expect(anchors.item(0).href).toEqual("http://no-links-foo.com/");
expect(anchors.item(1).href).toEqual("http://no-links-bar.com/");
}).then(done);
});
it("replaces HTMLAnchors when present", function(done) {
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +`
<div id=testElem class=nolinks>
http://no-links-foo.com
http://no-links-bar.com
<div>
`
};
ops.config.format = "markdown";
makeRSDoc(ops, function(doc) {
var anchors = doc.querySelectorAll("#testElem a");
expect(anchors.length).toEqual(0);
expect(doc.querySelector("a[href='http://no-links-foo.com']")).toBeFalsy();
expect(doc.querySelector("a[href='http://no-links-bar.com']")).toBeFalsy();
}).then(done);
});
it("it correctly handles quoted elements", function(done){
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
"<p id='test-text1'>outer text\n\"<code>inner text</code>\".</p>\n" +
"<p id='test-text2'>outer\n \"`inner`\".</p>"
};
ops.config.format = "markdown";
makeRSDoc(ops, function(doc) {
var text1 = doc.getElementById("test-text1").textContent;
expect(text1).toEqual("outer text \"inner text\".");
var text2 = doc.getElementById("test-text2").innerHTML;
expect(text2).toEqual("outer \"<code>inner</code>\".");
}).then(done);
});
});
});