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 12, 2023
1 parent d5a134d commit 63e6ef9
Show file tree
Hide file tree
Showing 14 changed files with 412 additions and 248 deletions.
10 changes: 3 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
coverage/
node_modules/
packages/remark/*.d.ts
packages/remark-cli/*.d.ts
packages/remark-parse/lib/*.d.ts
packages/remark-parse/test.d.ts
packages/remark-stringify/lib/*.d.ts
packages/remark-stringify/test.d.ts
test.d.ts
.DS_Store
*.d.ts
*.log
yarn.lock
!/packages/remark-parse/index.d.ts
!/packages/remark-stringify/index.d.ts
17 changes: 9 additions & 8 deletions packages/remark-cli/cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node

/**
* @typedef Pack
* @property {string} name
Expand All @@ -25,16 +26,16 @@ const cli = JSON.parse(
)

args({
processor: remark,
name: proc.name,
description: cli.description,
extensions: markdownExtensions,
ignoreName: '.' + proc.name + 'ignore',
name: proc.name,
packageField: proc.name + 'Config',
pluginPrefix: proc.name,
processor: remark,
rcName: '.' + proc.name + 'rc',
version: [
proc.name + ': ' + proc.version,
cli.name + ': ' + cli.version
].join(', '),
pluginPrefix: proc.name,
packageField: proc.name + 'Config',
rcName: '.' + proc.name + 'rc',
ignoreName: '.' + proc.name + 'ignore',
extensions: markdownExtensions
].join(', ')
})
25 changes: 22 additions & 3 deletions packages/remark-parse/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
// This wrapper exists because JS in TS can’t export a `@type` of a function.
import type {Root} from 'mdast'
import type {Plugin} from 'unified'
import type {Options} from './lib/index.js'

declare const remarkParse: Plugin<[(Options | undefined)?], string, Root>
export type {Options} from './lib/index.js'

/**
* Add support for parsing from markdown.
*
* @this
* Unified processor.
* @param
* Configuration (optional).
* @returns
* Nothing.
*/
declare const remarkParse: Plugin<
[(Readonly<Options> | null | undefined)?],
string,
Root
>
export default remarkParse

export type {Options} from './lib/index.js'
// To do: register types.
// // Add custom settings supported when `remark-parse` is added.
// declare module 'unified' {
// interface Settings extends Options {}
// }
1 change: 1 addition & 0 deletions packages/remark-parse/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// Note: types exposed from `index.d.ts`.
export {default} from './lib/index.js'
59 changes: 39 additions & 20 deletions packages/remark-parse/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast-util-from-markdown').Options} Options
* @typedef {import('mdast-util-from-markdown').Options} FromMarkdownOptions
* @typedef {import('unified').Parser<Root>} Parser
* @typedef {import('unified').Processor<Root>} Processor
*/

/**
* @typedef {Omit<FromMarkdownOptions, 'extensions' | 'fromMarkdownExtensions'>} Options
*/

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

/**
* @this {import('unified').Processor}
* @type {import('unified').Plugin<[Options?] | void[], string, Root>}
* Aadd support for parsing from markdown.
*
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
* @returns {undefined}
* Nothing.
*/
export default function remarkParse(options) {
/** @type {import('unified').Parser<Root>} */
const parser = (doc) => {
/** @type {Processor} */
// @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly.
const self = this

self.parser = parser

/**
* @type {Parser}
*/
function parser(doc) {
// To do: remove cast when typed.
// Assume options.
const settings = /** @type {Options} */ (this.data('settings'))
const settings = /** @type {Options} */ (self.data('settings'))

return fromMarkdown(
doc,
Object.assign({}, 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: this.data('micromarkExtensions') || [],
// @ts-expect-error: to do: type.
mdastExtensions: this.data('fromMarkdownExtensions') || []
})
)
}
/** @type {FromMarkdownOptions} */
const resolvedOptions = {
...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') || []
}

Object.assign(this, {Parser: parser})
return fromMarkdown(doc, resolvedOptions)
}
}
15 changes: 14 additions & 1 deletion packages/remark-parse/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@
"strict": true
},
"xo": {
"prettier": true
"overrides": [
{
"files": [
"**/*.ts"
],
"rules": {
"@typescript-eslint/ban-types": "off"
}
}
],
"prettier": true,
"rules": {
"unicorn/no-this-assignment": "off"
}
}
}
25 changes: 22 additions & 3 deletions packages/remark-stringify/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
// This wrapper exists because JS in TS can’t export a `@type` of a function.
import type {Root} from 'mdast'
import type {Plugin} from 'unified'
import type {Options} from './lib/index.js'

declare const remarkStringify: Plugin<[(Options | undefined)?], Root, string>
export type {Options} from './lib/index.js'

/**
* Add support for serializing as HTML.
*
* @this
* Unified processor.
* @param
* Configuration (optional).
* @returns
* Nothing.
*/
declare const remarkStringify: Plugin<
[(Readonly<Options> | null | undefined)?],
Root,
string
>
export default remarkStringify

export type {Options} from './lib/index.js'
// To do: register types.
// // Add custom settings supported when `remark-stringify` is added.
// declare module 'unified' {
// interface Settings extends Options {}
// }
1 change: 1 addition & 0 deletions packages/remark-stringify/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// Note: types exposed from `index.d.ts`.
export {default} from './lib/index.js'
58 changes: 36 additions & 22 deletions packages/remark-stringify/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,50 @@
/**
* @typedef {import('mdast').Root|import('mdast').Content} Node
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownOptions
* @typedef {import('unified').Compiler<Root, string>} Compiler
* @typedef {import('unified').Processor<undefined, undefined, undefined, Root, string>} Processor
*/

/**
* @typedef {Omit<ToMarkdownOptions, 'extensions'>} Options
*/

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

/**
* @this {import('unified').Processor}
* @type {import('unified').Plugin<[Options?]|void[], Node, string>}
* Add support for serializing as markdown.
*
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
* @returns {undefined}
* Nothing.
*/
export default function remarkStringify(options) {
/** @type {import('unified').Compiler<Node, string>} */
const compiler = (tree) => {
/** @type {Processor} */
// @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly.
const self = this

self.compiler = compiler

/**
* @type {Compiler}
*/
function compiler(tree) {
// To do: remove cast when typed.
// Assume options.
const settings = /** @type {Options} */ (this.data('settings'))
const settings = /** @type {Options} */ (self.data('settings'))

return toMarkdown(
tree,
Object.assign({}, 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.
extensions:
// @ts-expect-error: to do: type.
/** @type {ToMarkdownOptions['extensions']} */ (
// @ts-expect-error: to do: type.
this.data('toMarkdownExtensions')
) || []
})
)
}
/** @type {ToMarkdownOptions} */
const resolvedOptions = {
...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') || []
}

Object.assign(this, {Compiler: compiler})
return toMarkdown(tree, resolvedOptions)
}
}
15 changes: 14 additions & 1 deletion packages/remark-stringify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@
"strict": true
},
"xo": {
"prettier": true
"overrides": [
{
"files": [
"**/*.ts"
],
"rules": {
"@typescript-eslint/ban-types": "off"
}
}
],
"prettier": true,
"rules": {
"unicorn/no-this-assignment": "off"
}
}
}
6 changes: 5 additions & 1 deletion packages/remark/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {unified} from 'unified'
import remarkParse from 'remark-parse'
import remarkStringify from 'remark-stringify'
import {unified} from 'unified'

/**
* Create a new unified processor that already uses `remark-parse` and
* `remark-stringify`.
*/
export const remark = unified().use(remarkParse).use(remarkStringify).freeze()
2 changes: 1 addition & 1 deletion packages/remark/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ main()

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

console.log(String(file))
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ console.log(String(file)) // => '## Hi, Saturn!'

/** @type {import('unified').Plugin<[], import('mdast').Root>} */
function myRemarkPluginToIncreaseHeadings() {
return (tree) => {
visit(tree, (node) => {
return function (tree) {
visit(tree, function (node) {
if (node.type === 'heading') {
node.depth++
}
Expand Down

0 comments on commit 63e6ef9

Please sign in to comment.