-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathrehype-fix-dom-nesting.js
More file actions
415 lines (368 loc) · 12.8 KB
/
Copy pathrehype-fix-dom-nesting.js
File metadata and controls
415 lines (368 loc) · 12.8 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* eslint-env node */
/**
* rehype plugin: emit markup the HTML parser will not rearrange.
*
* React hydration compares the tree it renders against the DOM the browser
* parsed from the server-rendered HTML. Where those disagree, hydration fails
* ("Minified React error #418") and, with no Suspense boundary to recover into,
* React discards the server markup and re-renders the whole root on the client
* ("Minified React error #423"). Markup the parser silently restructures is
* therefore a hydration bug, even when it renders the way the author intended.
*
* Two shapes show up in MDX content:
*
* 1. Block-level content inside a <p>. MDX parses the body of a multi-line JSX
* element as Markdown, so authoring
*
* <p>
* Some text.
* </p>
*
* compiles to `<p><p>Some text.</p></p>`, and the parser closes the outer <p>
* before the nested one. The same happens for a heading, list, table, <div>
* or code block written inside a hand-written <p>.
*
* 2. Table rows and cells written without their section. `<table><tr>` and
* `<thead><th>` are how people write tables by hand, but the parser inserts
* the implied <tbody> and <tr>, so every row lands one level deeper than
* React put it.
*
* This plugin applies the parser's own rules at build time - hoisting block
* children out of paragraphs, and inserting the table sections and rows the
* parser would - so the emitted HTML survives a round trip through it.
*/
// Elements whose start tag implicitly closes an open <p> (HTML Standard,
// "a p element's end tag can be omitted" plus the in-body insertion rules).
const BLOCK_LEVEL_TAGS = new Set([
"address",
"article",
"aside",
"blockquote",
"center",
"dd",
"details",
"dialog",
"dir",
"div",
"dl",
"dt",
"fieldset",
"figcaption",
"figure",
"footer",
"form",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"header",
"hgroup",
"hr",
"li",
"main",
"menu",
"nav",
"ol",
"p",
"pre",
"section",
"summary",
"table",
"ul",
]);
// Components that render a single text element (<p> by default). MDX turns
// their multi-line body into a Markdown paragraph, which lands inside whatever
// element the component renders — invalid there for every variant it supports.
const PARAGRAPH_COMPONENTS = new Set(["Typography"]);
// Table structure the parser fills in when rows or cells are written bare.
const TABLE_SECTION_TAGS = new Set(["thead", "tbody", "tfoot"]);
const TABLE_CELL_TAGS = new Set(["td", "th"]);
// Stray text in any of these is "foster parented" out of the table entirely.
const TABLE_INTERNAL_TAGS = new Set(["table", "thead", "tbody", "tfoot", "tr"]);
const tagNameOf = (node) => {
if (!node || typeof node !== "object") return null;
if (node.type === "element") return node.tagName;
if (node.type === "mdxJsxFlowElement" || node.type === "mdxJsxTextElement") {
return node.name;
}
return null;
};
const lowerTagNameOf = (node) => (tagNameOf(node) || "").toLowerCase();
const isBlockLevel = (node) => {
const tagName = tagNameOf(node);
return (
typeof tagName === "string" && BLOCK_LEVEL_TAGS.has(tagName.toLowerCase())
);
};
const isParagraph = (node) => tagNameOf(node) === "p";
const isBlankText = (node) =>
node &&
node.type === "text" &&
typeof node.value === "string" &&
node.value.trim() === "";
// Whether a paragraph node carries markup worth keeping (a className, an id, …)
// when a redundant wrapper has to be dropped.
const hasAttributes = (node) => {
if (node.type === "element") {
return !!node.properties && Object.keys(node.properties).length > 0;
}
return Array.isArray(node.attributes) && node.attributes.length > 0;
};
// `<p><p class="lead">text</p></p>` keeps whichever of the two wrappers carries
// the attributes, so styling written by the author survives.
const collapseSoleNestedParagraph = (paragraph) => {
let current = paragraph;
for (;;) {
const content = current.children.filter((child) => !isBlankText(child));
if (content.length !== 1 || !isParagraph(content[0])) return current;
const inner = content[0];
if (hasAttributes(current) || !hasAttributes(inner)) {
current.children = inner.children;
} else {
current = inner;
}
}
};
// `<Typography>` and friends render one text element, so a lone Markdown
// paragraph in their body is always misplaced. Only a sole paragraph is lifted:
// a body deliberately written as several paragraphs is left as the author wrote
// it, since the component would have to be given a block-level `component` prop
// for that to be valid anyway.
const isParagraphComponent = (node) => {
const name = tagNameOf(node);
return !!name && PARAGRAPH_COMPONENTS.has(name);
};
// `<Typography>` and friends render one text element, so a Markdown paragraph
// in their body is always misplaced. Each paragraph becomes its own copy of the
// component, which keeps the author's paragraph breaks and their styling: a
// body written as two paragraphs renders as two, rather than running together.
// A paragraph carrying its own markup is kept instead of the component wrapper,
// since the more specific attributes are the ones worth preserving.
const liftParagraphsFromComponent = (component) => {
// Blank text is ignored when deciding what to do, but the children are walked
// as written: whitespace between two inline children is a word separator.
const content = component.children.filter((child) => !isBlankText(child));
const needsWork = content.some(
(child) =>
isParagraph(child) || isBlockLevel(child) || containsBlockLevel(child),
);
if (!content.length || !needsWork) return [component];
const siblings = [];
let inlineRun = [];
const flushInlineRun = () => {
if (inlineRun.some((child) => !isBlankText(child))) {
siblings.push(withChildren(component, inlineRun));
}
inlineRun = [];
};
for (const child of component.children) {
if (isParagraph(child)) {
flushInlineRun();
// A paragraph carrying its own markup is kept in place of the component
// wrapper, since the more specific attributes are worth preserving.
siblings.push(
hasAttributes(child) ? child : withChildren(component, child.children),
);
continue;
}
// Anything else block-level cannot live in a text element at all, so it
// moves out beside the component, exactly as the parser would place it.
for (const piece of partitionAroundBlocks([child])) {
if (piece.block) {
flushInlineRun();
siblings.push(piece.block);
} else {
inlineRun.push(...piece.inline);
}
}
}
flushInlineRun();
return siblings.length ? siblings : [component];
};
// A paragraph reached through inline wrappers is invalid too
// (`<p><em><p>text</p></em></p>`), so drop it and keep its content in place.
// Block-level children are left alone: they get hoisted out of the paragraph
// afterwards, and paragraphs are legal once inside them.
const unwrapInlineParagraphs = (node) => {
if (!Array.isArray(node.children)) return;
const children = [];
for (const child of node.children) {
if (isBlockLevel(child) && !isParagraph(child)) {
children.push(child);
continue;
}
unwrapInlineParagraphs(child);
if (isParagraph(child)) children.push(...child.children);
else children.push(child);
}
node.children = children;
};
const withChildren = (node, children) => {
const copy = { ...node, children };
delete copy.position;
return copy;
};
// A block reached through inline wrappers (`<p><em>a<div>B</div>c</em></p>`)
// closes the paragraph just as a direct child would, so it has to be found at
// any inline depth.
const containsBlockLevel = (node) =>
Array.isArray(node.children) &&
node.children.some(
(child) => isBlockLevel(child) || containsBlockLevel(child),
);
// Separate a run of children into the inline pieces and block-level pieces the
// parser would end up with, splitting inline wrappers around any block they
// contain: `<em>a<div>B</div>c</em>` becomes `<em>a</em>`, `<div>B</div>`,
// `<em>c</em>`, which is what the parser builds when it reconstructs the
// formatting element after the block.
const partitionAroundBlocks = (children) => {
const pieces = [];
let inlineRun = [];
const flushInlineRun = () => {
if (inlineRun.length) {
pieces.push({ inline: inlineRun });
inlineRun = [];
}
};
for (const child of children) {
if (isBlockLevel(child)) {
flushInlineRun();
pieces.push({ block: child });
} else if (containsBlockLevel(child)) {
for (const piece of partitionAroundBlocks(child.children)) {
if (piece.block) {
flushInlineRun();
pieces.push(piece);
} else {
inlineRun.push(withChildren(child, piece.inline));
}
}
} else {
inlineRun.push(child);
}
}
flushInlineRun();
return pieces;
};
// Turn one paragraph into the sequence of siblings a browser would produce:
// inline runs stay in a paragraph, block-level content becomes a sibling.
const splitParagraph = (paragraph) => {
const siblings = [];
let isFirstSegment = true;
for (const piece of partitionAroundBlocks(paragraph.children)) {
if (piece.block) {
siblings.push(piece.block);
continue;
}
if (piece.inline.every(isBlankText)) continue;
if (isFirstSegment) {
paragraph.children = piece.inline;
siblings.push(paragraph);
isFirstSegment = false;
} else {
siblings.push(withChildren(paragraph, piece.inline));
}
}
return siblings;
};
// Group each run of children the parser would gather under an implied element.
const wrapRunsOfChildren = (node, belongsInWrapper, wrapperTagName) => {
if (!node.children.some(belongsInWrapper)) return;
const children = [];
let run = [];
const flushRun = () => {
if (run.length) {
children.push({
type: "element",
tagName: wrapperTagName,
properties: {},
children: run,
});
run = [];
}
};
for (const child of node.children) {
if (belongsInWrapper(child)) run.push(child);
else {
flushRun();
children.push(child);
}
}
flushRun();
node.children = children;
};
// Insert the <tbody> and <tr> the parser would, so the markup round-trips.
const fixTableStructure = (node) => {
const tagName = lowerTagNameOf(node);
if (!TABLE_INTERNAL_TAGS.has(tagName)) return;
const isCell = (child) => TABLE_CELL_TAGS.has(lowerTagNameOf(child));
const isRow = (child) => lowerTagNameOf(child) === "tr";
if (TABLE_SECTION_TAGS.has(tagName)) {
wrapRunsOfChildren(node, isCell, "tr");
} else if (tagName === "table") {
// A bare cell implies a row as well as a section, so wrap cells first.
wrapRunsOfChildren(node, isCell, "tr");
wrapRunsOfChildren(node, isRow, "tbody");
}
};
// Text between a table's rows and cells belongs to no cell, so the parser
// "foster parents" it: the text is moved out to just before the table. Doing
// the same here keeps the emitted markup and the parsed DOM in step. Whitespace
// is simply dropped, since it renders as nothing once outside the table.
const fosterParentStrayText = (table) => {
const strayText = [];
const collect = (node) => {
if (!TABLE_INTERNAL_TAGS.has(lowerTagNameOf(node))) return;
node.children = node.children.filter((child) => {
if (child.type !== "text") {
collect(child);
return true;
}
if (!isBlankText(child)) strayText.push(child);
return false;
});
};
collect(table);
return strayText;
};
// Depth-first, so a paragraph that only becomes invalid after its own children
// are normalised is still handled by its parent on the way back up.
const fixParagraphNesting = (node) => {
if (!node || !Array.isArray(node.children)) return;
node.children.forEach(fixParagraphNesting);
fixTableStructure(node);
const children = [];
for (const child of node.children) {
if (!Array.isArray(child.children)) {
children.push(child);
continue;
}
if (lowerTagNameOf(child) === "table") {
children.push(...fosterParentStrayText(child), child);
continue;
}
if (isParagraphComponent(child)) {
children.push(...liftParagraphsFromComponent(child));
continue;
}
if (!isParagraph(child)) {
children.push(child);
continue;
}
const paragraph = collapseSoleNestedParagraph(child);
unwrapInlineParagraphs(paragraph);
if (containsBlockLevel(paragraph))
children.push(...splitParagraph(paragraph));
else children.push(paragraph);
}
node.children = children;
};
const rehypeFixDomNesting = () => (tree) => {
fixParagraphNesting(tree);
return tree;
};
module.exports = rehypeFixDomNesting;
module.exports.BLOCK_LEVEL_TAGS = BLOCK_LEVEL_TAGS;
module.exports.PARAGRAPH_COMPONENTS = PARAGRAPH_COMPONENTS;