@@ -25,7 +25,16 @@ export const defaultSerializeMdNodesOptions: SerializeMdOptions['nodes'] = {
2525 code_block : {
2626 type : 'code_block' ,
2727 serialize : ( children , node ) =>
28- `\n\`\`\`${ node . language || '' } \n${ children } \n\`\`\`\n` ,
28+ // eslint-disable-next-line @typescript-eslint/no-base-to-string
29+ `\n\`\`\`${ node . lang || '' } \n${ children } \`\`\`\n` ,
30+ } ,
31+ code_line : {
32+ type : 'code_line' ,
33+ serialize : ( children ) => `${ children } \n` ,
34+ } ,
35+ equation : {
36+ type : 'equation' ,
37+ serialize : ( children , node ) => `$$\n${ node . texExpression } \n$$` ,
2938 } ,
3039 h1 : { type : 'h1' , serialize : ( children ) => `\n# ${ children } \n` } ,
3140 h2 : { type : 'h2' , serialize : ( children ) => `\n## ${ children } \n` } ,
@@ -52,6 +61,10 @@ export const defaultSerializeMdNodesOptions: SerializeMdOptions['nodes'] = {
5261 return `\n\n` ;
5362 } ,
5463 } ,
64+ inline_equation : {
65+ type : 'inline_equation' ,
66+ serialize : ( children , node ) => `$${ node . texExpression } $` ,
67+ } ,
5568 italic : { isLeaf : true , type : 'italic' } ,
5669 li : {
5770 type : 'li' ,
@@ -90,9 +103,15 @@ export const defaultSerializeMdNodesOptions: SerializeMdOptions['nodes'] = {
90103 } ,
91104 p : {
92105 type : 'p' ,
93- serialize : ( children , node , { ulListStyleTypes = [ ] } ) => {
106+ serialize : ( children , node , { nodes , ulListStyleTypes = [ ] } ) => {
94107 const listStyleType = node . listStyleType ;
95108
109+ const isInTableCell =
110+ node . parent ?. type === nodes . td . type ||
111+ node . parent ?. type === nodes . th . type ;
112+
113+ const breakTag = isInTableCell ? `<br />` : `\n` ;
114+
96115 if ( listStyleType ) {
97116 let pre = '' ;
98117
@@ -104,22 +123,69 @@ export const defaultSerializeMdNodesOptions: SerializeMdOptions['nodes'] = {
104123 const listStart = node . listStart ?? 1 ;
105124
106125 const isOL = ! ulListStyleTypes . includes ( listStyleType ) ;
107- const treatAsLeaf =
108- node . children . length === 1 && isLeafNode ( node . children [ 0 ] ) ;
109126
110127 // https://github.com/remarkjs/remark-react/issues/65
111128 if ( isOL && listDepth > 0 ) {
112129 pre += ' ' ;
113130 }
114131
115132 // TODO: support all styles
116- return `${ pre } ${ isOL ? listStart + '.' : '-' } ${ children } ${ treatAsLeaf ? '\n' : '' } ` ;
133+ return `${ pre } ${ isOL ? listStart + '.' : '-' } ${ children } ${ breakTag } ` ;
117134 }
118135
119- return `\n${ children } \n` ;
136+ const pre = isInTableCell ? '' : '\n' ;
137+
138+ return `${ pre } ${ children } ${ breakTag } ` ;
120139 } ,
121140 } ,
122141 strikethrough : { isLeaf : true , type : 'strikethrough' } ,
142+ table : {
143+ type : 'table' ,
144+ serialize : ( children ) => {
145+ const lines = children . split ( '\n' ) . filter ( Boolean ) ;
146+
147+ // Line 0 is the header row
148+ const headerLine = lines [ 0 ] . trim ( ) ;
149+
150+ // Remove extra "|" from both sides
151+ let lineTrimmed = headerLine ;
152+
153+ if ( lineTrimmed . startsWith ( '|' ) ) {
154+ lineTrimmed = lineTrimmed . slice ( 1 ) ;
155+ }
156+ if ( lineTrimmed . endsWith ( '|' ) ) {
157+ lineTrimmed = lineTrimmed . slice ( 0 , - 1 ) ;
158+ }
159+
160+ // Generate "---" separators based on number of columns
161+ const cols = lineTrimmed . split ( '|' ) . length ;
162+ const separator = `| ${ Array ( cols ) . fill ( '---' ) . join ( ' | ' ) } |` ;
163+
164+ // Insert separator line into array
165+ lines . splice ( 1 , 0 , separator ) ;
166+
167+ // Join back into string
168+ return lines . join ( '\n' ) ;
169+ } ,
170+ } ,
171+ td : {
172+ type : 'td' ,
173+ serialize : ( children ) => {
174+ return `| ${ children } ` ;
175+ } ,
176+ } ,
177+ th : {
178+ type : 'th' ,
179+ serialize : ( children ) => {
180+ return `| ${ children } ` ;
181+ } ,
182+ } ,
183+ tr : {
184+ type : 'tr' ,
185+ serialize : ( children ) => {
186+ return `${ children } |\n` ;
187+ } ,
188+ } ,
123189 ul : {
124190 type : 'ul' ,
125191 serialize : ( children , _ , { listDepth } ) => {
0 commit comments