Skip to content

Commit bd8c922

Browse files
committed
Add typed settings
1 parent 63e6ef9 commit bd8c922

File tree

12 files changed

+55
-39
lines changed

12 files changed

+55
-39
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ node_modules/
44
*.d.ts
55
*.log
66
yarn.lock
7+
!/packages/remark/index.d.ts
78
!/packages/remark-parse/index.d.ts
89
!/packages/remark-stringify/index.d.ts

packages/remark-parse/index.d.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type {Root} from 'mdast'
2+
import type {Extension} from 'mdast-util-from-markdown'
3+
import type {Extension as MicromarkExtension} from 'micromark-util-types'
24
import type {Plugin} from 'unified'
35
import type {Options} from './lib/index.js'
46

@@ -21,8 +23,12 @@ declare const remarkParse: Plugin<
2123
>
2224
export default remarkParse
2325

24-
// To do: register types.
25-
// // Add custom settings supported when `remark-parse` is added.
26-
// declare module 'unified' {
27-
// interface Settings extends Options {}
28-
// }
26+
// Add custom settings supported when `remark-parse` is added.
27+
declare module 'unified' {
28+
interface Settings extends Options {}
29+
30+
interface Data {
31+
micromarkExtensions?: MicromarkExtension[]
32+
fromMarkdownExtensions?: Array<Extension[] | Extension>
33+
}
34+
}

packages/remark-parse/lib/index.js

+3-12
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,14 @@ export default function remarkParse(options) {
3030
* @type {Parser}
3131
*/
3232
function parser(doc) {
33-
// To do: remove cast when typed.
34-
// Assume options.
35-
const settings = /** @type {Options} */ (self.data('settings'))
36-
37-
/** @type {FromMarkdownOptions} */
38-
const resolvedOptions = {
39-
...settings,
33+
return fromMarkdown(doc, {
34+
...self.data('settings'),
4035
...options,
4136
// Note: these options are not in the readme.
4237
// The goal is for them to be set by plugins on `data` instead of being
4338
// passed by users.
44-
// @ts-expect-error: to do: type.
4539
extensions: self.data('micromarkExtensions') || [],
46-
// @ts-expect-error: to do: type.
4740
mdastExtensions: self.data('fromMarkdownExtensions') || []
48-
}
49-
50-
return fromMarkdown(doc, resolvedOptions)
41+
})
5142
}
5243
}

packages/remark-parse/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"dependencies": {
4444
"@types/mdast": "^4.0.0",
4545
"mdast-util-from-markdown": "^2.0.0",
46+
"micromark-util-types": "^2.0.0",
4647
"unified": "^11.0.0"
4748
},
4849
"scripts": {},
@@ -59,7 +60,8 @@
5960
"**/*.ts"
6061
],
6162
"rules": {
62-
"@typescript-eslint/ban-types": "off"
63+
"@typescript-eslint/ban-types": "off",
64+
"@typescript-eslint/consistent-type-definitions": "off"
6365
}
6466
}
6567
],

packages/remark-stringify/index.d.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {Root} from 'mdast'
2+
import type {Options as Extension} from 'mdast-util-to-markdown'
23
import type {Plugin} from 'unified'
34
import type {Options} from './lib/index.js'
45

@@ -21,8 +22,11 @@ declare const remarkStringify: Plugin<
2122
>
2223
export default remarkStringify
2324

24-
// To do: register types.
25-
// // Add custom settings supported when `remark-stringify` is added.
26-
// declare module 'unified' {
27-
// interface Settings extends Options {}
28-
// }
25+
// Add custom settings supported when `remark-stringify` is added.
26+
declare module 'unified' {
27+
interface Settings extends Options {}
28+
29+
interface Data {
30+
toMarkdownExtensions?: Extension[]
31+
}
32+
}

packages/remark-stringify/lib/index.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,13 @@ export default function remarkStringify(options) {
3030
* @type {Compiler}
3131
*/
3232
function compiler(tree) {
33-
// To do: remove cast when typed.
34-
// Assume options.
35-
const settings = /** @type {Options} */ (self.data('settings'))
36-
37-
/** @type {ToMarkdownOptions} */
38-
const resolvedOptions = {
39-
...settings,
33+
return toMarkdown(tree, {
34+
...self.data('settings'),
4035
...options,
4136
// Note: this option is not in the readme.
4237
// The goal is for it to be set by plugins on `data` instead of being
4338
// passed by users.
44-
// @ts-expect-error: to do: type.
4539
extensions: self.data('toMarkdownExtensions') || []
46-
}
47-
48-
return toMarkdown(tree, resolvedOptions)
40+
})
4941
}
5042
}

packages/remark-stringify/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
"**/*.ts"
6060
],
6161
"rules": {
62-
"@typescript-eslint/ban-types": "off"
62+
"@typescript-eslint/ban-types": "off",
63+
"@typescript-eslint/consistent-type-definitions": "off"
6364
}
6465
}
6566
],

packages/remark/index.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference types="remark-parse" />
2+
/// <reference types="remark-stringify" />
3+
4+
import type {Root} from 'hast'
5+
import type {Processor} from 'unified'
6+
7+
/**
8+
* Create a new unified processor that already uses `remark-parse` and
9+
* `remark-stringify`.
10+
*/
11+
export const remark: Processor<Root, undefined, undefined, Root, string>

packages/remark/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Note: types exposed from `index.d.ts`
12
import remarkParse from 'remark-parse'
23
import remarkStringify from 'remark-stringify'
34
import {unified} from 'unified'

packages/remark/package.json

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@
5151
"strict": true
5252
},
5353
"xo": {
54+
"overrides": [
55+
{
56+
"files": [
57+
"**/*.ts"
58+
],
59+
"rules": {
60+
"@typescript-eslint/triple-slash-reference": "off"
61+
}
62+
}
63+
],
5464
"prettier": true
5565
}
5666
}

test.js

-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ test('remark', async function (t) {
2525
await t.test('should accept settings', async function () {
2626
assert.equal(
2727
remark()
28-
// @ts-expect-error: to do: type settings.
2928
.data('settings', {closeAtx: true})
3029
.processSync('# foo')
3130
.toString(),
@@ -139,9 +138,7 @@ test('remark-parse', async function (t) {
139138

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

211208
await t.test('should support extensions', async function () {
212209
const result = unified()
213-
// @ts-expect-error: to do: type settings.
214210
.data('toMarkdownExtensions', [gfmToMarkdown()])
215211
.use(remarkStringify)
216212
.stringify({

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"exclude": ["**/coverage/", "**/node_modules/"],
1414
"include": [
1515
"**/*.js",
16+
"packages/remark/index.d.ts",
1617
"packages/remark-parse/index.d.ts",
1718
"packages/remark-stringify/index.d.ts"
1819
]

0 commit comments

Comments
 (0)