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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
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('Markdoc - render html', () => {
let fixture;
before(async () => {
fixture = await getFixture('render-html');
});
describe('dev', () => {
let devServer;
before(async () => {
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('renders content - simple', async () => {
const res = await fixture.fetch('/simple');
const html = await res.text();
renderSimpleChecks(html);
});
it('renders content - nested-html', async () => {
const res = await fixture.fetch('/nested-html');
const html = await res.text();
renderNestedHTMLChecks(html);
});
it('renders content - components interleaved with html', async () => {
const res = await fixture.fetch('/components');
const html = await res.text();
renderComponentsHTMLChecks(html);
});
it('renders content - randomly cased html attributes', async () => {
const res = await fixture.fetch('/randomly-cased-html-attributes');
const html = await res.text();
renderRandomlyCasedHTMLAttributesChecks(html);
});
it('renders content - html within partials', async () => {
const res = await fixture.fetch('/with-partial');
const html = await res.text();
renderHTMLWithinPartialChecks(html);
});
});
describe('build', () => {
before(async () => {
await fixture.build();
});
it('renders content - simple', async () => {
const html = await fixture.readFile('/simple/index.html');
renderSimpleChecks(html);
});
it('renders content - nested-html', async () => {
const html = await fixture.readFile('/nested-html/index.html');
renderNestedHTMLChecks(html);
});
it('renders content - components interleaved with html', async () => {
const html = await fixture.readFile('/components/index.html');
renderComponentsHTMLChecks(html);
});
it('renders content - randomly cased html attributes', async () => {
const html = await fixture.readFile('/randomly-cased-html-attributes/index.html');
renderRandomlyCasedHTMLAttributesChecks(html);
});
it('renders content - html within partials', async () => {
const html = await fixture.readFile('/with-partial/index.html');
renderHTMLWithinPartialChecks(html);
});
});
});
/** @param {string} html */
function renderSimpleChecks(html) {
const { document } = parseHTML(html);
const h2 = document.querySelector('h2');
assert.equal(h2.textContent, 'Simple post header');
const spanInsideH2 = document.querySelector('h2 > span');
assert.equal(spanInsideH2.textContent, 'post');
assert.equal(spanInsideH2.className, 'inside-h2');
assert.equal(spanInsideH2.style.color, 'fuscia');
const p1 = document.querySelector('article > p:nth-of-type(1)');
assert.equal(p1.children.length, 1);
assert.equal(p1.textContent, 'This is a simple Markdoc post.');
const p2 = document.querySelector('article > p:nth-of-type(2)');
assert.equal(p2.children.length, 0);
assert.equal(p2.textContent, 'This is a paragraph!');
const p3 = document.querySelector('article > p:nth-of-type(3)');
assert.equal(p3.children.length, 1);
assert.equal(p3.textContent, 'This is a span inside a paragraph!');
const video = document.querySelector('video');
assert.ok(video, 'A video element should exist');
assert.ok(video.hasAttribute('autoplay'), 'The video element should have the autoplay attribute');
assert.ok(video.hasAttribute('muted'), 'The video element should have the muted attribute');
}
/** @param {string} html */
function renderNestedHTMLChecks(html) {
const { document } = parseHTML(html);
const p1 = document.querySelector('p:nth-of-type(1)');
assert.equal(p1.id, 'p1');
assert.equal(p1.textContent, 'before inner after');
assert.equal(p1.children.length, 1);
const p1Span1 = p1.querySelector('span');
assert.equal(p1Span1.textContent, 'inner');
assert.equal(p1Span1.id, 'inner1');
assert.equal(p1Span1.className, 'inner-class');
assert.equal(p1Span1.style.color, 'hotpink');
const p2 = document.querySelector('p:nth-of-type(2)');
assert.equal(p2.id, 'p2');
assert.equal(p2.textContent, '\n before\n inner\n after\n');
assert.equal(p2.children.length, 1);
const divL1 = document.querySelector('div:nth-of-type(1)');
assert.equal(divL1.id, 'div-l1');
assert.equal(divL1.children.length, 2);
const divL2_1 = divL1.querySelector('div:nth-of-type(1)');
assert.equal(divL2_1.id, 'div-l2-1');
assert.equal(divL2_1.children.length, 1);
const p3 = divL2_1.querySelector('p:nth-of-type(1)');
assert.equal(p3.id, 'p3');
assert.equal(p3.textContent, 'before inner after');
assert.equal(p3.children.length, 1);
const divL2_2 = divL1.querySelector('div:nth-of-type(2)');
assert.equal(divL2_2.id, 'div-l2-2');
assert.equal(divL2_2.children.length, 2);
const p4 = divL2_2.querySelector('p:nth-of-type(1)');
assert.equal(p4.id, 'p4');
assert.equal(p4.textContent, 'before inner after');
assert.equal(p4.children.length, 1);
const p5 = divL2_2.querySelector('p:nth-of-type(2)');
assert.equal(p5.id, 'p5');
assert.equal(p5.textContent, 'before inner after');
assert.equal(p5.children.length, 1);
}
/**
*
* @param {string} html */
function renderRandomlyCasedHTMLAttributesChecks(html) {
const { document } = parseHTML(html);
const td1 = document.querySelector('#td1');
const td2 = document.querySelector('#td1');
const td3 = document.querySelector('#td1');
const td4 = document.querySelector('#td1');
// all four <td>'s which had randomly cased variants of colspan/rowspan should all be rendered lowercased at this point
assert.equal(td1.getAttribute('colspan'), '3');
assert.equal(td1.getAttribute('rowspan'), '2');
assert.equal(td2.getAttribute('colspan'), '3');
assert.equal(td2.getAttribute('rowspan'), '2');
assert.equal(td3.getAttribute('colspan'), '3');
assert.equal(td3.getAttribute('rowspan'), '2');
assert.equal(td4.getAttribute('colspan'), '3');
assert.equal(td4.getAttribute('rowspan'), '2');
}
/**
* @param {string} html
*/
function renderHTMLWithinPartialChecks(html) {
const { document } = parseHTML(html);
const li = document.querySelector('ul > li#partial');
assert.equal(li.textContent, 'List item');
}
/**
* Asserts that the rendered HTML tags with interleaved Markdoc tags (both block and inline) rendered in the expected nested graph of elements
*
* @param {string} html */
function renderComponentsHTMLChecks(html) {
const { document } = parseHTML(html);
const naturalP1 = document.querySelector('article > p:nth-of-type(1)');
assert.equal(naturalP1.textContent, 'This is a inline mark in regular Markdown markup.');
assert.equal(naturalP1.children.length, 1);
const p1 = document.querySelector('article > p:nth-of-type(2)');
assert.equal(p1.id, 'p1');
assert.equal(p1.textContent, 'This is a inline mark under some HTML');
assert.equal(p1.children.length, 1);
assertInlineMark(p1.children[0]);
const div1p1 = document.querySelector('article > #div1 > p:nth-of-type(1)');
assert.equal(div1p1.id, 'div1-p1');
assert.equal(div1p1.textContent, 'This is a inline mark under some HTML');
assert.equal(div1p1.children.length, 1);
assertInlineMark(div1p1.children[0]);
const div1p2 = document.querySelector('article > #div1 > p:nth-of-type(2)');
assert.equal(div1p2.id, 'div1-p2');
assert.equal(div1p2.textContent, 'This is a inline mark under some HTML');
assert.equal(div1p2.children.length, 1);
const div1p2span1 = div1p2.querySelector('span');
assert.equal(div1p2span1.id, 'div1-p2-span1');
assert.equal(div1p2span1.textContent, 'inline mark');
assert.equal(div1p2span1.children.length, 1);
assertInlineMark(div1p2span1.children[0]);
const aside1 = document.querySelector('article > aside:nth-of-type(1)');
const aside1Title = aside1.querySelector('p.title');
assert.equal(aside1Title.textContent.trim(), 'Aside One');
const aside1Section = aside1.querySelector('section');
const aside1SectionP1 = aside1Section.querySelector('p:nth-of-type(1)');
assert.equal(
aside1SectionP1.textContent,
"I'm a Markdown paragraph inside an top-level aside tag",
);
const aside1H2_1 = aside1Section.querySelector('h2:nth-of-type(1)');
assert.equal(aside1H2_1.id, 'im-an-h2-via-markdown-markup'); // automatic slug
assert.equal(aside1H2_1.textContent, "I'm an H2 via Markdown markup");
const aside1H2_2 = aside1Section.querySelector('h2:nth-of-type(2)');
assert.equal(aside1H2_2.id, 'h-two');
assert.equal(aside1H2_2.textContent, "I'm an H2 via HTML markup");
const aside1SectionP2 = aside1Section.querySelector('p:nth-of-type(2)');
assert.equal(aside1SectionP2.textContent, 'Markdown bold vs HTML bold');
assert.equal(aside1SectionP2.children.length, 2);
const aside1SectionP2Strong1 = aside1SectionP2.querySelector('strong:nth-of-type(1)');
assert.equal(aside1SectionP2Strong1.textContent, 'Markdown bold');
const aside1SectionP2Strong2 = aside1SectionP2.querySelector('strong:nth-of-type(2)');
assert.equal(aside1SectionP2Strong2.textContent, 'HTML bold');
const article = document.querySelector('article');
assert.equal(article.textContent.includes('RENDERED'), true);
assert.notEqual(article.textContent.includes('NOT RENDERED'), true);
const section1 = document.querySelector('article > #section1');
const section1div1 = section1.querySelector('#div1');
const section1Aside1 = section1div1.querySelector('aside:nth-of-type(1)');
const section1Aside1Title = section1Aside1.querySelector('p.title');
assert.equal(section1Aside1Title.textContent.trim(), 'Nested un-indented Aside');
const section1Aside1Section = section1Aside1.querySelector('section');
const section1Aside1SectionP1 = section1Aside1Section.querySelector('p:nth-of-type(1)');
assert.equal(section1Aside1SectionP1.textContent, 'regular Markdown markup');
const section1Aside1SectionP4 = section1Aside1Section.querySelector('p:nth-of-type(2)');
assert.equal(section1Aside1SectionP4.textContent, 'nested inline mark content');
assert.equal(section1Aside1SectionP4.children.length, 1);
assertInlineMark(section1Aside1SectionP4.children[0]);
const section1div2 = section1.querySelector('#div2');
const section1Aside2 = section1div2.querySelector('aside:nth-of-type(1)');
const section1Aside2Title = section1Aside2.querySelector('p.title');
assert.equal(section1Aside2Title.textContent.trim(), 'Nested indented Aside 💀');
const section1Aside2Section = section1Aside2.querySelector('section');
const section1Aside2SectionP1 = section1Aside2Section.querySelector('p:nth-of-type(1)');
assert.equal(section1Aside2SectionP1.textContent, 'regular Markdown markup');
const section1Aside1SectionP5 = section1Aside2Section.querySelector('p:nth-of-type(2)');
assert.equal(section1Aside1SectionP5.id, 'p5');
assert.equal(section1Aside1SectionP5.children.length, 1);
const section1Aside1SectionP5Span1 = section1Aside1SectionP5.children[0];
assert.equal(section1Aside1SectionP5Span1.textContent, 'inline mark');
assert.equal(section1Aside1SectionP5Span1.children.length, 1);
const section1Aside1SectionP5Span1Span1 = section1Aside1SectionP5Span1.children[0];
assert.equal(section1Aside1SectionP5Span1Span1.textContent, ' mark');
}
/** @param {HTMLElement | null | undefined} el */
function assertInlineMark(el) {
assert.ok(el);
assert.equal(el.children.length, 0);
assert.equal(el.textContent, 'inline mark');
assert.equal(el.className, 'mark');
assert.equal(el.style.color, 'hotpink');
}
|