1
1
/**
2
- * @typedef {import('mdast').AlignType } AlignType
3
2
* @typedef {import('mdast').Table } Table
4
3
* @typedef {import('mdast').TableRow } TableRow
5
4
* @typedef {import('mdast').TableCell } TableCell
6
5
* @typedef {import('mdast').InlineCode } InlineCode
6
+ *
7
7
* @typedef {import('markdown-table').MarkdownTableOptions } MarkdownTableOptions
8
+ *
8
9
* @typedef {import('mdast-util-from-markdown').CompileContext } CompileContext
9
10
* @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
10
11
* @typedef {import('mdast-util-from-markdown').Handle } FromMarkdownHandle
12
+ *
11
13
* @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
12
14
* @typedef {import('mdast-util-to-markdown').Handle } ToMarkdownHandle
13
15
* @typedef {import('mdast-util-to-markdown').Context } ToMarkdownContext
14
16
* @typedef {import('mdast-util-to-markdown').SafeOptions } SafeOptions
15
- *
17
+ */
18
+
19
+ /**
16
20
* @typedef Options
17
- * @property {boolean } [tableCellPadding=true]
18
- * @property {boolean } [tablePipeAlign=true]
19
- * @property {MarkdownTableOptions['stringLength'] } [stringLength]
21
+ * Configuration.
22
+ * @property {boolean | null | undefined } [tableCellPadding=true]
23
+ * Whether to add a space of padding between delimiters and cells.
24
+ * @property {boolean | null | undefined } [tablePipeAlign=true]
25
+ * Whether to align the delimiters.
26
+ * @property {MarkdownTableOptions['stringLength'] | null | undefined } [stringLength]
27
+ * Function to detect the length of table cell content, used when aligning
28
+ * the delimiters between cells
20
29
*/
21
30
22
31
import { containerPhrasing } from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
23
32
import { inlineCode } from 'mdast-util-to-markdown/lib/handle/inline-code.js'
24
33
import { markdownTable } from 'markdown-table'
25
34
26
- /** @type {FromMarkdownExtension } */
35
+ // To do: next major: use `state` and `state` utilities from `mdast-util-to-markdown`.
36
+ // To do: next major: use `defaultHandlers.inlineCode`.
37
+ // To do: next major: expose functions.
38
+
39
+ /**
40
+ * Extension for `mdast-util-from-markdown` to enable GFM tables.
41
+ *
42
+ * @type {FromMarkdownExtension }
43
+ */
27
44
export const gfmTableFromMarkdown = {
28
45
enter : {
29
46
table : enterTable ,
@@ -45,7 +62,7 @@ export const gfmTableFromMarkdown = {
45
62
* @type {FromMarkdownHandle }
46
63
*/
47
64
function enterTable ( token ) {
48
- /** @type {Array<'left'| 'right'| 'center'| 'none'> } */
65
+ /** @type {Array<'left' | 'right' | 'center' | 'none'> } */
49
66
// @ts -expect-error: `align` is custom.
50
67
const align = token . _align
51
68
this . enter (
@@ -121,8 +138,13 @@ function replace($0, $1) {
121
138
}
122
139
123
140
/**
124
- * @param {Options } [options]
141
+ * Create an extension for `mdast-util-to-markdown` to enable GFM tables in
142
+ * markdown.
143
+ *
144
+ * @param {Options | null | undefined } [options]
145
+ * Configuration.
125
146
* @returns {ToMarkdownExtension }
147
+ * Extension for `mdast-util-to-markdown` to enable GFM tables.
126
148
*/
127
149
export function gfmTableToMarkdown ( options ) {
128
150
const settings = options || { }
@@ -165,7 +187,6 @@ export function gfmTableToMarkdown(options) {
165
187
function handleTable ( node , _ , context , safeOptions ) {
166
188
return serializeData (
167
189
handleTableAsData ( node , context , safeOptions ) ,
168
- // @ts -expect-error: fixed in `markdown-table@3.0.1`.
169
190
node . align
170
191
)
171
192
}
@@ -180,8 +201,8 @@ export function gfmTableToMarkdown(options) {
180
201
*/
181
202
function handleTableRow ( node , _ , context , safeOptions ) {
182
203
const row = handleTableRowAsData ( node , context , safeOptions )
183
- // `markdown-table` will always add an align row
184
204
const value = serializeData ( [ row ] )
205
+ // `markdown-table` will always add an align row
185
206
return value . slice ( 0 , value . indexOf ( '\n' ) )
186
207
}
187
208
@@ -204,13 +225,16 @@ export function gfmTableToMarkdown(options) {
204
225
205
226
/**
206
227
* @param {Array<Array<string>> } matrix
207
- * @param {Array<string> } [align]
228
+ * @param {Array<string | null | undefined> | null | undefined } [align]
208
229
*/
209
230
function serializeData ( matrix , align ) {
210
231
return markdownTable ( matrix , {
211
232
align,
233
+ // @ts -expect-error: `markdown-table` types should support `null`.
212
234
alignDelimiters,
235
+ // @ts -expect-error: `markdown-table` types should support `null`.
213
236
padding,
237
+ // @ts -expect-error: `markdown-table` types should support `null`.
214
238
stringLength
215
239
} )
216
240
}
0 commit comments