summaryrefslogtreecommitdiff
path: root/packages/integrations/markdoc/test/headings.test.js
blob: 1a6061aa6b4d193053047217c18ddd4524348047 (plain) (blame)
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
260
261
262
263
264
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { parseHTML } from 'linkedom';
import { loadFixture } from '../../../astro/test/test-utils.js';

async function getFixture(name) {
	return await loadFixture({
		root: new URL(`./fixtures/${name}/`, import.meta.url),
	});
}

describe('experimental.headingIdCompat', () => {
	let fixture;

	before(async () => {
		fixture = await loadFixture({
			root: new URL(`./fixtures/headings/`, import.meta.url),
			experimental: { headingIdCompat: true },
		});
	});

	describe('dev', () => {
		let devServer;

		before(async () => {
			devServer = await fixture.startDevServer();
		});

		after(async () => {
			await devServer.stop();
		});

		it('applies IDs to headings containing special characters', async () => {
			const res = await fixture.fetch('/headings-with-special-characters');
			const html = await res.text();
			const { document } = parseHTML(html);

			assert.equal(document.querySelector('h2')?.id, 'picture-');
			assert.equal(document.querySelector('h3')?.id, '-sacrebleu--');
		});
	});
});

describe('Markdoc - Headings', () => {
	let fixture;

	before(async () => {
		fixture = await getFixture('headings');
	});

	describe('dev', () => {
		let devServer;

		before(async () => {
			devServer = await fixture.startDevServer();
		});

		after(async () => {
			await devServer.stop();
		});

		it('applies IDs to headings', async () => {
			const res = await fixture.fetch('/headings');
			const html = await res.text();
			const { document } = parseHTML(html);

			idTest(document);
		});

		it('applies IDs to headings containing special characters', async () => {
			const res = await fixture.fetch('/headings-with-special-characters');
			const html = await res.text();
			const { document } = parseHTML(html);

			assert.equal(document.querySelector('h2')?.id, 'picture');
			assert.equal(document.querySelector('h3')?.id, '-sacrebleu-');
		});

		it('generates the same IDs for other documents with the same headings', async () => {
			const res = await fixture.fetch('/headings-stale-cache-check');
			const html = await res.text();
			const { document } = parseHTML(html);

			idTest(document);
		});

		it('generates a TOC with correct info', async () => {
			const res = await fixture.fetch('/headings');
			const html = await res.text();
			const { document } = parseHTML(html);

			tocTest(document);
		});
	});

	describe('build', () => {
		before(async () => {
			await fixture.build();
		});

		it('applies IDs to headings', async () => {
			const html = await fixture.readFile('/headings/index.html');
			const { document } = parseHTML(html);

			idTest(document);
		});

		it('generates the same IDs for other documents with the same headings', async () => {
			const html = await fixture.readFile('/headings-stale-cache-check/index.html');
			const { document } = parseHTML(html);

			idTest(document);
		});

		it('generates a TOC with correct info', async () => {
			const html = await fixture.readFile('/headings/index.html');
			const { document } = parseHTML(html);

			tocTest(document);
		});
	});
});

describe('Markdoc - Headings with custom Astro renderer', () => {
	let fixture;

	before(async () => {
		fixture = await getFixture('headings-custom');
	});

	describe('dev', () => {
		let devServer;

		before(async () => {
			devServer = await fixture.startDevServer();
		});

		after(async () => {
			await devServer.stop();
		});

		it('applies IDs to headings', async () => {
			const res = await fixture.fetch('/headings');
			const html = await res.text();
			const { document } = parseHTML(html);

			idTest(document);
		});

		it('generates the same IDs for other documents with the same headings', async () => {
			const res = await fixture.fetch('/headings-stale-cache-check');
			const html = await res.text();
			const { document } = parseHTML(html);

			idTest(document);
		});

		it('generates a TOC with correct info', async () => {
			const res = await fixture.fetch('/headings');
			const html = await res.text();
			const { document } = parseHTML(html);

			tocTest(document);
		});

		it('renders Astro component for each heading', async () => {
			const res = await fixture.fetch('/headings');
			const html = await res.text();
			const { document } = parseHTML(html);

			astroComponentTest(document);
		});
	});

	describe('build', () => {
		before(async () => {
			await fixture.build();
		});

		it('applies IDs to headings', async () => {
			const html = await fixture.readFile('/headings/index.html');
			const { document } = parseHTML(html);

			idTest(document);
		});

		it('generates the same IDs for other documents with the same headings', async () => {
			const html = await fixture.readFile('/headings-stale-cache-check/index.html');
			const { document } = parseHTML(html);

			idTest(document);
		});

		it('generates a TOC with correct info', async () => {
			const html = await fixture.readFile('/headings/index.html');
			const { document } = parseHTML(html);

			tocTest(document);
		});

		it('renders Astro component for each heading', async () => {
			const html = await fixture.readFile('/headings/index.html');
			const { document } = parseHTML(html);

			astroComponentTest(document);
		});
	});
});

const depthToHeadingMap = {
	1: {
		slug: 'level-1-heading',
		text: 'Level 1 heading',
	},
	2: {
		slug: 'level-2-heading',
		text: 'Level 2 heading',
	},
	3: {
		slug: 'level-3-heading',
		text: 'Level 3 heading',
	},
	4: {
		slug: 'level-4-heading',
		text: 'Level 4 heading',
	},
	5: {
		slug: 'id-override',
		text: 'Level 5 heading with override',
	},
	6: {
		slug: 'level-6-heading',
		text: 'Level 6 heading',
	},
};

/** @param {Document} document */
function idTest(document) {
	for (const [depth, info] of Object.entries(depthToHeadingMap)) {
		assert.equal(document.querySelector(`h${depth}`)?.getAttribute('id'), info.slug);
	}
}

/** @param {Document} document */
function tocTest(document) {
	const toc = document.querySelector('[data-toc] > ul');
	assert.equal(toc.children.length, Object.keys(depthToHeadingMap).length);

	for (const [depth, info] of Object.entries(depthToHeadingMap)) {
		const linkEl = toc.querySelector(`a[href="#${info.slug}"]`);
		assert.ok(linkEl);
		assert.equal(linkEl.getAttribute('data-depth'), depth);
		assert.equal(linkEl.textContent.trim(), info.text);
	}
}

/** @param {Document} document */
function astroComponentTest(document) {
	const headings = document.querySelectorAll('h1, h2, h3, h4, h5, h6');

	for (const heading of headings) {
		assert.equal(heading.hasAttribute('data-custom-heading'), true);
	}
}