Skip to content

Commit

Permalink
Add typed settings
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 12, 2023
1 parent 63e6ef9 commit bd8c922
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ node_modules/
*.d.ts
*.log
yarn.lock
!/packages/remark/index.d.ts
!/packages/remark-parse/index.d.ts
!/packages/remark-stringify/index.d.ts
16 changes: 11 additions & 5 deletions packages/remark-parse/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type {Root} from 'mdast'
import type {Extension} from 'mdast-util-from-markdown'
import type {Extension as MicromarkExtension} from 'micromark-util-types'
import type {Plugin} from 'unified'
import type {Options} from './lib/index.js'

Expand All @@ -21,8 +23,12 @@ declare const remarkParse: Plugin<
>
export default remarkParse

// To do: register types.
// // Add custom settings supported when `remark-parse` is added.
// declare module 'unified' {
// interface Settings extends Options {}
// }
// Add custom settings supported when `remark-parse` is added.
declare module 'unified' {
interface Settings extends Options {}

interface Data {
micromarkExtensions?: MicromarkExtension[]
fromMarkdownExtensions?: Array<Extension[] | Extension>
}
}
15 changes: 3 additions & 12 deletions packages/remark-parse/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,14 @@ export default function remarkParse(options) {
* @type {Parser}
*/
function parser(doc) {
// To do: remove cast when typed.
// Assume options.
const settings = /** @type {Options} */ (self.data('settings'))

/** @type {FromMarkdownOptions} */
const resolvedOptions = {
...settings,
return fromMarkdown(doc, {
...self.data('settings'),
...options,
// Note: these options are not in the readme.
// The goal is for them to be set by plugins on `data` instead of being
// passed by users.
// @ts-expect-error: to do: type.
extensions: self.data('micromarkExtensions') || [],
// @ts-expect-error: to do: type.
mdastExtensions: self.data('fromMarkdownExtensions') || []
}

return fromMarkdown(doc, resolvedOptions)
})
}
}
4 changes: 3 additions & 1 deletion packages/remark-parse/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-from-markdown": "^2.0.0",
"micromark-util-types": "^2.0.0",
"unified": "^11.0.0"
},
"scripts": {},
Expand All @@ -59,7 +60,8 @@
"**/*.ts"
],
"rules": {
"@typescript-eslint/ban-types": "off"
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/consistent-type-definitions": "off"
}
}
],
Expand Down
14 changes: 9 additions & 5 deletions packages/remark-stringify/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {Root} from 'mdast'
import type {Options as Extension} from 'mdast-util-to-markdown'
import type {Plugin} from 'unified'
import type {Options} from './lib/index.js'

Expand All @@ -21,8 +22,11 @@ declare const remarkStringify: Plugin<
>
export default remarkStringify

// To do: register types.
// // Add custom settings supported when `remark-stringify` is added.
// declare module 'unified' {
// interface Settings extends Options {}
// }
// Add custom settings supported when `remark-stringify` is added.
declare module 'unified' {
interface Settings extends Options {}

interface Data {
toMarkdownExtensions?: Extension[]
}
}
14 changes: 3 additions & 11 deletions packages/remark-stringify/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,13 @@ export default function remarkStringify(options) {
* @type {Compiler}
*/
function compiler(tree) {
// To do: remove cast when typed.
// Assume options.
const settings = /** @type {Options} */ (self.data('settings'))

/** @type {ToMarkdownOptions} */
const resolvedOptions = {
...settings,
return toMarkdown(tree, {
...self.data('settings'),
...options,
// Note: this option is not in the readme.
// The goal is for it to be set by plugins on `data` instead of being
// passed by users.
// @ts-expect-error: to do: type.
extensions: self.data('toMarkdownExtensions') || []
}

return toMarkdown(tree, resolvedOptions)
})
}
}
3 changes: 2 additions & 1 deletion packages/remark-stringify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"**/*.ts"
],
"rules": {
"@typescript-eslint/ban-types": "off"
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/consistent-type-definitions": "off"
}
}
],
Expand Down
11 changes: 11 additions & 0 deletions packages/remark/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference types="remark-parse" />
/// <reference types="remark-stringify" />

import type {Root} from 'hast'
import type {Processor} from 'unified'

/**
* Create a new unified processor that already uses `remark-parse` and
* `remark-stringify`.
*/
export const remark: Processor<Root, undefined, undefined, Root, string>
1 change: 1 addition & 0 deletions packages/remark/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Note: types exposed from `index.d.ts`
import remarkParse from 'remark-parse'
import remarkStringify from 'remark-stringify'
import {unified} from 'unified'
Expand Down
10 changes: 10 additions & 0 deletions packages/remark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@
"strict": true
},
"xo": {
"overrides": [
{
"files": [
"**/*.ts"
],
"rules": {
"@typescript-eslint/triple-slash-reference": "off"
}
}
],
"prettier": true
}
}
4 changes: 0 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ test('remark', async function (t) {
await t.test('should accept settings', async function () {
assert.equal(
remark()
// @ts-expect-error: to do: type settings.
.data('settings', {closeAtx: true})
.processSync('# foo')
.toString(),
Expand Down Expand Up @@ -139,9 +138,7 @@ test('remark-parse', async function (t) {

await t.test('should support extensions', function () {
const tree = unified()
// @ts-expect-error: to do: type settings.
.data('micromarkExtensions', [gfm()])
// @ts-expect-error: to do: type settings.
.data('fromMarkdownExtensions', [gfmFromMarkdown()])
.use(remarkParse)
.parse('* [x] contact@example.com ~~strikethrough~~')
Expand Down Expand Up @@ -210,7 +207,6 @@ test('remark-stringify', async function (t) {

await t.test('should support extensions', async function () {
const result = unified()
// @ts-expect-error: to do: type settings.
.data('toMarkdownExtensions', [gfmToMarkdown()])
.use(remarkStringify)
.stringify({
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"exclude": ["**/coverage/", "**/node_modules/"],
"include": [
"**/*.js",
"packages/remark/index.d.ts",
"packages/remark-parse/index.d.ts",
"packages/remark-stringify/index.d.ts"
]
Expand Down

0 comments on commit bd8c922

Please sign in to comment.