Skip to content

Commit

Permalink
Refactor code style in scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Nov 11, 2023
1 parent db4810d commit 0e79b65
Show file tree
Hide file tree
Showing 89 changed files with 3,094 additions and 3,350 deletions.
91 changes: 51 additions & 40 deletions doc/create-a-custom-rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@ touch .remarkrc.js

```js
// .remarkrc.js
module.exports = {
plugins: []
}
/**
* @typedef {import('unified').Preset} Preset
*/

/** @type {Preset} */
const preset = {plugins: []}

export default preset
```

Then, in our `package.json`, add the following script to process all the
Expand Down Expand Up @@ -142,7 +147,7 @@ import {lintRule} from 'unified-lint-rule'

const remarkLintNoGifAllowed = lintRule(
'remark-lint:no-gif-allowed',
(tree, file, options) => {
function (tree, file, options) {
// Rule implementation
}
)
Expand All @@ -155,9 +160,16 @@ namespace.
If your project was named `my-project`, then you can export your rule as:

```js
const remarkLintNoGifAllowed = lintRule('my-project-name:no-gif-allowed', () => {})
const remarkLintNoGifAllowed = lintRule(
'my-project-name:no-gif-allowed',
function () {}
)

// Or:
const remarkLintNoGifAllowed = lintRule('my-npm-published-package:no-gif-allowed', () => {})
const remarkLintNoGifAllowed = lintRule(
'my-npm-published-package:no-gif-allowed',
function () {}
)
```

This can help you when wanting to create a group of rules under the same
Expand All @@ -168,7 +180,7 @@ This can help you when wanting to create a group of rules under the same
Your rule function will receive three arguments:

```js
(tree, file, options) => {}
function rule(tree, file, options) {}
```

* `tree` (*required*): [mdast][]
Expand All @@ -187,38 +199,36 @@ recursively inspect all the image nodes, and
nodes that we have generated ourselves and do not belong to the `doc.md`.

```js
/**
* @typedef {import('mdast').Root} Root
*/

import {lintRule} from 'unified-lint-rule'
import {visit} from 'unist-visit-util'
import {generated} from 'unist-util-generated'

function isValidNode(node) {
// Here we check whether the given node violates our rule.
// Implementation details are not relevant to the scope of this example.
// This is an overly simplified solution for demonstration purposes
if (node.url && typeof node.url === 'string') {
return !node.url.endsWith('.gif')
}
}
import {visit} from 'unist-util-visit'

const remarkLintNoGifAllowed = lintRule(
'remark-lint:no-gif-allowed',
(tree, file, options) => {
visit(tree, 'image', (node) => {
if (!generated(node)) {
// This is an extremely simplified example of how to structure
// the logic to check whether a node violates your rule.
// You have complete freedom over how to visit/inspect the tree,
// and on how to implement the validation logic for your node.
const isValid = isValidNode(node)

if (!isValid) {
// Remember to provide the node as second argument to the message,
// in order to obtain the position and column where the violation occurred.
file.message(
'Invalid image file extensions. Please do not use gifs',
node
)
}
/**
* @param {Root} tree
* Tree.
* @returns {undefined}
* Nothing.
*/
function (tree, file, options) {
visit(tree, 'image', function (node) {
// This is an extremely simplified example of how to structure
// the logic to check whether a node violates your rule.
// You have complete freedom over how to visit/inspect the tree,
// and on how to implement the validation logic for your node.
const isValid = !node.url.endsWith('.gif')

if (!isValid) {
// Remember to provide the node as second argument to the message,
// in order to obtain the position and column where the violation occurred.
file.message(
'Invalid image file extensions. Please do not use gifs',
node
)
}
})
}
Expand All @@ -236,13 +246,14 @@ You can do that by importing your rule and adding it in `plugins` array:

```js
// .remarkrc.js
import remarkLintNoGifAllowed from './rules/no-gif-allowed.js'
/**
* @typedef {import('unified').Preset} Preset
*/

const plugins = {
plugins: [remarkLintNoGifAllowed]
}
import remarkLintNoGifAllowed from './rules/no-gif-allowed.js'

const preset = {plugins}
/** @type {Preset} */
const preset = {plugins: [remarkLintNoGifAllowed]}

export default preset
```
Expand Down
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@
"c8": "^8.0.0",
"comment-parser": "^1.0.0",
"github-slugger": "^2.0.0",
"mdast-util-from-markdown": "^2.0.0",
"mdast-util-gfm": "^3.0.0",
"mdast-util-to-markdown": "^2.0.0",
"mdast-zone": "^6.0.0",
"micromark-extension-gfm": "^3.0.0",
"parse-author": "^2.0.0",
"prettier": "^3.0.0",
"remark": "^15.0.0",
Expand All @@ -127,16 +131,16 @@
"remark-toc": "^9.0.0",
"remark-validate-links": "^13.0.0",
"strip-indent": "^4.0.0",
"to-vfile": "^8.0.0",
"type-coverage": "^2.0.0",
"type-fest": "^4.0.0",
"typescript": "^5.0.0",
"unist-builder": "^4.0.0",
"unist-util-remove-position": "^5.0.0",
"vfile": "^6.0.0",
"xo": "^0.56.0"
},
"scripts": {
"generate": "node --conditions development script/build-presets.js && node --conditions development script/build-rules.js",
"generate": "node --conditions development script/build-plugins.js && node --conditions development script/build-presets.js",
"build": "tsc --build --clean && tsc --build && type-coverage",
"format": "remark . --frail --output --quiet && prettier . --log-level warn --write && xo --fix",
"test": "npm run build && npm run generate && npm run format && npm run test-coverage",
Expand Down Expand Up @@ -174,8 +178,8 @@
"remark-lint-list-item-indent",
"space"
],
"./script/plugin/list-of-presets.js",
"./script/plugin/list-of-rules.js"
"./script/plugin/list-of-plugins.js",
"./script/plugin/list-of-presets.js"
]
},
"typeCoverage": {
Expand All @@ -193,7 +197,8 @@
],
"rules": {
"max-depth": "off",
"no-await-in-loop": "off"
"no-await-in-loop": "off",
"unicorn/no-array-callback-reference": "off"
}
}
],
Expand Down
18 changes: 8 additions & 10 deletions packages/remark-lint-blockquote-indentation/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,20 @@ In browsers with [`esm.sh`][esmsh]:
On the API:

```js
import {read} from 'to-vfile'
import {reporter} from 'vfile-reporter'
import {remark} from 'remark'
import remarkLint from 'remark-lint'
import remarkLintBlockquoteIndentation from 'remark-lint-blockquote-indentation'
import {read} from 'to-vfile'
import {reporter} from 'vfile-reporter'

main()
const file = await read('example.md')

async function main() {
const file = await remark()
.use(remarkLint)
.use(remarkLintBlockquoteIndentation)
.process(await read('example.md'))
await remark()
.use(remarkLint)
.use(remarkLintBlockquoteIndentation)
.process(file)

console.error(reporter(file))
}
console.error(reporter(file))
```

On the CLI:
Expand Down
18 changes: 8 additions & 10 deletions packages/remark-lint-checkbox-character-style/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,20 @@ In browsers with [`esm.sh`][esmsh]:
On the API:

```js
import {read} from 'to-vfile'
import {reporter} from 'vfile-reporter'
import {remark} from 'remark'
import remarkLint from 'remark-lint'
import remarkLintCheckboxCharacterStyle from 'remark-lint-checkbox-character-style'
import {read} from 'to-vfile'
import {reporter} from 'vfile-reporter'

main()
const file = await read('example.md')

async function main() {
const file = await remark()
.use(remarkLint)
.use(remarkLintCheckboxCharacterStyle)
.process(await read('example.md'))
await remark()
.use(remarkLint)
.use(remarkLintCheckboxCharacterStyle)
.process(file)

console.error(reporter(file))
}
console.error(reporter(file))
```

On the CLI:
Expand Down
18 changes: 8 additions & 10 deletions packages/remark-lint-checkbox-content-indent/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,20 @@ In browsers with [`esm.sh`][esmsh]:
On the API:

```js
import {read} from 'to-vfile'
import {reporter} from 'vfile-reporter'
import {remark} from 'remark'
import remarkLint from 'remark-lint'
import remarkLintCheckboxContentIndent from 'remark-lint-checkbox-content-indent'
import {read} from 'to-vfile'
import {reporter} from 'vfile-reporter'

main()
const file = await read('example.md')

async function main() {
const file = await remark()
.use(remarkLint)
.use(remarkLintCheckboxContentIndent)
.process(await read('example.md'))
await remark()
.use(remarkLint)
.use(remarkLintCheckboxContentIndent)
.process(file)

console.error(reporter(file))
}
console.error(reporter(file))
```

On the CLI:
Expand Down
18 changes: 8 additions & 10 deletions packages/remark-lint-code-block-style/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,20 @@ In browsers with [`esm.sh`][esmsh]:
On the API:

```js
import {read} from 'to-vfile'
import {reporter} from 'vfile-reporter'
import {remark} from 'remark'
import remarkLint from 'remark-lint'
import remarkLintCodeBlockStyle from 'remark-lint-code-block-style'
import {read} from 'to-vfile'
import {reporter} from 'vfile-reporter'

main()
const file = await read('example.md')

async function main() {
const file = await remark()
.use(remarkLint)
.use(remarkLintCodeBlockStyle)
.process(await read('example.md'))
await remark()
.use(remarkLint)
.use(remarkLintCodeBlockStyle)
.process(file)

console.error(reporter(file))
}
console.error(reporter(file))
```

On the CLI:
Expand Down
18 changes: 8 additions & 10 deletions packages/remark-lint-definition-case/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,20 @@ In browsers with [`esm.sh`][esmsh]:
On the API:

```js
import {read} from 'to-vfile'
import {reporter} from 'vfile-reporter'
import {remark} from 'remark'
import remarkLint from 'remark-lint'
import remarkLintDefinitionCase from 'remark-lint-definition-case'
import {read} from 'to-vfile'
import {reporter} from 'vfile-reporter'

main()
const file = await read('example.md')

async function main() {
const file = await remark()
.use(remarkLint)
.use(remarkLintDefinitionCase)
.process(await read('example.md'))
await remark()
.use(remarkLint)
.use(remarkLintDefinitionCase)
.process(file)

console.error(reporter(file))
}
console.error(reporter(file))
```

On the CLI:
Expand Down
18 changes: 8 additions & 10 deletions packages/remark-lint-definition-spacing/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,20 @@ In browsers with [`esm.sh`][esmsh]:
On the API:

```js
import {read} from 'to-vfile'
import {reporter} from 'vfile-reporter'
import {remark} from 'remark'
import remarkLint from 'remark-lint'
import remarkLintDefinitionSpacing from 'remark-lint-definition-spacing'
import {read} from 'to-vfile'
import {reporter} from 'vfile-reporter'

main()
const file = await read('example.md')

async function main() {
const file = await remark()
.use(remarkLint)
.use(remarkLintDefinitionSpacing)
.process(await read('example.md'))
await remark()
.use(remarkLint)
.use(remarkLintDefinitionSpacing)
.process(file)

console.error(reporter(file))
}
console.error(reporter(file))
```

On the CLI:
Expand Down
Loading

0 comments on commit 0e79b65

Please sign in to comment.