Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 11, 2021
1 parent f5cb7d2 commit e7a7701
Show file tree
Hide file tree
Showing 77 changed files with 765 additions and 510 deletions.
79 changes: 46 additions & 33 deletions doc/create-a-custom-rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,25 @@ We then export the result of calling `rule` by providing the *namespace and rule

```js
// rules/no-gif-allowed.js
import {lintRule} from 'unified-lint-rule'

var rule = require('unified-lint-rule')
function noGifAllowed(tree, file, options) {
// Rule implementation
}
module.exports = rule('remark-lint:no-gif-allowed', noGifAllowed)
const remarkLintNoGifAllowed = lintRule(
'remark-lint:no-gif-allowed',
(tree, file, options) => {
// Rule implementation
}
)

export default remarkLintNoGifAllowed
```

Let’s say you want all your custom rules to be defined as part of your project namespace.
If your project was named `my-project`, then you can export your rule as:

```js
module.exports = rule('my-project-name:no-gif-allowed', noGifAllowed)
const remarkLintNoGifAllowed = lintRule('my-project-name:no-gif-allowed', () => {})
// Or:
module.exports = rule('my-npm-published-package:no-gif-allowed', noGifAllowed)
const remarkLintNoGifAllowed = lintRule('my-npm-published-package:no-gif-allowed', () => {})
```

This can help you when wanting to create a group of rules under the same *namespace*.
Expand All @@ -157,7 +161,7 @@ This can help you when wanting to create a group of rules under the same *namesp
Your rule function will receive three arguments:

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

* `tree` (*required*): [mdast](https://github.com/syntax-tree/mdast)
Expand All @@ -173,9 +177,9 @@ Because we will be inspecting [mdast](https://github.com/syntax-tree/mdast), whi
For this example, we will use [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit) to recursively inspect all the image nodes, and [`unist-util-generated`](https://github.com/syntax-tree/unist-util-generated) to ensure we are not inspecting nodes that we have generated ourselves and do not belong to the `doc.md`.

```js
const rule = require('unified-lint-rule')
const visit = require('unist-visit-util')
const generated = require('unist-util-generated')
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.
Expand All @@ -185,27 +189,32 @@ function isValidNode(node) {
return !node.url.endsWith('.gif')
}
}
function noGifAllowed(tree, file, options) {
visit(tree, 'image', visitor)
function visitor(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 extentions. Please do not use gifs',
node
)

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 extentions. Please do not use gifs',
node
)
}
}
}
})
}
}
module.exports = rule('remark-lint:no-gif-allowed', noGifAllowed)
)

export default remarkLintNoGifAllowed
```

[Back to Top](#contents)
Expand All @@ -218,11 +227,15 @@ You can do that by importing your rule and adding it in `plugins` array:

```js
// .remarkrc.js
const noGifAllowed = require('./rules/no-gif-allowed.js')
import remarkLintNoGifAllowed from './rules/no-gif-allowed.js'

module.exports = {
plugins: [noGifAllowed]
const plugins = {
plugins: [remarkLintNoGifAllowed]
}

const preset = {plugins}

export default preset
```

[Back to Top](#contents)
Expand Down
24 changes: 18 additions & 6 deletions doc/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,52 @@ information.
`false` turns rules off — the code no longer runs:

```js
import remarkLintFinalNewline from 'remark-lint-final-newline'

remark()
.use(require('remark-lint-final-newline'), false)
.use(remarkLintFinalNewline, false)
//
```

`true` turns a rule on again:

```js
import remarkLintFinalNewline from 'remark-lint-final-newline'

remark()
.use(require('remark-lint-final-newline'), true)
.use(remarkLintFinalNewline, true)
//
```

Rules can be configured with a severity too. The following ignores all
messages from the plugin:

```js
import remarkLintFinalNewline from 'remark-lint-final-newline'

remark()
.use(require('remark-lint-final-newline'), [0])
.use(remarkLintFinalNewline, [0])
//
```

…and passing `[1]` explicitly sets the normal behavior (warn for problems).
To trigger an error instead of a warning, pass `2`:

```js
import remarkLintFinalNewline from 'remark-lint-final-newline'

remark()
.use(require('remark-lint-final-newline'), [2])
.use(remarkLintFinalNewline, [2])
//
```

It’s also possible to pass both a severity and configuration:

```js
import remarkLintMaximumLineLength from 'remark-lint-maximum-line-length'

remark()
.use(require('remark-lint-maximum-line-length'), [2, 70])
.use(remarkLintMaximumLineLength, [2, 70])
//
```

Expand All @@ -58,8 +68,10 @@ Lastly, strings can also be passed, instead of numbers:
`error` instead of `2`.

```js
import remarkLintMaximumLineLength from 'remark-lint-maximum-line-length'

remark()
.use(require('remark-lint-maximum-line-length'), ['error', 70])
.use(remarkLintMaximumLineLength, ['error', 70])
//
```

Expand Down
15 changes: 9 additions & 6 deletions packages/remark-lint-blockquote-indentation/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,17 @@ remark -u lint -u lint-blockquote-indentation readme.md
Or use this on the API:

```diff
var remark = require('remark')
var report = require('vfile-reporter')
import {remark} from 'remark'
import {reporter} from 'vfile-reporter'
import remarkLint from 'remark-lint'
import remarkLintBlockquoteIndentation from 'remark-lint-blockquote-indentation'

remark()
.use(require('remark-lint'))
+ .use(require('remark-lint-blockquote-indentation'))
.process('_Emphasis_ and **importance**', function (err, file) {
console.error(report(err || file))
.use(remarkLint)
+ .use(remarkLintBlockquoteIndentation)
.process('_Emphasis_ and **importance**')
.then((file) => {
console.error(reporter(file))
})
```

Expand Down
15 changes: 9 additions & 6 deletions packages/remark-lint-checkbox-character-style/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,17 @@ remark -u lint -u lint-checkbox-character-style readme.md
Or use this on the API:

```diff
var remark = require('remark')
var report = require('vfile-reporter')
import {remark} from 'remark'
import {reporter} from 'vfile-reporter'
import remarkLint from 'remark-lint'
import remarkLintCheckboxCharacterStyle from 'remark-lint-checkbox-character-style'

remark()
.use(require('remark-lint'))
+ .use(require('remark-lint-checkbox-character-style'))
.process('_Emphasis_ and **importance**', function (err, file) {
console.error(report(err || file))
.use(remarkLint)
+ .use(remarkLintCheckboxCharacterStyle)
.process('_Emphasis_ and **importance**')
.then((file) => {
console.error(reporter(file))
})
```

Expand Down
15 changes: 9 additions & 6 deletions packages/remark-lint-checkbox-content-indent/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,17 @@ remark -u lint -u lint-checkbox-content-indent readme.md
Or use this on the API:

```diff
var remark = require('remark')
var report = require('vfile-reporter')
import {remark} from 'remark'
import {reporter} from 'vfile-reporter'
import remarkLint from 'remark-lint'
import remarkLintCheckboxContentIndent from 'remark-lint-checkbox-content-indent'

remark()
.use(require('remark-lint'))
+ .use(require('remark-lint-checkbox-content-indent'))
.process('_Emphasis_ and **importance**', function (err, file) {
console.error(report(err || file))
.use(remarkLint)
+ .use(remarkLintCheckboxContentIndent)
.process('_Emphasis_ and **importance**')
.then((file) => {
console.error(reporter(file))
})
```

Expand Down
15 changes: 9 additions & 6 deletions packages/remark-lint-code-block-style/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,17 @@ remark -u lint -u lint-code-block-style readme.md
Or use this on the API:

```diff
var remark = require('remark')
var report = require('vfile-reporter')
import {remark} from 'remark'
import {reporter} from 'vfile-reporter'
import remarkLint from 'remark-lint'
import remarkLintCodeBlockStyle from 'remark-lint-code-block-style'

remark()
.use(require('remark-lint'))
+ .use(require('remark-lint-code-block-style'))
.process('_Emphasis_ and **importance**', function (err, file) {
console.error(report(err || file))
.use(remarkLint)
+ .use(remarkLintCodeBlockStyle)
.process('_Emphasis_ and **importance**')
.then((file) => {
console.error(reporter(file))
})
```

Expand Down
15 changes: 9 additions & 6 deletions packages/remark-lint-definition-case/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,17 @@ remark -u lint -u lint-definition-case readme.md
Or use this on the API:

```diff
var remark = require('remark')
var report = require('vfile-reporter')
import {remark} from 'remark'
import {reporter} from 'vfile-reporter'
import remarkLint from 'remark-lint'
import remarkLintDefinitionCase from 'remark-lint-definition-case'

remark()
.use(require('remark-lint'))
+ .use(require('remark-lint-definition-case'))
.process('_Emphasis_ and **importance**', function (err, file) {
console.error(report(err || file))
.use(remarkLint)
+ .use(remarkLintDefinitionCase)
.process('_Emphasis_ and **importance**')
.then((file) => {
console.error(reporter(file))
})
```

Expand Down
15 changes: 9 additions & 6 deletions packages/remark-lint-definition-spacing/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,17 @@ remark -u lint -u lint-definition-spacing readme.md
Or use this on the API:

```diff
var remark = require('remark')
var report = require('vfile-reporter')
import {remark} from 'remark'
import {reporter} from 'vfile-reporter'
import remarkLint from 'remark-lint'
import remarkLintDefinitionSpacing from 'remark-lint-definition-spacing'

remark()
.use(require('remark-lint'))
+ .use(require('remark-lint-definition-spacing'))
.process('_Emphasis_ and **importance**', function (err, file) {
console.error(report(err || file))
.use(remarkLint)
+ .use(remarkLintDefinitionSpacing)
.process('_Emphasis_ and **importance**')
.then((file) => {
console.error(reporter(file))
})
```

Expand Down
15 changes: 9 additions & 6 deletions packages/remark-lint-emphasis-marker/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,17 @@ remark -u lint -u lint-emphasis-marker readme.md
Or use this on the API:

```diff
var remark = require('remark')
var report = require('vfile-reporter')
import {remark} from 'remark'
import {reporter} from 'vfile-reporter'
import remarkLint from 'remark-lint'
import remarkLintEmphasisMarker from 'remark-lint-emphasis-marker'

remark()
.use(require('remark-lint'))
+ .use(require('remark-lint-emphasis-marker'))
.process('_Emphasis_ and **importance**', function (err, file) {
console.error(report(err || file))
.use(remarkLint)
+ .use(remarkLintEmphasisMarker)
.process('_Emphasis_ and **importance**')
.then((file) => {
console.error(reporter(file))
})
```

Expand Down
15 changes: 9 additions & 6 deletions packages/remark-lint-fenced-code-flag/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,17 @@ remark -u lint -u lint-fenced-code-flag readme.md
Or use this on the API:

```diff
var remark = require('remark')
var report = require('vfile-reporter')
import {remark} from 'remark'
import {reporter} from 'vfile-reporter'
import remarkLint from 'remark-lint'
import remarkLintFencedCodeFlag from 'remark-lint-fenced-code-flag'

remark()
.use(require('remark-lint'))
+ .use(require('remark-lint-fenced-code-flag'))
.process('_Emphasis_ and **importance**', function (err, file) {
console.error(report(err || file))
.use(remarkLint)
+ .use(remarkLintFencedCodeFlag)
.process('_Emphasis_ and **importance**')
.then((file) => {
console.error(reporter(file))
})
```

Expand Down
Loading

0 comments on commit e7a7701

Please sign in to comment.