@@ -168,18 +168,74 @@ function normalizeMarkdownIndentation(content) {
168168 return result . join ( '\n' ) ;
169169}
170170
171- function normalizeMarkdownTables ( content ) {
171+ function splitTableRow ( line ) {
172+ return line . trim ( ) . replace ( / ^ \| / , '' ) . replace ( / \| $ / , '' ) . split ( '|' ) . map ( ( cell ) => cell . trim ( ) ) ;
173+ }
174+
175+ function isTableSeparator ( line ) {
176+ const cells = splitTableRow ( line ) ;
177+ return cells . length >= 2 && cells . every ( ( cell ) => / ^ : ? - { 3 , } : ? $ / . test ( cell ) ) ;
178+ }
179+
180+ function isTableRow ( line ) {
181+ const trimmed = line . trim ( ) ;
182+ return trimmed . includes ( '|' ) && ! trimmed . startsWith ( '```' ) ;
183+ }
184+
185+ function escapeHtml ( value ) {
186+ return value
187+ . replace ( / & / g, '&' )
188+ . replace ( / < / g, '<' )
189+ . replace ( / > / g, '>' )
190+ . replace ( / " / g, '"' ) ;
191+ }
192+
193+ function renderTableCell ( value ) {
194+ return escapeHtml ( value )
195+ . replace ( / ` ( [ ^ ` ] + ) ` / g, '<code>$1</code>' )
196+ . replace ( / \* \* ( [ ^ * ] + ) \* \* / g, '<strong>$1</strong>' )
197+ . replace ( / \[ ( [ ^ \] ] + ) \] \( ( [ ^ ) ] + ) \) / g, '<a href="$2">$1</a>' ) ;
198+ }
199+
200+ function markdownTableToHtml ( rows ) {
201+ const header = splitTableRow ( rows [ 0 ] ) ;
202+ const body = rows . slice ( 2 ) . map ( splitTableRow ) ;
203+ const headHtml = header . map ( ( cell ) => `<th>${ renderTableCell ( cell ) } </th>` ) . join ( '' ) ;
204+ const bodyHtml = body
205+ . map ( ( row ) => `<tr>${ row . map ( ( cell ) => `<td>${ renderTableCell ( cell ) } </td>` ) . join ( '' ) } </tr>` )
206+ . join ( '\n' ) ;
207+ return `<table>\n<thead>\n<tr>${ headHtml } </tr>\n</thead>\n<tbody>\n${ bodyHtml } \n</tbody>\n</table>` ;
208+ }
209+
210+ function convertMarkdownTables ( content ) {
172211 const lines = content . split ( '\n' ) ;
173- return lines . map ( ( line , index ) => {
174- const trimmed = line . trim ( ) ;
175- const prev = lines [ index - 1 ] ?. trim ( ) || '' ;
176- const next = lines [ index + 1 ] ?. trim ( ) || '' ;
177- if ( ! trimmed . includes ( '|' ) || ! prev . includes ( '|' ) || ! next . includes ( '|' ) ) return line ;
178-
179- const cells = trimmed . replace ( / ^ \| / , '' ) . replace ( / \| $ / , '' ) . split ( '|' ) ;
180- if ( cells . length < 2 || ! cells . every ( ( cell ) => / ^ \s * : ? - { 3 , } : ? \s * $ / . test ( cell ) ) ) return line ;
181- return `| ${ cells . map ( ( ) => '---' ) . join ( ' | ' ) } |` ;
182- } ) . join ( '\n' ) ;
212+ const result = [ ] ;
213+ let inFence = false ;
214+
215+ for ( let index = 0 ; index < lines . length ; index += 1 ) {
216+ const line = lines [ index ] ;
217+ if ( line . trimStart ( ) . startsWith ( '```' ) ) {
218+ inFence = ! inFence ;
219+ result . push ( line ) ;
220+ continue ;
221+ }
222+
223+ if ( ! inFence && isTableRow ( line ) && isTableSeparator ( lines [ index + 1 ] || '' ) ) {
224+ const tableRows = [ line , lines [ index + 1 ] ] ;
225+ index += 2 ;
226+ while ( index < lines . length && isTableRow ( lines [ index ] ) && lines [ index ] . trim ( ) ) {
227+ tableRows . push ( lines [ index ] ) ;
228+ index += 1 ;
229+ }
230+ index -= 1 ;
231+ result . push ( markdownTableToHtml ( tableRows ) ) ;
232+ continue ;
233+ }
234+
235+ result . push ( line ) ;
236+ }
237+
238+ return result . join ( '\n' ) ;
183239}
184240
185241function shouldPrefixDescription ( output , description ) {
@@ -220,7 +276,7 @@ function mdxToMarkdown(content) {
220276 . replace ( / < \/ ( N o t e | T i p | W a r n i n g | I n f o | C h e c k | W a r n i n g ) > / g, '\n' )
221277 . replace ( / < \/ ? ( S t e p s | T a b s | A c c o r d i o n G r o u p | C a r d G r o u p | C o d e G r o u p | F r a m e ) \b [ ^ > ] * > / g, '\n' ) ;
222278
223- output = normalizeMarkdownTables ( normalizeMarkdownIndentation ( output ) )
279+ output = convertMarkdownTables ( normalizeMarkdownIndentation ( output ) )
224280 . replace ( / < i m g \b ( [ ^ > ] * ) > / g, ( _tag , attrs ) => {
225281 const alt = getAttr ( attrs , 'alt' ) || 'image' ;
226282 const src = getAttr ( attrs , 'src' ) ;
0 commit comments