Skip to content

Commit 764d3a2

Browse files
authoredFeb 9, 2023
Remove whitespace between unraveled text nodes
Closes GH-2000. Closes GH-2252.
1 parent d0a65fc commit 764d3a2

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed
 

Diff for: ‎packages/mdx/lib/plugin/remark-mark-and-unravel.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
2+
* @typedef {import('mdast').Content} Content
23
* @typedef {import('mdast').Root} Root
34
*
45
* @typedef {import('remark-mdx')} DoNotTouchAsThisImportItIncludesMdxInTree
@@ -47,6 +48,9 @@ export function remarkMarkAndUnravel() {
4748
if (all && oneOrMore) {
4849
offset = -1
4950

51+
/** @type {Array<Content>} */
52+
const newChildren = []
53+
5054
while (++offset < children.length) {
5155
const child = children[offset]
5256

@@ -59,9 +63,18 @@ export function remarkMarkAndUnravel() {
5963
// @ts-expect-error: content model is fine.
6064
child.type = 'mdxFlowExpression'
6165
}
66+
67+
if (
68+
child.type === 'text' &&
69+
/^[\t\r\n ]+$/.test(String(child.value))
70+
) {
71+
// Empty.
72+
} else {
73+
newChildren.push(child)
74+
}
6275
}
6376

64-
parent.children.splice(index, 1, ...children)
77+
parent.children.splice(index, 1, ...newChildren)
6578
return index
6679
}
6780
}

Diff for: ‎packages/mdx/test/compile.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ test('MDX (JSX)', async () => {
12561256
renderToStaticMarkup(
12571257
React.createElement(await run(compileSync('<a>b</a>\t<b>c</b>')))
12581258
),
1259-
'<a>b</a>\n\t\n<b>c</b>',
1259+
'<a>b</a>\n<b>c</b>',
12601260
'should unravel JSX (text) and whitespace as only children'
12611261
)
12621262

@@ -1363,6 +1363,46 @@ test('MDX (JSX)', async () => {
13631363
'<i>the sum of one and one is: 2</i>',
13641364
'should support JSX in expressions'
13651365
)
1366+
1367+
// Important: there should not be whitespace in the `tr`.
1368+
// This is normally not present, but unraveling makes this a bit more complex.
1369+
// See: <https://github.com/mdx-js/mdx/issues/2000>.
1370+
assert.equal(
1371+
compileSync(`<table>
1372+
<thead>
1373+
<tr>
1374+
<th>a</th>
1375+
<th>b</th>
1376+
</tr>
1377+
</thead>
1378+
</table>`).value,
1379+
[
1380+
'/*@jsxRuntime automatic @jsxImportSource react*/',
1381+
'import {jsx as _jsx, jsxs as _jsxs} from "react/jsx-runtime";',
1382+
'function _createMdxContent(props) {',
1383+
' return _jsx("table", {',
1384+
' children: _jsx("thead", {',
1385+
' children: _jsxs("tr", {',
1386+
' children: [_jsx("th", {',
1387+
' children: "a"',
1388+
' }), _jsx("th", {',
1389+
' children: "b"',
1390+
' })]',
1391+
' })',
1392+
' })',
1393+
' });',
1394+
'}',
1395+
'function MDXContent(props = {}) {',
1396+
' const {wrapper: MDXLayout} = props.components || ({});',
1397+
' return MDXLayout ? _jsx(MDXLayout, Object.assign({}, props, {',
1398+
' children: _jsx(_createMdxContent, props)',
1399+
' })) : _createMdxContent(props);',
1400+
'}',
1401+
'export default MDXContent;',
1402+
''
1403+
].join('\n'),
1404+
'should support JSX in expressions'
1405+
)
13661406
})
13671407

13681408
test('MDX (ESM)', async () => {

1 commit comments

Comments
 (1)

vercel[bot] commented on Feb 9, 2023

@vercel[bot]

Successfully deployed to the following URLs:

mdx – ./

mdx-mdx.vercel.app
mdxjs.com
mdx-git-main-mdx.vercel.app
v2.mdxjs.com

Please sign in to comment.