Skip to content

Commit 01c24d2

Browse files
committed
Refactor code-style
* Add more docs to JSDoc * Add support for `null` in input of API types
1 parent 2bcc6fc commit 01c24d2

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

lib/index.js

+35-11
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
11
/**
2-
* @typedef {import('mdast').AlignType} AlignType
32
* @typedef {import('mdast').Table} Table
43
* @typedef {import('mdast').TableRow} TableRow
54
* @typedef {import('mdast').TableCell} TableCell
65
* @typedef {import('mdast').InlineCode} InlineCode
6+
*
77
* @typedef {import('markdown-table').MarkdownTableOptions} MarkdownTableOptions
8+
*
89
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
910
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
1011
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
12+
*
1113
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
1214
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
1315
* @typedef {import('mdast-util-to-markdown').Context} ToMarkdownContext
1416
* @typedef {import('mdast-util-to-markdown').SafeOptions} SafeOptions
15-
*
17+
*/
18+
19+
/**
1620
* @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
2029
*/
2130

2231
import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
2332
import {inlineCode} from 'mdast-util-to-markdown/lib/handle/inline-code.js'
2433
import {markdownTable} from 'markdown-table'
2534

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+
*/
2744
export const gfmTableFromMarkdown = {
2845
enter: {
2946
table: enterTable,
@@ -45,7 +62,7 @@ export const gfmTableFromMarkdown = {
4562
* @type {FromMarkdownHandle}
4663
*/
4764
function enterTable(token) {
48-
/** @type {Array<'left'|'right'|'center'|'none'>} */
65+
/** @type {Array<'left' | 'right' | 'center' | 'none'>} */
4966
// @ts-expect-error: `align` is custom.
5067
const align = token._align
5168
this.enter(
@@ -121,8 +138,13 @@ function replace($0, $1) {
121138
}
122139

123140
/**
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.
125146
* @returns {ToMarkdownExtension}
147+
* Extension for `mdast-util-to-markdown` to enable GFM tables.
126148
*/
127149
export function gfmTableToMarkdown(options) {
128150
const settings = options || {}
@@ -165,7 +187,6 @@ export function gfmTableToMarkdown(options) {
165187
function handleTable(node, _, context, safeOptions) {
166188
return serializeData(
167189
handleTableAsData(node, context, safeOptions),
168-
// @ts-expect-error: fixed in `markdown-table@3.0.1`.
169190
node.align
170191
)
171192
}
@@ -180,8 +201,8 @@ export function gfmTableToMarkdown(options) {
180201
*/
181202
function handleTableRow(node, _, context, safeOptions) {
182203
const row = handleTableRowAsData(node, context, safeOptions)
183-
// `markdown-table` will always add an align row
184204
const value = serializeData([row])
205+
// `markdown-table` will always add an align row
185206
return value.slice(0, value.indexOf('\n'))
186207
}
187208

@@ -204,13 +225,16 @@ export function gfmTableToMarkdown(options) {
204225

205226
/**
206227
* @param {Array<Array<string>>} matrix
207-
* @param {Array<string>} [align]
228+
* @param {Array<string | null | undefined> | null | undefined} [align]
208229
*/
209230
function serializeData(matrix, align) {
210231
return markdownTable(matrix, {
211232
align,
233+
// @ts-expect-error: `markdown-table` types should support `null`.
212234
alignDelimiters,
235+
// @ts-expect-error: `markdown-table` types should support `null`.
213236
padding,
237+
// @ts-expect-error: `markdown-table` types should support `null`.
214238
stringLength
215239
})
216240
}

0 commit comments

Comments
 (0)