Skip to content

Commit

Permalink
Refactor types to use ConstructNameMap interface
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 30, 2023
1 parent 142b0f8 commit 52d1b84
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
*.d.ts
*.log
yarn.lock
!/index.d.ts
45 changes: 45 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export type {Options} from './lib/index.js'

export {gfmTableFromMarkdown, gfmTableToMarkdown} from './lib/index.js'

// Add custom data tracked to turn a syntax tree into markdown.
declare module 'mdast-util-to-markdown' {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface ConstructNameMap {
/**
* Whole table.
*
* ```markdown
* > | | a |
* ^^^^^
* > | | - |
* ^^^^^
* ```
*/
table: 'table'

/**
* Table cell.
*
* ```markdown
* > | | a |
* ^^^^^
* | | - |
* ```
*/
tableCell: 'tableCell'

/**
* Table row.
*
* ```markdown
* > | | a |
* ^^^^^
* | | - |
* ```
*/
tableRow: 'tableRow'
}
}

// Note: `Table` is exposed from `@types/mdast`.
5 changes: 1 addition & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
/**
* @typedef {import('./lib/index.js').Options} Options
*/

// Note: types exposed from `index.d.ts`.
export {gfmTableFromMarkdown, gfmTableToMarkdown} from './lib/index.js'
7 changes: 0 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,12 @@ export function gfmTableToMarkdown(options) {

return {
unsafe: [
// @ts-expect-error: to do: map.
{character: '\r', inConstruct: 'tableCell'},
// @ts-expect-error: to do: map.
{character: '\n', inConstruct: 'tableCell'},
// A pipe, when followed by a tab or space (padding), or a dash or colon
// (unpadded delimiter row), could result in a table.
{atBreak: true, character: '|', after: '[\t :-]'},
// A pipe in a cell must be encoded.
// @ts-expect-error: to do: map.
{character: '|', inConstruct: 'tableCell'},
// A colon must be followed by a dash, in which case it could start a
// delimiter row.
Expand Down Expand Up @@ -196,7 +193,6 @@ export function gfmTableToMarkdown(options) {
* @param {TableCell} node
*/
function handleTableCell(node, _, context, safeOptions) {
// @ts-expect-error: to do: map.
const exit = context.enter('tableCell')
const subexit = context.enter('phrasing')
const value = containerPhrasing(node, context, {
Expand Down Expand Up @@ -232,7 +228,6 @@ export function gfmTableToMarkdown(options) {
let index = -1
/** @type {Array<Array<string>>} */
const result = []
// @ts-expect-error: to do: map.
const subexit = context.enter('table')

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

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

// @ts-expect-error: to do: map.
if (context.stack.includes('tableCell')) {
value = value.replace(/\|/g, '\\$&')
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"include": ["**/*.js"],
"include": ["**/*.js", "index.d.ts"],
"exclude": ["coverage/", "node_modules/"],
"compilerOptions": {
"checkJs": true,
Expand Down

0 comments on commit 52d1b84

Please sign in to comment.