Skip to content

Commit b660735

Browse files
committed
Fix to filter whitespace in tables
1 parent 0682f97 commit b660735

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

Diff for: lib/index.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,23 @@ import {stringify as spaces} from 'space-separated-tokens'
177177
import styleToObject from 'style-to-object'
178178
import {pointStart} from 'unist-util-position'
179179
import {VFileMessage} from 'vfile-message'
180+
import {whitespace} from 'hast-util-whitespace'
180181

181182
const own = {}.hasOwnProperty
182183

184+
// `react-dom` triggers a warning for *any* white space in tables.
185+
// To follow GFM, `mdast-util-to-hast` injects line endings between elements.
186+
// Other tools might do so too, but they don’t do here, so we remove all of
187+
// that.
188+
189+
// See: <https://github.com/facebook/react/pull/7081>.
190+
// See: <https://github.com/facebook/react/pull/7515>.
191+
// See: <https://github.com/remarkjs/remark-react/issues/64>.
192+
// See: <https://github.com/rehypejs/rehype-react/pull/29>.
193+
// See: <https://github.com/rehypejs/rehype-react/pull/32>.
194+
// See: <https://github.com/rehypejs/rehype-react/pull/45>.
195+
const tableElements = new Set(['table', 'thead', 'tbody', 'tfoot', 'tr'])
196+
183197
/**
184198
* Transform a hast tree to preact, react, solid, svelte, vue, etc.,
185199
* with an automatic JSX runtime.
@@ -272,14 +286,21 @@ function one(state, node, key) {
272286
state.schema = schema
273287
}
274288

275-
const children = createChildren(state, node)
289+
let children = createChildren(state, node)
276290
const props = createProperties(state, node)
291+
let type = state.Fragment
277292

278-
let type = node.type === 'root' ? state.Fragment : node.tagName
293+
if (node.type === 'element') {
294+
if (tableElements.has(node.tagName)) {
295+
children = children.filter((child) => !whitespace(child))
296+
}
279297

280-
if (typeof type === 'string' && own.call(state.components, type)) {
281-
const key = /** @type {keyof JSX.IntrinsicElements} */ (type)
282-
type = state.components[key]
298+
if (own.call(state.components, node.tagName)) {
299+
const key = /** @type {keyof JSX.IntrinsicElements} */ (node.tagName)
300+
type = state.components[key]
301+
} else {
302+
type = node.tagName
303+
}
283304
}
284305

285306
props.children = children

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@types/hast": "^2.0.0",
4040
"@types/unist": "^2.0.0",
4141
"comma-separated-tokens": "^2.0.0",
42+
"hast-util-whitespace": "^2.0.0",
4243
"property-information": "^6.0.0",
4344
"space-separated-tokens": "^2.0.0",
4445
"style-to-object": "^0.4.1",

Diff for: test/index.js

+35
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,38 @@ test('components', () => {
401401
'should support class components'
402402
)
403403
})
404+
405+
test('react specific: filter whitespace in tables', () => {
406+
assert.equal(
407+
renderToStaticMarkup(
408+
toJsxRuntime(
409+
h(null, [
410+
h('table', [
411+
' ',
412+
h('thead', [
413+
' ',
414+
h('tr', [' ', h('th', [' ', h('b', 'a'), ' ']), ' ']),
415+
' '
416+
]),
417+
' ',
418+
h('tbody', [
419+
' ',
420+
h('tr', [' ', h('td', [' ', h('b', 'b'), ' ']), ' ']),
421+
' '
422+
]),
423+
' ',
424+
h('tfoot', [
425+
' ',
426+
h('tr', [' ', h('td', [' ', h('b', 'c'), ' ']), ' ']),
427+
' '
428+
]),
429+
' '
430+
])
431+
]),
432+
production
433+
)
434+
),
435+
'<table><thead><tr><th> <b>a</b> </th></tr></thead><tbody><tr><td> <b>b</b> </td></tr></tbody><tfoot><tr><td> <b>c</b> </td></tr></tfoot></table>',
436+
'should ignore whitespace in `table`, `thead`, `tbody`, `tfoot`, and `tr`'
437+
)
438+
})

0 commit comments

Comments
 (0)