Skip to content

Commit 3a7f194

Browse files
committedOct 19, 2023
mdx: add tableCellAlignToStyle option, to use align
The default behavior remains fine: a CSS `textalign` prop is added on `td`, `th`, instead of an `align` (which is the default for GFM). That’s what React wants: otherwise it warns. But if you use some other framework, there’s now a toggle for it.
1 parent 172e519 commit 3a7f194

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed
 

‎packages/mdx/lib/core.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ const removedOptions = [
7676
*/
7777
export function createProcessor(options) {
7878
const {
79+
SourceMapGenerator,
7980
development,
81+
elementAttributeNameCase,
8082
jsx,
8183
format,
8284
outputFormat,
@@ -85,9 +87,8 @@ export function createProcessor(options) {
8587
rehypePlugins,
8688
remarkPlugins,
8789
remarkRehypeOptions,
88-
elementAttributeNameCase,
8990
stylePropertyNameCase,
90-
SourceMapGenerator,
91+
tableCellAlignToStyle,
9192
...rest
9293
} = options || {}
9394
let index = -1
@@ -136,7 +137,11 @@ export function createProcessor(options) {
136137
}
137138

138139
pipeline
139-
.use(rehypeRecma, {elementAttributeNameCase, stylePropertyNameCase})
140+
.use(rehypeRecma, {
141+
elementAttributeNameCase,
142+
stylePropertyNameCase,
143+
tableCellAlignToStyle
144+
})
140145
.use(recmaDocument, {...rest, outputFormat})
141146
.use(recmaJsxRewrite, {
142147
development,

‎packages/mdx/lib/plugin/rehype-recma.js

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*
2323
* This casing is used for hast elements, not for embedded MDX JSX nodes
2424
* (components that someone authored manually).
25+
* @property {boolean | null | undefined} [tableCellAlignToStyle=true]
26+
* Turn obsolete `align` props on `td` and `th` into CSS `style` props
27+
* (default: `true`).
2528
*
2629
* @typedef {'css' | 'dom'} StylePropertyNameCase
2730
* Casing to use for property names in `style` objects.

‎packages/mdx/readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,11 @@ default: `'dom'`).
751751
This casing is used for hast elements, not for embedded MDX JSX nodes
752752
(components that someone authored manually).
753753

754+
###### `options.tableCellAlignToStyle`
755+
756+
Turn obsolete `align` props on `td` and `th` into CSS `style` props (default:
757+
`true`).
758+
754759
###### Returns
755760

756761
`Promise<VFile>` — Promise that resolves to the compiled JS as a [vfile][].

‎packages/mdx/test/compile.js

+25
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {MDXProvider} from '@mdx-js/react'
1515
import React from 'react'
1616
import {renderToStaticMarkup} from 'react-dom/server'
1717
import rehypeRaw from 'rehype-raw'
18+
import remarkGfm from 'remark-gfm'
1819
import {SourceMapGenerator} from 'source-map'
1920
import {VFile} from 'vfile'
2021
import {run} from './context/run.js'
@@ -1302,6 +1303,30 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
13021303
}
13031304
}
13041305
)
1306+
1307+
await t.test('should support `tableCellAlignToStyle`', async function () {
1308+
assert.match(
1309+
String(
1310+
await compile('| a |\n| :- |', {
1311+
remarkPlugins: [remarkGfm],
1312+
tableCellAlignToStyle: true,
1313+
jsx: true
1314+
})
1315+
),
1316+
/textAlign: "left"/
1317+
)
1318+
1319+
assert.match(
1320+
String(
1321+
await compile('| a |\n| :- |', {
1322+
remarkPlugins: [remarkGfm],
1323+
tableCellAlignToStyle: false,
1324+
jsx: true
1325+
})
1326+
),
1327+
/align="left"/
1328+
)
1329+
})
13051330
})
13061331

13071332
test('@mdx-js/mdx: createProcessor', async function (t) {

0 commit comments

Comments
 (0)
Please sign in to comment.