11/**
2+ * @typedef {import('mdast').InlineCode } InlineCode
23 * @typedef {import('mdast').Table } Table
3- * @typedef {import('mdast').TableRow } TableRow
44 * @typedef {import('mdast').TableCell } TableCell
5- * @typedef {import('mdast').InlineCode } InlineCode
5+ * @typedef {import('mdast').TableRow } TableRow
66 *
7- * @typedef {import('markdown-table').MarkdownTableOptions } MarkdownTableOptions
7+ * @typedef {import('markdown-table').Options } MarkdownTableOptions
88 *
99 * @typedef {import('mdast-util-from-markdown').CompileContext } CompileContext
1010 * @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
2020 * @typedef Options
2121 * Configuration.
2222 * @property {boolean | null | undefined } [tableCellPadding=true]
23- * Whether to add a space of padding between delimiters and cells.
23+ * Whether to add a space of padding between delimiters and cells (default:
24+ * `true`).
2425 * @property {boolean | null | undefined } [tablePipeAlign=true]
25- * Whether to align the delimiters.
26+ * Whether to align the delimiters (default: `true`) .
2627 * @property {MarkdownTableOptions['stringLength'] | null | undefined } [stringLength]
2728 * Function to detect the length of table cell content, used when aligning
28- * the delimiters between cells
29+ * the delimiters between cells (optional).
2930 */
3031
32+ import { ok as assert } from 'devlop'
3133import { markdownTable } from 'markdown-table'
3234import { defaultHandlers } from 'mdast-util-to-markdown'
3335
34- // To do: next major: use `state` and `state` utilities from `mdast-util-to-markdown`.
3536// To do: next major: expose functions.
3637
3738/**
@@ -60,13 +61,14 @@ export const gfmTableFromMarkdown = {
6061 * @type {FromMarkdownHandle }
6162 */
6263function enterTable ( token ) {
63- /** @type {Array<'left' | 'right' | 'center' | 'none'> } */
64- // @ts -expect-error: `align` is custom.
6564 const align = token . _align
65+ assert ( align , 'expected `_align` on table' )
6666 this . enter (
6767 {
6868 type : 'table' ,
69- align : align . map ( ( d ) => ( d === 'none' ? null : d ) ) ,
69+ align : align . map ( function ( d ) {
70+ return d === 'none' ? null : d
71+ } ) ,
7072 children : [ ]
7173 } ,
7274 token
@@ -120,7 +122,8 @@ function exitCodeText(token) {
120122 value = value . replace ( / \\ ( [ \\ | ] ) / g, replace )
121123 }
122124
123- const node = /** @type {InlineCode } */ ( this . stack [ this . stack . length - 1 ] )
125+ const node = this . stack [ this . stack . length - 1 ]
126+ assert ( node . type === 'inlineCode' )
124127 node . value = value
125128 this . exit ( token )
126129}
@@ -171,22 +174,19 @@ export function gfmTableToMarkdown(options) {
171174 { atBreak : true , character : '-' , after : '[:|-]' }
172175 ] ,
173176 handlers : {
177+ inlineCode : inlineCodeWithTable ,
174178 table : handleTable ,
175- tableRow : handleTableRow ,
176179 tableCell : handleTableCell ,
177- inlineCode : inlineCodeWithTable
180+ tableRow : handleTableRow
178181 }
179182 }
180183
181184 /**
182185 * @type {ToMarkdownHandle }
183186 * @param {Table } node
184187 */
185- function handleTable ( node , _ , context , safeOptions ) {
186- return serializeData (
187- handleTableAsData ( node , context , safeOptions ) ,
188- node . align
189- )
188+ function handleTable ( node , _ , state , info ) {
189+ return serializeData ( handleTableAsData ( node , state , info ) , node . align )
190190 }
191191
192192 /**
@@ -197,8 +197,8 @@ export function gfmTableToMarkdown(options) {
197197 * @type {ToMarkdownHandle }
198198 * @param {TableRow } node
199199 */
200- function handleTableRow ( node , _ , context , safeOptions ) {
201- const row = handleTableRowAsData ( node , context , safeOptions )
200+ function handleTableRow ( node , _ , state , info ) {
201+ const row = handleTableRowAsData ( node , state , info )
202202 const value = serializeData ( [ row ] )
203203 // `markdown-table` will always add an align row
204204 return value . slice ( 0 , value . indexOf ( '\n' ) )
@@ -208,11 +208,11 @@ export function gfmTableToMarkdown(options) {
208208 * @type {ToMarkdownHandle }
209209 * @param {TableCell } node
210210 */
211- function handleTableCell ( node , _ , context , safeOptions ) {
212- const exit = context . enter ( 'tableCell' )
213- const subexit = context . enter ( 'phrasing' )
214- const value = context . containerPhrasing ( node , {
215- ...safeOptions ,
211+ function handleTableCell ( node , _ , state , info ) {
212+ const exit = state . enter ( 'tableCell' )
213+ const subexit = state . enter ( 'phrasing' )
214+ const value = state . containerPhrasing ( node , {
215+ ...info ,
216216 before : around ,
217217 after : around
218218 } )
@@ -239,22 +239,18 @@ export function gfmTableToMarkdown(options) {
239239
240240 /**
241241 * @param {Table } node
242- * @param {State } context
243- * @param {Info } safeOptions
242+ * @param {State } state
243+ * @param {Info } info
244244 */
245- function handleTableAsData ( node , context , safeOptions ) {
245+ function handleTableAsData ( node , state , info ) {
246246 const children = node . children
247247 let index = - 1
248248 /** @type {Array<Array<string>> } */
249249 const result = [ ]
250- const subexit = context . enter ( 'table' )
250+ const subexit = state . enter ( 'table' )
251251
252252 while ( ++ index < children . length ) {
253- result [ index ] = handleTableRowAsData (
254- children [ index ] ,
255- context ,
256- safeOptions
257- )
253+ result [ index ] = handleTableRowAsData ( children [ index ] , state , info )
258254 }
259255
260256 subexit ( )
@@ -264,26 +260,21 @@ export function gfmTableToMarkdown(options) {
264260
265261 /**
266262 * @param {TableRow } node
267- * @param {State } context
268- * @param {Info } safeOptions
263+ * @param {State } state
264+ * @param {Info } info
269265 */
270- function handleTableRowAsData ( node , context , safeOptions ) {
266+ function handleTableRowAsData ( node , state , info ) {
271267 const children = node . children
272268 let index = - 1
273269 /** @type {Array<string> } */
274270 const result = [ ]
275- const subexit = context . enter ( 'tableRow' )
271+ const subexit = state . enter ( 'tableRow' )
276272
277273 while ( ++ index < children . length ) {
278274 // Note: the positional info as used here is incorrect.
279275 // Making it correct would be impossible due to aligning cells?
280276 // And it would need copy/pasting `markdown-table` into this project.
281- result [ index ] = handleTableCell (
282- children [ index ] ,
283- node ,
284- context ,
285- safeOptions
286- )
277+ result [ index ] = handleTableCell ( children [ index ] , node , state , info )
287278 }
288279
289280 subexit ( )
@@ -295,10 +286,10 @@ export function gfmTableToMarkdown(options) {
295286 * @type {ToMarkdownHandle }
296287 * @param {InlineCode } node
297288 */
298- function inlineCodeWithTable ( node , parent , context ) {
299- let value = defaultHandlers . inlineCode ( node , parent , context )
289+ function inlineCodeWithTable ( node , parent , state ) {
290+ let value = defaultHandlers . inlineCode ( node , parent , state )
300291
301- if ( context . stack . includes ( 'tableCell' ) ) {
292+ if ( state . stack . includes ( 'tableCell' ) ) {
302293 value = value . replace ( / \| / g, '\\$&' )
303294 }
304295
0 commit comments