1- import phrasing from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
2- import defaultInlineCode from 'mdast-util-to-markdown/lib/handle/inline-code.js'
1+ /**
2+ * @typedef {import('mdast').Table } Table
3+ * @typedef {import('mdast').TableRow } TableRow
4+ * @typedef {import('mdast').TableCell } TableCell
5+ * @typedef {import('mdast').InlineCode } InlineCode
6+ * @typedef {import('markdown-table').MarkdownTableOptions } MarkdownTableOptions
7+ * @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
8+ * @typedef {import('mdast-util-from-markdown').Handle } FromMarkdownHandle
9+ * @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
10+ * @typedef {import('mdast-util-to-markdown').Handle } ToMarkdownHandle
11+ * @typedef {import('mdast-util-to-markdown').Context } ToMarkdownContext
12+ *
13+ * @typedef Options
14+ * @property {boolean } [tableCellPadding=true]
15+ * @property {boolean } [tablePipeAlign=true]
16+ * @property {MarkdownTableOptions['stringLength'] } [stringLength]
17+ */
18+
19+ import { containerPhrasing } from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
20+ import { inlineCode } from 'mdast-util-to-markdown/lib/handle/inline-code.js'
321import { markdownTable } from 'markdown-table'
422
23+ /** @type {FromMarkdownExtension } */
524export const gfmTableFromMarkdown = {
625 enter : {
726 table : enterTable ,
@@ -18,30 +37,37 @@ export const gfmTableFromMarkdown = {
1837 }
1938}
2039
40+ /** @type {FromMarkdownHandle } */
2141function enterTable ( token ) {
42+ // @ts -expect-error: `align` is custom.
2243 this . enter ( { type : 'table' , align : token . _align , children : [ ] } , token )
2344 this . setData ( 'inTable' , true )
2445}
2546
47+ /** @type {FromMarkdownHandle } */
2648function exitTable ( token ) {
2749 this . exit ( token )
2850 this . setData ( 'inTable' )
2951}
3052
53+ /** @type {FromMarkdownHandle } */
3154function enterRow ( token ) {
3255 this . enter ( { type : 'tableRow' , children : [ ] } , token )
3356}
3457
58+ /** @type {FromMarkdownHandle } */
3559function exit ( token ) {
3660 this . exit ( token )
3761}
3862
63+ /** @type {FromMarkdownHandle } */
3964function enterCell ( token ) {
4065 this . enter ( { type : 'tableCell' , children : [ ] } , token )
4166}
4267
4368// Overwrite the default code text data handler to unescape escaped pipes when
4469// they are in tables.
70+ /** @type {FromMarkdownHandle } */
4571function exitCodeText ( token ) {
4672 let value = this . resume ( )
4773
@@ -53,11 +79,20 @@ function exitCodeText(token) {
5379 this . exit ( token )
5480}
5581
82+ /**
83+ * @param {string } $0
84+ * @param {string } $1
85+ * @returns {string }
86+ */
5687function replace ( $0 , $1 ) {
5788 // Pipes work, backslashes don’t (but can’t escape pipes).
5889 return $1 === '|' ? $1 : $0
5990}
6091
92+ /**
93+ * @param {Options } [options]
94+ * @returns {ToMarkdownExtension }
95+ */
6196export function gfmTableToMarkdown ( options ) {
6297 const settings = options || { }
6398 const padding = settings . tableCellPadding
@@ -92,27 +127,48 @@ export function gfmTableToMarkdown(options) {
92127 }
93128 }
94129
130+ /**
131+ * @type {ToMarkdownHandle }
132+ * @param {Table } node
133+ */
95134 function handleTable ( node , _ , context ) {
135+ // @ts -expect-error: fixed in `markdown-table@3.0.1`.
96136 return serializeData ( handleTableAsData ( node , context ) , node . align )
97137 }
98138
99- // This function isn’t really used normally, because we handle rows at the
100- // table level.
101- // But, if someone passes in a table row, this ensures we make somewhat sense.
139+ /**
140+ * This function isn’t really used normally, because we handle rows at the
141+ * table level.
142+ * But, if someone passes in a table row, this ensures we make somewhat sense.
143+ *
144+ * @type {ToMarkdownHandle }
145+ * @param {TableRow } node
146+ */
102147 function handleTableRow ( node , _ , context ) {
103148 const row = handleTableRowAsData ( node , context )
104149 // `markdown-table` will always add an align row
105150 const value = serializeData ( [ row ] )
106151 return value . slice ( 0 , value . indexOf ( '\n' ) )
107152 }
108153
154+ /**
155+ * @type {ToMarkdownHandle }
156+ * @param {TableCell } node
157+ */
109158 function handleTableCell ( node , _ , context ) {
110159 const exit = context . enter ( 'tableCell' )
111- const value = phrasing ( node , context , { before : around , after : around } )
160+ const value = containerPhrasing ( node , context , {
161+ before : around ,
162+ after : around
163+ } )
112164 exit ( )
113165 return value
114166 }
115167
168+ /**
169+ * @param {Array.<Array.<string>> } matrix
170+ * @param {Array.<string> } [align]
171+ */
116172 function serializeData ( matrix , align ) {
117173 return markdownTable ( matrix , {
118174 align,
@@ -122,14 +178,18 @@ export function gfmTableToMarkdown(options) {
122178 } )
123179 }
124180
181+ /**
182+ * @param {Table } node
183+ * @param {ToMarkdownContext } context
184+ */
125185 function handleTableAsData ( node , context ) {
126186 const children = node . children
127187 let index = - 1
128- const length = children . length
188+ /** @type { Array.<Array.<string>> } */
129189 const result = [ ]
130190 const subexit = context . enter ( 'table' )
131191
132- while ( ++ index < length ) {
192+ while ( ++ index < children . length ) {
133193 result [ index ] = handleTableRowAsData ( children [ index ] , context )
134194 }
135195
@@ -138,14 +198,18 @@ export function gfmTableToMarkdown(options) {
138198 return result
139199 }
140200
201+ /**
202+ * @param {TableRow } node
203+ * @param {ToMarkdownContext } context
204+ */
141205 function handleTableRowAsData ( node , context ) {
142206 const children = node . children
143207 let index = - 1
144- const length = children . length
208+ /** @type { Array.<string> } */
145209 const result = [ ]
146210 const subexit = context . enter ( 'tableRow' )
147211
148- while ( ++ index < length ) {
212+ while ( ++ index < children . length ) {
149213 result [ index ] = handleTableCell ( children [ index ] , node , context )
150214 }
151215
@@ -154,8 +218,12 @@ export function gfmTableToMarkdown(options) {
154218 return result
155219 }
156220
221+ /**
222+ * @type {ToMarkdownHandle }
223+ * @param {InlineCode } node
224+ */
157225 function inlineCodeWithTable ( node , parent , context ) {
158- let value = defaultInlineCode ( node , parent , context )
226+ let value = inlineCode ( node , parent , context )
159227
160228 if ( context . stack . includes ( 'tableCell' ) ) {
161229 value = value . replace ( / \| / g, '\\$&' )
0 commit comments