9595 * @property {StylePropertyNameCase | null | undefined } [stylePropertyNameCase='dom']
9696 * Specify casing to use for property names in `style` objects (default:
9797 * `'dom'`).
98+ * @property {boolean | null | undefined } [tableCellAlignToStyle=true]
99+ * Turn obsolete `align` props on `td` and `th` into CSS `style` props
100+ * (default: `true`).
98101 *
99102 * @typedef RuntimeDevelopment
100103 * Runtime fields when development is on.
175178 * Current schema.
176179 * @property {StylePropertyNameCase } stylePropertyNameCase
177180 * Casing to use for property names in `style` objects.
181+ * @property {boolean } tableCellAlignToStyle
182+ * Turn obsolete `align` props on `td` and `th` into CSS `style` props.
178183 *
179184 * @typedef {Record<string, string> } Style
180185 * Style map.
@@ -229,6 +234,8 @@ const dashSomething = /-([a-z])/g
229234// See: <https://github.com/rehypejs/rehype-react/pull/45>.
230235const tableElements = new Set ( [ 'table' , 'tbody' , 'thead' , 'tfoot' , 'tr' ] )
231236
237+ const tableCellElement = new Set ( [ 'td' , 'th' ] )
238+
232239/**
233240 * Transform a hast tree to preact, react, solid, svelte, vue, etc.,
234241 * with an automatic JSX runtime.
@@ -281,7 +288,8 @@ export function toJsxRuntime(tree, options) {
281288 passKeys : options . passKeys !== false ,
282289 passNode : options . passNode || false ,
283290 schema : options . space === 'svg' ? svg : html ,
284- stylePropertyNameCase : options . stylePropertyNameCase || 'dom'
291+ stylePropertyNameCase : options . stylePropertyNameCase || 'dom' ,
292+ tableCellAlignToStyle : options . tableCellAlignToStyle !== false
285293 }
286294
287295 const result = one ( state , tree , undefined )
@@ -479,6 +487,9 @@ function createProperties(state, ancestors) {
479487 let prop
480488
481489 if ( 'properties' in node && node . properties ) {
490+ /** @type {string | undefined } */
491+ let alignValue
492+
482493 for ( prop in node . properties ) {
483494 if ( prop !== 'children' && own . call ( node . properties , prop ) ) {
484495 const result = createProperty (
@@ -489,10 +500,34 @@ function createProperties(state, ancestors) {
489500 )
490501
491502 if ( result ) {
492- props [ result [ 0 ] ] = result [ 1 ]
503+ const [ key , value ] = result
504+
505+ if (
506+ state . tableCellAlignToStyle &&
507+ key === 'align' &&
508+ typeof value === 'string' &&
509+ tableCellElement . has ( node . tagName )
510+ ) {
511+ alignValue = value
512+ } else {
513+ props [ key ] = value
514+ }
493515 }
494516 }
495517 }
518+
519+ if ( alignValue ) {
520+ // Assume style is an object.
521+ const style = /** @type {Style } */ ( props . style || ( props . style = { } ) )
522+ const cssField = 'text-align'
523+ style [
524+ state . stylePropertyNameCase === 'css'
525+ ? // Note: test this when Solid doesn’t want to merge my upcoming PR.
526+ /* c8 ignore next */
527+ transformStyleToCssCasing ( cssField )
528+ : cssField
529+ ] = alignValue
530+ }
496531 }
497532
498533 return props
@@ -538,7 +573,7 @@ function createProperty(state, ancestors, prop, value) {
538573 : parseStyle ( state , ancestors , String ( value ) )
539574
540575 if ( state . stylePropertyNameCase === 'css' ) {
541- styleObject = transformStyleToCssCasing ( styleObject )
576+ styleObject = transformStylesToCssCasing ( styleObject )
542577 }
543578
544579 return [ 'style' , styleObject ]
@@ -619,24 +654,34 @@ function parseStyle(state, ancestors, value) {
619654 * @param {Style } domCasing
620655 * @returns {Style }
621656 */
622- function transformStyleToCssCasing ( domCasing ) {
657+ function transformStylesToCssCasing ( domCasing ) {
623658 /** @type {Style } */
624659 const cssCasing = { }
625660 /** @type {string } */
626661 let from
627662
628663 for ( from in domCasing ) {
629664 if ( own . call ( domCasing , from ) ) {
630- let to = from . replace ( cap , toDash )
631- // Handle `ms-xxx` -> `-ms-xxx`.
632- if ( to . slice ( 0 , 3 ) === 'ms-' ) to = '-' + to
633- cssCasing [ to ] = domCasing [ from ]
665+ cssCasing [ transformStyleToCssCasing ( from ) ] = domCasing [ from ]
634666 }
635667 }
636668
637669 return cssCasing
638670}
639671
672+ /**
673+ * Transform a DOM casing style field to a CSS casing style field.
674+ *
675+ * @param {string } from
676+ * @returns {string }
677+ */
678+ function transformStyleToCssCasing ( from ) {
679+ let to = from . replace ( cap , toDash )
680+ // Handle `ms-xxx` -> `-ms-xxx`.
681+ if ( to . slice ( 0 , 3 ) === 'ms-' ) to = '-' + to
682+ return to
683+ }
684+
640685/**
641686 * Make `$1` capitalized.
642687 *
0 commit comments