1
1
/**
2
+ * @typedef {import('mdast').InlineCode } InlineCode
2
3
* @typedef {import('mdast').Table } Table
3
- * @typedef {import('mdast').TableRow } TableRow
4
4
* @typedef {import('mdast').TableCell } TableCell
5
- * @typedef {import('mdast').InlineCode } InlineCode
5
+ * @typedef {import('mdast').TableRow } TableRow
6
6
*
7
- * @typedef {import('markdown-table').MarkdownTableOptions } MarkdownTableOptions
7
+ * @typedef {import('markdown-table').Options } MarkdownTableOptions
8
8
*
9
9
* @typedef {import('mdast-util-from-markdown').CompileContext } CompileContext
10
10
* @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
20
20
* @typedef Options
21
21
* Configuration.
22
22
* @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`).
24
25
* @property {boolean | null | undefined } [tablePipeAlign=true]
25
- * Whether to align the delimiters.
26
+ * Whether to align the delimiters (default: `true`) .
26
27
* @property {MarkdownTableOptions['stringLength'] | null | undefined } [stringLength]
27
28
* Function to detect the length of table cell content, used when aligning
28
- * the delimiters between cells
29
+ * the delimiters between cells (optional).
29
30
*/
30
31
32
+ import { ok as assert } from 'devlop'
31
33
import { markdownTable } from 'markdown-table'
32
34
import { defaultHandlers } from 'mdast-util-to-markdown'
33
35
34
- // To do: next major: use `state` and `state` utilities from `mdast-util-to-markdown`.
35
36
// To do: next major: expose functions.
36
37
37
38
/**
@@ -60,13 +61,14 @@ export const gfmTableFromMarkdown = {
60
61
* @type {FromMarkdownHandle }
61
62
*/
62
63
function enterTable ( token ) {
63
- /** @type {Array<'left' | 'right' | 'center' | 'none'> } */
64
- // @ts -expect-error: `align` is custom.
65
64
const align = token . _align
65
+ assert ( align , 'expected `_align` on table' )
66
66
this . enter (
67
67
{
68
68
type : 'table' ,
69
- align : align . map ( ( d ) => ( d === 'none' ? null : d ) ) ,
69
+ align : align . map ( function ( d ) {
70
+ return d === 'none' ? null : d
71
+ } ) ,
70
72
children : [ ]
71
73
} ,
72
74
token
@@ -120,7 +122,8 @@ function exitCodeText(token) {
120
122
value = value . replace ( / \\ ( [ \\ | ] ) / g, replace )
121
123
}
122
124
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' )
124
127
node . value = value
125
128
this . exit ( token )
126
129
}
@@ -171,22 +174,19 @@ export function gfmTableToMarkdown(options) {
171
174
{ atBreak : true , character : '-' , after : '[:|-]' }
172
175
] ,
173
176
handlers : {
177
+ inlineCode : inlineCodeWithTable ,
174
178
table : handleTable ,
175
- tableRow : handleTableRow ,
176
179
tableCell : handleTableCell ,
177
- inlineCode : inlineCodeWithTable
180
+ tableRow : handleTableRow
178
181
}
179
182
}
180
183
181
184
/**
182
185
* @type {ToMarkdownHandle }
183
186
* @param {Table } node
184
187
*/
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 )
190
190
}
191
191
192
192
/**
@@ -197,8 +197,8 @@ export function gfmTableToMarkdown(options) {
197
197
* @type {ToMarkdownHandle }
198
198
* @param {TableRow } node
199
199
*/
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 )
202
202
const value = serializeData ( [ row ] )
203
203
// `markdown-table` will always add an align row
204
204
return value . slice ( 0 , value . indexOf ( '\n' ) )
@@ -208,11 +208,11 @@ export function gfmTableToMarkdown(options) {
208
208
* @type {ToMarkdownHandle }
209
209
* @param {TableCell } node
210
210
*/
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 ,
216
216
before : around ,
217
217
after : around
218
218
} )
@@ -239,22 +239,18 @@ export function gfmTableToMarkdown(options) {
239
239
240
240
/**
241
241
* @param {Table } node
242
- * @param {State } context
243
- * @param {Info } safeOptions
242
+ * @param {State } state
243
+ * @param {Info } info
244
244
*/
245
- function handleTableAsData ( node , context , safeOptions ) {
245
+ function handleTableAsData ( node , state , info ) {
246
246
const children = node . children
247
247
let index = - 1
248
248
/** @type {Array<Array<string>> } */
249
249
const result = [ ]
250
- const subexit = context . enter ( 'table' )
250
+ const subexit = state . enter ( 'table' )
251
251
252
252
while ( ++ index < children . length ) {
253
- result [ index ] = handleTableRowAsData (
254
- children [ index ] ,
255
- context ,
256
- safeOptions
257
- )
253
+ result [ index ] = handleTableRowAsData ( children [ index ] , state , info )
258
254
}
259
255
260
256
subexit ( )
@@ -264,26 +260,21 @@ export function gfmTableToMarkdown(options) {
264
260
265
261
/**
266
262
* @param {TableRow } node
267
- * @param {State } context
268
- * @param {Info } safeOptions
263
+ * @param {State } state
264
+ * @param {Info } info
269
265
*/
270
- function handleTableRowAsData ( node , context , safeOptions ) {
266
+ function handleTableRowAsData ( node , state , info ) {
271
267
const children = node . children
272
268
let index = - 1
273
269
/** @type {Array<string> } */
274
270
const result = [ ]
275
- const subexit = context . enter ( 'tableRow' )
271
+ const subexit = state . enter ( 'tableRow' )
276
272
277
273
while ( ++ index < children . length ) {
278
274
// Note: the positional info as used here is incorrect.
279
275
// Making it correct would be impossible due to aligning cells?
280
276
// 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 )
287
278
}
288
279
289
280
subexit ( )
@@ -295,10 +286,10 @@ export function gfmTableToMarkdown(options) {
295
286
* @type {ToMarkdownHandle }
296
287
* @param {InlineCode } node
297
288
*/
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 )
300
291
301
- if ( context . stack . includes ( 'tableCell' ) ) {
292
+ if ( state . stack . includes ( 'tableCell' ) ) {
302
293
value = value . replace ( / \| / g, '\\$&' )
303
294
}
304
295
0 commit comments