Skip to content

Commit

Permalink
Fix to improve removal of whitespace between tables
Browse files Browse the repository at this point in the history
Reviewed-by: Titus Wormer <tituswormer@gmail.com>

Related-to GH-28.
Related-to GH-29.
Closes GH-32.
  • Loading branch information
TheComp44 authored Sep 10, 2021
1 parent 46d3fb4 commit b97da2f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import {toH} from 'hast-to-hyperscript'
// @ts-expect-error: hush.
import tableCellStyle from '@mapbox/hast-util-table-cell-style'
import {whitespace} from 'hast-util-whitespace'

const own = {}.hasOwnProperty
const tableElements = new Set([
Expand Down Expand Up @@ -90,7 +91,7 @@ export default function rehypeReact(options) {
// See: <https://github.com/facebook/react/pull/7515>.
// See: <https://github.com/remarkjs/remark-react/issues/64>.
if (children && tableElements.has(name)) {
children = children.filter((child) => child !== '\n')
children = children.filter((child) => !whitespace(child))
}

if (options.components && own.call(options.components, name)) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@types/hast": "^2.0.0",
"@types/react": "^17.0.0",
"hast-to-hyperscript": "^10.0.0",
"hast-util-whitespace": "^2.0.0",
"unified": "^10.0.0"
},
"devDependencies": {
Expand Down
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,22 @@ test('React ' + React.version, (t) => {
'should transform an element with align property'
)

t.deepEqual(
processor.stringify(
u('root', [
h('table', {}, ['\n ', h('tbody', {}, ['\n ', h('tr', {})])])
])
),
React.createElement('div', {}, [
React.createElement('table', {key: 'h-1'}, [
React.createElement('tbody', {key: 'h-2'}, [
React.createElement('tr', {key: 'h-3'}, undefined)
])
])
]),
'should transform a table with whitespace'
)

const headingNode = h('h1')
/** @param {object} props */
const Heading1 = function (props) {
Expand Down

0 comments on commit b97da2f

Please sign in to comment.