Skip to content

Commit 5db2f94

Browse files
authored
fix: label table cells without assuming their position (#1256)
1 parent ff4d9ad commit 5db2f94

3 files changed

Lines changed: 119 additions & 8 deletions

File tree

‎.changeset/table-whitespace.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@doc-kit/generator-react': patch
3+
---
4+
5+
Fix a crash when a table's rows and cells are surrounded by whitespace text nodes, as `rehype-raw` 9.1.1 and later keep them. The transformer now finds the header row and the cells themselves instead of assuming they are the first children, and labels cells by their column.

‎packages/react/src/jsx-ast/plugins/__tests__/transformer.test.mjs‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,97 @@ import { describe, it } from 'node:test';
33

44
import transformer from '../transformer.mjs';
55

6+
const text = value => ({ type: 'text', value });
7+
8+
const element = (tagName, children) => ({
9+
type: 'element',
10+
tagName,
11+
properties: {},
12+
children,
13+
});
14+
15+
const cell = (tagName, value) => element(tagName, [text(value)]);
16+
17+
/**
18+
* Builds a row whose cells are surrounded by whitespace text nodes, as
19+
* `rehype-raw` leaves them.
20+
*/
21+
const spacedRow = (tagName, values) =>
22+
element('tr', [
23+
text('\n'),
24+
...values.flatMap(value => [cell(tagName, value), text('\n')]),
25+
]);
26+
27+
/**
28+
* Builds a table whose rows and cells are surrounded by whitespace text nodes.
29+
*/
30+
const spacedTable = () =>
31+
element('table', [
32+
text('\n'),
33+
element('thead', [
34+
text('\n'),
35+
spacedRow('th', ['Name', 'Value']),
36+
text('\n'),
37+
]),
38+
text('\n'),
39+
element('tbody', [
40+
text('\n'),
41+
spacedRow('td', ['a', '1']),
42+
text('\n'),
43+
spacedRow('td', ['b', '2']),
44+
text('\n'),
45+
]),
46+
text('\n'),
47+
]);
48+
649
describe('jsx-ast transformer', () => {
50+
it('labels cells when whitespace surrounds the header row', () => {
51+
const tree = { type: 'root', children: [spacedTable()] };
52+
53+
transformer()(tree);
54+
55+
const [container] = tree.children;
56+
const table = container.children[0];
57+
const tbody = table.children.find(el => el.tagName === 'tbody');
58+
const labels = tbody.children
59+
.filter(el => el.tagName === 'tr')
60+
.flatMap(tr => tr.children.filter(el => el.tagName === 'td'))
61+
.map(td => td.properties['data-label']);
62+
63+
assert.equal(container.tagName, 'div');
64+
assert.deepEqual(container.properties.className, ['overflow-container']);
65+
assert.deepEqual(labels, ['Name', 'Value', 'Name', 'Value']);
66+
});
67+
68+
it('does not throw when the header has no row', () => {
69+
const tree = {
70+
type: 'root',
71+
children: [
72+
element('table', [
73+
element('thead', []),
74+
element('tbody', [spacedRow('td', ['a'])]),
75+
]),
76+
],
77+
};
78+
79+
assert.doesNotThrow(() => transformer()(tree));
80+
});
81+
82+
it('does not throw when a table with a header has no body', () => {
83+
const tree = {
84+
type: 'root',
85+
children: [
86+
element('table', [
87+
text('\n'),
88+
element('thead', [spacedRow('th', ['a'])]),
89+
text('\n'),
90+
]),
91+
],
92+
};
93+
94+
assert.doesNotThrow(() => transformer()(tree));
95+
});
96+
797
it('moves generated footnotes into the page content fragment', () => {
898
const content = {
999
type: 'mdxJsxFlowElement',

‎packages/react/src/jsx-ast/plugins/transformer.mjs‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ const isFootnotesSection = node =>
1313
(node.properties?.dataFootnotes !== undefined ||
1414
node.properties?.className?.includes('footnotes'));
1515

16+
/**
17+
* Whether a HAST node is a table cell.
18+
* @param {import('hast').Element} node
19+
*/
20+
const isTableCell = node => node.tagName === 'td' || node.tagName === 'th';
21+
1622
/**
1723
* Finds the JSX fragment wrapping the page content (see `buildContent`).
1824
* @param {import('hast').Root} tree
@@ -50,18 +56,28 @@ const transformer = tree => {
5056
// Not every table will have a header, so only do this on tables
5157
// with them.
5258
const thead = node.children.find(el => el.tagName === 'thead');
59+
const tbody = node.children.find(el => el.tagName === 'tbody');
5360

54-
if (thead) {
61+
if (thead && tbody) {
62+
// Table rows and cells don't have to be the first children, nor be
63+
// adjacent: `rehype-raw` keeps the whitespace between them as text
64+
// nodes.
5565
// TODO(@avivkeller): These are only strings afaict, so a `toString` dependency
5666
// might not actually be needed.
57-
const headers = thead.children[0].children.map(toString);
58-
const tbody = node.children.find(el => el.tagName === 'tbody');
67+
const headerRow = thead.children.find(el => el.tagName === 'tr');
68+
const headers =
69+
headerRow?.children.filter(isTableCell).map(toString) ?? [];
70+
71+
for (const tr of tbody.children.filter(el => el.tagName === 'tr')) {
72+
const cells = tr.children.filter(isTableCell);
5973

60-
visit(
61-
tbody,
62-
node => node.tagName === 'td',
63-
(node, index) => (node.properties['data-label'] = headers[index])
64-
);
74+
for (const [column, cell] of cells.entries()) {
75+
if (cell.tagName === 'td' && headers[column] !== undefined) {
76+
cell.properties ||= {};
77+
cell.properties['data-label'] = headers[column];
78+
}
79+
}
80+
}
6581
}
6682
}
6783
});

0 commit comments

Comments
 (0)