Skip to content

Commit 52d1b84

Browse files
committed
Refactor types to use ConstructNameMap interface
1 parent 142b0f8 commit 52d1b84

File tree

5 files changed

+48
-12
lines changed

5 files changed

+48
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules/
44
*.d.ts
55
*.log
66
yarn.lock
7+
!/index.d.ts

index.d.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
export type {Options} from './lib/index.js'
2+
3+
export {gfmTableFromMarkdown, gfmTableToMarkdown} from './lib/index.js'
4+
5+
// Add custom data tracked to turn a syntax tree into markdown.
6+
declare module 'mdast-util-to-markdown' {
7+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
8+
interface ConstructNameMap {
9+
/**
10+
* Whole table.
11+
*
12+
* ```markdown
13+
* > | | a |
14+
* ^^^^^
15+
* > | | - |
16+
* ^^^^^
17+
* ```
18+
*/
19+
table: 'table'
20+
21+
/**
22+
* Table cell.
23+
*
24+
* ```markdown
25+
* > | | a |
26+
* ^^^^^
27+
* | | - |
28+
* ```
29+
*/
30+
tableCell: 'tableCell'
31+
32+
/**
33+
* Table row.
34+
*
35+
* ```markdown
36+
* > | | a |
37+
* ^^^^^
38+
* | | - |
39+
* ```
40+
*/
41+
tableRow: 'tableRow'
42+
}
43+
}
44+
45+
// Note: `Table` is exposed from `@types/mdast`.

index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
/**
2-
* @typedef {import('./lib/index.js').Options} Options
3-
*/
4-
1+
// Note: types exposed from `index.d.ts`.
52
export {gfmTableFromMarkdown, gfmTableToMarkdown} from './lib/index.js'

lib/index.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,12 @@ export function gfmTableToMarkdown(options) {
136136

137137
return {
138138
unsafe: [
139-
// @ts-expect-error: to do: map.
140139
{character: '\r', inConstruct: 'tableCell'},
141-
// @ts-expect-error: to do: map.
142140
{character: '\n', inConstruct: 'tableCell'},
143141
// A pipe, when followed by a tab or space (padding), or a dash or colon
144142
// (unpadded delimiter row), could result in a table.
145143
{atBreak: true, character: '|', after: '[\t :-]'},
146144
// A pipe in a cell must be encoded.
147-
// @ts-expect-error: to do: map.
148145
{character: '|', inConstruct: 'tableCell'},
149146
// A colon must be followed by a dash, in which case it could start a
150147
// delimiter row.
@@ -196,7 +193,6 @@ export function gfmTableToMarkdown(options) {
196193
* @param {TableCell} node
197194
*/
198195
function handleTableCell(node, _, context, safeOptions) {
199-
// @ts-expect-error: to do: map.
200196
const exit = context.enter('tableCell')
201197
const subexit = context.enter('phrasing')
202198
const value = containerPhrasing(node, context, {
@@ -232,7 +228,6 @@ export function gfmTableToMarkdown(options) {
232228
let index = -1
233229
/** @type {Array<Array<string>>} */
234230
const result = []
235-
// @ts-expect-error: to do: map.
236231
const subexit = context.enter('table')
237232

238233
while (++index < children.length) {
@@ -258,7 +253,6 @@ export function gfmTableToMarkdown(options) {
258253
let index = -1
259254
/** @type {Array<string>} */
260255
const result = []
261-
// @ts-expect-error: to do: map.
262256
const subexit = context.enter('tableRow')
263257

264258
while (++index < children.length) {
@@ -285,7 +279,6 @@ export function gfmTableToMarkdown(options) {
285279
function inlineCodeWithTable(node, parent, context) {
286280
let value = inlineCode(node, parent, context)
287281

288-
// @ts-expect-error: to do: map.
289282
if (context.stack.includes('tableCell')) {
290283
value = value.replace(/\|/g, '\\$&')
291284
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"include": ["**/*.js"],
2+
"include": ["**/*.js", "index.d.ts"],
33
"exclude": ["coverage/", "node_modules/"],
44
"compilerOptions": {
55
"checkJs": true,

0 commit comments

Comments
 (0)