Skip to content

Commit fd0a325

Browse files
committed
Refactor code-style
1 parent 2684024 commit fd0a325

File tree

4 files changed

+596
-518
lines changed

4 files changed

+596
-518
lines changed

index.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export {gfmTableFromMarkdown, gfmTableToMarkdown} from './lib/index.js'
44

55
// Add custom data tracked to turn markdown into a tree.
66
declare module 'mdast-util-from-markdown' {
7-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
87
interface CompileData {
98
/**
109
* Whether we’re currently in a table.
@@ -15,7 +14,6 @@ declare module 'mdast-util-from-markdown' {
1514

1615
// Add custom data tracked to turn a syntax tree into markdown.
1716
declare module 'mdast-util-to-markdown' {
18-
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
1917
interface ConstructNameMap {
2018
/**
2119
* Whole table.

lib/index.js

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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
@@ -20,18 +20,19 @@
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'
3133
import {markdownTable} from 'markdown-table'
3234
import {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
*/
6263
function 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

package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
],
4040
"dependencies": {
4141
"@types/mdast": "^4.0.0",
42+
"devlop": "^1.0.0",
4243
"markdown-table": "^3.0.0",
4344
"mdast-util-from-markdown": "^2.0.0",
4445
"mdast-util-to-markdown": "^2.0.0"
@@ -84,6 +85,16 @@
8485
"strict": true
8586
},
8687
"xo": {
88+
"overrides": [
89+
{
90+
"files": [
91+
"**/*.ts"
92+
],
93+
"rules": {
94+
"@typescript-eslint/consistent-type-definitions": "off"
95+
}
96+
}
97+
],
8798
"prettier": true
8899
}
89100
}

0 commit comments

Comments
 (0)