@@ -78,9 +78,12 @@ function createStyleSpan(params: MarkdownStyleSpan): MarkdownStyleSpan {
7878 return span ;
7979}
8080
81+ export type MarkdownTableAlignment = "left" | "center" | "right" ;
82+
8183export type MarkdownTableData = {
8284 headers : string [ ] ;
8385 rows : string [ ] [ ] ;
86+ aligns ?: ( MarkdownTableAlignment | undefined ) [ ] ;
8487} ;
8588
8689export type MarkdownTableCell = {
@@ -113,6 +116,7 @@ type TableCell = MarkdownTableCell;
113116type TableState = {
114117 headers : TableCell [ ] ;
115118 rows : TableCell [ ] [ ] ;
119+ aligns : ( MarkdownTableAlignment | undefined ) [ ] ;
116120 currentRow : TableCell [ ] ;
117121 currentCell : RenderTarget | null ;
118122 inHeader : boolean ;
@@ -172,6 +176,20 @@ function getAttr(token: MarkdownToken, name: string): string | null {
172176 return null ;
173177}
174178
179+ function markdownTableAlignmentFromToken ( token : MarkdownToken ) : MarkdownTableAlignment | undefined {
180+ const value = getAttr ( token , "style" ) ?? "" ;
181+ if ( / t e x t - a l i g n \s * : \s * l e f t / i. test ( value ) ) {
182+ return "left" ;
183+ }
184+ if ( / t e x t - a l i g n \s * : \s * c e n t e r / i. test ( value ) ) {
185+ return "center" ;
186+ }
187+ if ( / t e x t - a l i g n \s * : \s * r i g h t / i. test ( value ) ) {
188+ return "right" ;
189+ }
190+ return undefined ;
191+ }
192+
175193function createTextToken ( base : MarkdownToken , content : string ) : MarkdownToken {
176194 return { ...base , type : "text" , content, children : undefined } ;
177195}
@@ -432,6 +450,7 @@ function initTableState(): TableState {
432450 return {
433451 headers : [ ] ,
434452 rows : [ ] ,
453+ aligns : [ ] ,
435454 currentRow : [ ] ,
436455 currentCell : null ,
437456 inHeader : false ,
@@ -517,13 +536,15 @@ function collectTableBlock(state: RenderState) {
517536 }
518537 const headerCells = state . table . headers . map ( trimCell ) ;
519538 const rowCells = state . table . rows . map ( ( row ) => row . map ( trimCell ) ) ;
520- state . collectedTables . push ( {
539+ const table = {
521540 headers : headerCells . map ( ( cell ) => cell . text ) ,
522541 rows : rowCells . map ( ( row ) => row . map ( ( cell ) => cell . text ) ) ,
523542 headerCells,
524543 rowCells,
525544 placeholderOffset : state . text . length ,
526- } ) ;
545+ ...( state . table . aligns . some ( Boolean ) ? { aligns : [ ...state . table . aligns ] } : { } ) ,
546+ } ;
547+ state . collectedTables . push ( table ) ;
527548}
528549
529550function appendTableBulletValue (
@@ -874,6 +895,10 @@ function renderTokens(tokens: MarkdownToken[], state: RenderState): void {
874895 case "td_open" :
875896 if ( state . table ) {
876897 state . table . currentCell = initRenderTarget ( ) ;
898+ if ( token . type === "th_open" && state . table . inHeader ) {
899+ state . table . aligns [ state . table . currentRow . length ] =
900+ markdownTableAlignmentFromToken ( token ) ;
901+ }
877902 }
878903 break ;
879904 case "th_close" :
0 commit comments