-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
169 lines (151 loc) · 3.32 KB
/
test.js
File metadata and controls
169 lines (151 loc) · 3.32 KB
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
import assert from "node:assert";
import { describe, it, beforeEach } from "node:test";
import { minify } from "html-minifier";
import markdownIt from "markdown-it";
import markdownItAttrs from "markdown-it-attrs";
import anchorSections from "./index.js";
const minifyOptions = { collapseWhitespace: true };
/**
* Clean the result and expected strings for comparison.
* @param {string} result - The result string to clean.
* @param {string} expected - The expected string to clean.
* @returns {void}
*/
function cleanCompare(result, expected) {
assert.equal(minify(result, minifyOptions), minify(expected, minifyOptions));
}
const md = markdownIt().use(markdownItAttrs).use(anchorSections);
describe("markdown-it-anchor-sections", () => {
it("should add sections to headers", () => {
const expected = `<section id="header"><h1>header</h1><p>lorem</p></section>`;
const result = md.render(`
# header
lorem
`);
cleanCompare(result, expected);
});
it("should maintain header attributes from other plugins", () => {
const expected = `<section id="header"><h1 class="red">header</h1><p>lorem</p></section>`;
const result = md.render(`
# header {.red}
lorem
`);
cleanCompare(result, expected);
});
it("should close sections when a new header is of same or lower level", () => {
const expected = `<section id="asdf"><h2>asdf</h2><p>lorem</p></section><section id="fdsa"><h2>fdsa</h2><p>ipsum</p></section>`;
const result = md.render(`
## asdf
lorem
## fdsa
ipsum
`);
cleanCompare(result, expected);
});
it("should nest sections", () => {
const expected = `
<section id="header-1">
<h1>Header 1</h1>
<p>Text.</p>
<section id="header-2">
<h3>Header 2</h3>
<p>Lorem?</p>
</section>
<section id="header-3">
<h2>Header 3</h2>
<p>Ipsum.</p>
</section>
</section>
<section id="last-header">
<h1>Last header</h1>
<p>Markdown rules!</p>
</section>`;
const result = md.render(`
# Header 1
Text.
### Header 2
Lorem?
## Header 3
Ipsum.
# Last header
Markdown rules!
`);
cleanCompare(result, expected);
});
it("should parse incorrect order of headers", () => {
const expected = `
<section id="header-4">
<h4>Header 4</h4>
<p>Text.</p>
</section>
<section id="header-3">
<h3>Header 3</h3>
<p>Hello!</p>
</section>`;
const result = md.render(`
#### Header 4
Text.
### Header 3
Hello!
`);
cleanCompare(result, expected);
});
it("should handle sections in list", () => {
const expected = `
<ul>
<li>foo
<section id="header-2">
<h3>Header 2</h3>
Lorem?
</section>
</li>
<li>bar
<section id="last-header">
<h2>Last header</h2>
Markdown rules!
</section>
</li>
</ul>`;
const result = md.render(`
- foo
### Header 2
Lorem?
- bar
## Last header
Markdown rules!
`);
cleanCompare(result, expected);
});
it("should close sections when a new header is of same level", () => {
const expected = `
<section id="asdf">
<h3>asdf</h3>
<p>lorem</p>
</section>
<section id="fdsa">
<h3>fdsa</h3>
<p>ipsum</p>
</section>
`;
const result = md.render(`
### asdf
lorem
### fdsa
ipsum
`);
cleanCompare(result, expected);
});
it("should move, not copy, ID from header", () => {
const expected = `
<section id="asdf">
<h1>asdf</h1>
<p>qwerty</p>
</section>
`;
const result = md.render(`
# asdf {#asdf}
qwerty
`);
cleanCompare(result, expected);
});
});