From b84c624d9c8cb7344d64757dec0dbff6305b045a Mon Sep 17 00:00:00 2001 From: John <12599934+jorroll@users.noreply.github.com> Date: Fri, 14 Apr 2023 07:40:06 -0600 Subject: [PATCH] Add `fixTableCellAlign` option Closes GH-47. Closes GH-48. Reviewed-by: Titus Wormer --- lib/index.js | 16 ++++++++++++++-- readme.md | 7 +++++++ test.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 32f7d45..c1a5439 100644 --- a/lib/index.js +++ b/lib/index.js @@ -19,6 +19,12 @@ * You should typically pass `React.Fragment`. * @property {string|undefined} [prefix='h-'] * React key prefix. + * @property {boolean|undefined} [fixTableCellAlign=true] + * Fix obsolete align attributes on table cells by turning them + * into inline styles. + * Keep it on when working with markdown, turn it off when working + * with markup for emails. + * The default is `true`. * * @typedef {SharedOptions & (import('./complex-types').ComponentsWithNodeOptions|import('./complex-types').ComponentsWithoutNodeOptions)} Options * Configuration. @@ -51,13 +57,19 @@ export default function rehypeReact(options) { const createElement = options.createElement + const fixTableCellAlign = options.fixTableCellAlign !== false + Object.assign(this, {Compiler: compiler}) /** @type {import('unified').CompilerFunction} */ function compiler(node) { /** @type {ReactNode} */ - // @ts-expect-error: assume `name` is a known element. - let result = toH(h, tableCellStyle(node), options.prefix) + let result = toH( + // @ts-expect-error: assume `name` is a known element. + h, + fixTableCellAlign ? tableCellStyle(node) : node, + options.prefix + ) if (node.type === 'root') { // Invert . diff --git a/readme.md b/readme.md index d0a44c2..e7e8e67 100644 --- a/readme.md +++ b/readme.md @@ -174,6 +174,13 @@ React key prefix (`string`, default: `'h-'`). Pass the original hast node as `props.node` to custom React components (`boolean`, default: `false`). +###### `options.fixTableCellAlign` + +Fix obsolete align attributes on table cells by turning them +into inline styles (`boolean`, default: `true`). +Keep it on when working with markdown, turn it off when working +with markup for emails. + ## Types This package is fully typed with [TypeScript][]. diff --git a/test.js b/test.js index 5fd9a96..290a705 100644 --- a/test.js +++ b/test.js @@ -215,5 +215,38 @@ test('React ' + React.version, (t) => { 'should expose node from node prop' ) + t.deepEqual( + unified() + .use(rehypeReact, { + createElement: React.createElement, + fixTableCellAlign: false + }) + .stringify( + u('root', [ + h('table', {align: 'top'}, [ + '\n ', + h('tbody', {}, [ + '\n ', + h('tr', {}, [ + h('th', {}, ['\n ']), + h('td', {align: 'center'}, ['\n ']) + ]) + ]) + ]) + ]) + ), + React.createElement('div', {}, [ + React.createElement('table', {key: 'h-1', align: 'top'}, [ + React.createElement('tbody', {key: 'h-2'}, [ + React.createElement('tr', {key: 'h-3'}, [ + React.createElement('th', {key: 'h-4'}, ['\n ']), + React.createElement('td', {key: 'h-5', align: 'center'}, ['\n ']) + ]) + ]) + ]) + ]), + 'should respect mapDepricatedTableCellAttrsToInlineCss option' + ) + t.end() })