Skip to content

Commit 63e6ef9

Browse files
committed
Refactor code-style
1 parent d5a134d commit 63e6ef9

File tree

14 files changed

+412
-248
lines changed

14 files changed

+412
-248
lines changed

.gitignore

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
coverage/
22
node_modules/
3-
packages/remark/*.d.ts
4-
packages/remark-cli/*.d.ts
5-
packages/remark-parse/lib/*.d.ts
6-
packages/remark-parse/test.d.ts
7-
packages/remark-stringify/lib/*.d.ts
8-
packages/remark-stringify/test.d.ts
9-
test.d.ts
103
.DS_Store
4+
*.d.ts
115
*.log
126
yarn.lock
7+
!/packages/remark-parse/index.d.ts
8+
!/packages/remark-stringify/index.d.ts

packages/remark-cli/cli.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
23
/**
34
* @typedef Pack
45
* @property {string} name
@@ -25,16 +26,16 @@ const cli = JSON.parse(
2526
)
2627

2728
args({
28-
processor: remark,
29-
name: proc.name,
3029
description: cli.description,
30+
extensions: markdownExtensions,
31+
ignoreName: '.' + proc.name + 'ignore',
32+
name: proc.name,
33+
packageField: proc.name + 'Config',
34+
pluginPrefix: proc.name,
35+
processor: remark,
36+
rcName: '.' + proc.name + 'rc',
3137
version: [
3238
proc.name + ': ' + proc.version,
3339
cli.name + ': ' + cli.version
34-
].join(', '),
35-
pluginPrefix: proc.name,
36-
packageField: proc.name + 'Config',
37-
rcName: '.' + proc.name + 'rc',
38-
ignoreName: '.' + proc.name + 'ignore',
39-
extensions: markdownExtensions
40+
].join(', ')
4041
})

packages/remark-parse/index.d.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1-
// This wrapper exists because JS in TS can’t export a `@type` of a function.
21
import type {Root} from 'mdast'
32
import type {Plugin} from 'unified'
43
import type {Options} from './lib/index.js'
54

6-
declare const remarkParse: Plugin<[(Options | undefined)?], string, Root>
5+
export type {Options} from './lib/index.js'
6+
7+
/**
8+
* Add support for parsing from markdown.
9+
*
10+
* @this
11+
* Unified processor.
12+
* @param
13+
* Configuration (optional).
14+
* @returns
15+
* Nothing.
16+
*/
17+
declare const remarkParse: Plugin<
18+
[(Readonly<Options> | null | undefined)?],
19+
string,
20+
Root
21+
>
722
export default remarkParse
823

9-
export type {Options} from './lib/index.js'
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+
// }

packages/remark-parse/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
// Note: types exposed from `index.d.ts`.
12
export {default} from './lib/index.js'

packages/remark-parse/lib/index.js

+39-20
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,52 @@
11
/**
22
* @typedef {import('mdast').Root} Root
3-
* @typedef {import('mdast-util-from-markdown').Options} Options
3+
* @typedef {import('mdast-util-from-markdown').Options} FromMarkdownOptions
4+
* @typedef {import('unified').Parser<Root>} Parser
5+
* @typedef {import('unified').Processor<Root>} Processor
6+
*/
7+
8+
/**
9+
* @typedef {Omit<FromMarkdownOptions, 'extensions' | 'fromMarkdownExtensions'>} Options
410
*/
511

612
import {fromMarkdown} from 'mdast-util-from-markdown'
713

814
/**
9-
* @this {import('unified').Processor}
10-
* @type {import('unified').Plugin<[Options?] | void[], string, Root>}
15+
* Aadd support for parsing from markdown.
16+
*
17+
* @param {Readonly<Options> | null | undefined} [options]
18+
* Configuration (optional).
19+
* @returns {undefined}
20+
* Nothing.
1121
*/
1222
export default function remarkParse(options) {
13-
/** @type {import('unified').Parser<Root>} */
14-
const parser = (doc) => {
23+
/** @type {Processor} */
24+
// @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly.
25+
const self = this
26+
27+
self.parser = parser
28+
29+
/**
30+
* @type {Parser}
31+
*/
32+
function parser(doc) {
33+
// To do: remove cast when typed.
1534
// Assume options.
16-
const settings = /** @type {Options} */ (this.data('settings'))
35+
const settings = /** @type {Options} */ (self.data('settings'))
1736

18-
return fromMarkdown(
19-
doc,
20-
Object.assign({}, settings, options, {
21-
// Note: these options are not in the readme.
22-
// The goal is for them to be set by plugins on `data` instead of being
23-
// passed by users.
24-
// @ts-expect-error: to do: type.
25-
extensions: this.data('micromarkExtensions') || [],
26-
// @ts-expect-error: to do: type.
27-
mdastExtensions: this.data('fromMarkdownExtensions') || []
28-
})
29-
)
30-
}
37+
/** @type {FromMarkdownOptions} */
38+
const resolvedOptions = {
39+
...settings,
40+
...options,
41+
// Note: these options are not in the readme.
42+
// The goal is for them to be set by plugins on `data` instead of being
43+
// passed by users.
44+
// @ts-expect-error: to do: type.
45+
extensions: self.data('micromarkExtensions') || [],
46+
// @ts-expect-error: to do: type.
47+
mdastExtensions: self.data('fromMarkdownExtensions') || []
48+
}
3149

32-
Object.assign(this, {Parser: parser})
50+
return fromMarkdown(doc, resolvedOptions)
51+
}
3352
}

packages/remark-parse/package.json

+14-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@
5353
"strict": true
5454
},
5555
"xo": {
56-
"prettier": true
56+
"overrides": [
57+
{
58+
"files": [
59+
"**/*.ts"
60+
],
61+
"rules": {
62+
"@typescript-eslint/ban-types": "off"
63+
}
64+
}
65+
],
66+
"prettier": true,
67+
"rules": {
68+
"unicorn/no-this-assignment": "off"
69+
}
5770
}
5871
}

packages/remark-stringify/index.d.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1-
// This wrapper exists because JS in TS can’t export a `@type` of a function.
21
import type {Root} from 'mdast'
32
import type {Plugin} from 'unified'
43
import type {Options} from './lib/index.js'
54

6-
declare const remarkStringify: Plugin<[(Options | undefined)?], Root, string>
5+
export type {Options} from './lib/index.js'
6+
7+
/**
8+
* Add support for serializing as HTML.
9+
*
10+
* @this
11+
* Unified processor.
12+
* @param
13+
* Configuration (optional).
14+
* @returns
15+
* Nothing.
16+
*/
17+
declare const remarkStringify: Plugin<
18+
[(Readonly<Options> | null | undefined)?],
19+
Root,
20+
string
21+
>
722
export default remarkStringify
823

9-
export type {Options} from './lib/index.js'
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+
// }

packages/remark-stringify/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
// Note: types exposed from `index.d.ts`.
12
export {default} from './lib/index.js'
+36-22
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,50 @@
11
/**
2-
* @typedef {import('mdast').Root|import('mdast').Content} Node
2+
* @typedef {import('mdast').Root} Root
33
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownOptions
4+
* @typedef {import('unified').Compiler<Root, string>} Compiler
5+
* @typedef {import('unified').Processor<undefined, undefined, undefined, Root, string>} Processor
6+
*/
7+
8+
/**
49
* @typedef {Omit<ToMarkdownOptions, 'extensions'>} Options
510
*/
611

712
import {toMarkdown} from 'mdast-util-to-markdown'
813

914
/**
10-
* @this {import('unified').Processor}
11-
* @type {import('unified').Plugin<[Options?]|void[], Node, string>}
15+
* Add support for serializing as markdown.
16+
*
17+
* @param {Readonly<Options> | null | undefined} [options]
18+
* Configuration (optional).
19+
* @returns {undefined}
20+
* Nothing.
1221
*/
1322
export default function remarkStringify(options) {
14-
/** @type {import('unified').Compiler<Node, string>} */
15-
const compiler = (tree) => {
23+
/** @type {Processor} */
24+
// @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly.
25+
const self = this
26+
27+
self.compiler = compiler
28+
29+
/**
30+
* @type {Compiler}
31+
*/
32+
function compiler(tree) {
33+
// To do: remove cast when typed.
1634
// Assume options.
17-
const settings = /** @type {Options} */ (this.data('settings'))
35+
const settings = /** @type {Options} */ (self.data('settings'))
1836

19-
return toMarkdown(
20-
tree,
21-
Object.assign({}, settings, options, {
22-
// Note: this option is not in the readme.
23-
// The goal is for it to be set by plugins on `data` instead of being
24-
// passed by users.
25-
extensions:
26-
// @ts-expect-error: to do: type.
27-
/** @type {ToMarkdownOptions['extensions']} */ (
28-
// @ts-expect-error: to do: type.
29-
this.data('toMarkdownExtensions')
30-
) || []
31-
})
32-
)
33-
}
37+
/** @type {ToMarkdownOptions} */
38+
const resolvedOptions = {
39+
...settings,
40+
...options,
41+
// Note: this option is not in the readme.
42+
// The goal is for it to be set by plugins on `data` instead of being
43+
// passed by users.
44+
// @ts-expect-error: to do: type.
45+
extensions: self.data('toMarkdownExtensions') || []
46+
}
3447

35-
Object.assign(this, {Compiler: compiler})
48+
return toMarkdown(tree, resolvedOptions)
49+
}
3650
}

packages/remark-stringify/package.json

+14-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@
5353
"strict": true
5454
},
5555
"xo": {
56-
"prettier": true
56+
"overrides": [
57+
{
58+
"files": [
59+
"**/*.ts"
60+
],
61+
"rules": {
62+
"@typescript-eslint/ban-types": "off"
63+
}
64+
}
65+
],
66+
"prettier": true,
67+
"rules": {
68+
"unicorn/no-this-assignment": "off"
69+
}
5770
}
5871
}

packages/remark/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import {unified} from 'unified'
21
import remarkParse from 'remark-parse'
32
import remarkStringify from 'remark-stringify'
3+
import {unified} from 'unified'
44

5+
/**
6+
* Create a new unified processor that already uses `remark-parse` and
7+
* `remark-stringify`.
8+
*/
59
export const remark = unified().use(remarkParse).use(remarkStringify).freeze()

packages/remark/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ main()
175175

176176
async function main() {
177177
const file = await remark()
178-
.data('settings', {bullet: '*', setext: true, listItemIndent: 'one'})
178+
.data('settings', {bullet: '*', listItemIndent: 'one', setext: true})
179179
.process('# Moons of Neptune\n\n- Naiad\n- Thalassa\n- Despine\n- …')
180180

181181
console.log(String(file))

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ console.log(String(file)) // => '## Hi, Saturn!'
114114

115115
/** @type {import('unified').Plugin<[], import('mdast').Root>} */
116116
function myRemarkPluginToIncreaseHeadings() {
117-
return (tree) => {
118-
visit(tree, (node) => {
117+
return function (tree) {
118+
visit(tree, function (node) {
119119
if (node.type === 'heading') {
120120
node.depth++
121121
}

0 commit comments

Comments
 (0)