Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 18, 2023
1 parent 99b2857 commit bceb8b8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
51 changes: 28 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
/// <reference types="remark-parse" />
/// <reference types="remark-stringify" />

/**
* @typedef {import('mdast').Root} Root
* @typedef {import('micromark-extension-gfm').Options & import('mdast-util-gfm').Options} Options
* @typedef {import('mdast-util-gfm').Options} MdastOptions
* @typedef {import('micromark-extension-gfm').Options} MicromarkOptions
* @typedef {import('unified').Processor<Root>} Processor
*/

import {gfm} from 'micromark-extension-gfm'
/**
* @typedef {MicromarkOptions & MdastOptions} Options
* Configuration.
*/

import {gfmFromMarkdown, gfmToMarkdown} from 'mdast-util-gfm'
import {gfm} from 'micromark-extension-gfm'

/** @type {Options} */
const emptyOptions = {}

/**
* Plugin to support GFM (autolink literals, footnotes, strikethrough, tables, tasklists).
* Add support GFM (autolink literals, footnotes, strikethrough, tables, tasklists).
*
* @param {Options | null | undefined} [options='yaml']
* Configuration (default: `'yaml'`).
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {undefined}
* Nothing.
*/
export default function remarkGfm(options = {}) {
export default function remarkGfm(options) {
// @ts-expect-error: TS is wrong about `this`.
// eslint-disable-next-line unicorn/no-this-assignment
const self = /** @type {Processor} */ (this)
const settings = options || emptyOptions
const data = self.data()

add('micromarkExtensions', gfm(options))
add('fromMarkdownExtensions', gfmFromMarkdown())
add('toMarkdownExtensions', gfmToMarkdown(options))

/**
* @param {string} field
* @param {unknown} value
*/
function add(field, value) {
const list = /** @type {unknown[]} */ (
// Other extensions
/* c8 ignore next 2 */
// @ts-expect-error: to do: remove when remark is released.
data[field] || (data[field] = [])
)
const micromarkExtensions =
data.micromarkExtensions || (data.micromarkExtensions = [])
const fromMarkdownExtensions =
data.fromMarkdownExtensions || (data.fromMarkdownExtensions = [])
const toMarkdownExtensions =
data.toMarkdownExtensions || (data.toMarkdownExtensions = [])

list.push(value)
}
micromarkExtensions.push(gfm(settings))
fromMarkdownExtensions.push(gfmFromMarkdown())
toMarkdownExtensions.push(gfmToMarkdown(settings))
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@
"@types/mdast": "^4.0.0",
"mdast-util-gfm": "^3.0.0",
"micromark-extension-gfm": "^3.0.0",
"remark-parse": "^11.0.0",
"remark-stringify": "^11.0.0",
"unified": "^11.0.0"
},
"devDependencies": {
"@types/node": "^20.0.0",
"c8": "^8.0.0",
"is-hidden": "^2.0.0",
"prettier": "^3.0.0",
"remark": "^14.0.0",
"remark": "^15.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"string-width": "^6.0.0",
Expand Down
2 changes: 0 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ test('fixtures', async function (t) {
}

const proc = remark().use(remarkGfm, config)
/** @type {Root} */
// @ts-expect-error: remove when remark is released.
const actual = proc.parse(input)

try {
Expand Down

0 comments on commit bceb8b8

Please sign in to comment.