Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give rules access to settings #279

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
4 changes: 3 additions & 1 deletion .remarkrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import remarkGithub from 'remark-github'
import remarkValidateLinks from 'remark-validate-links'
import listOfPresets from './script/plugin/list-of-presets.js'
import listOfRules from './script/plugin/list-of-rules.js'
import listOfSettings from './script/plugin/list-of-settings.js'

const plugins = [
remarkPresetLintRecommended,
Expand All @@ -17,7 +18,8 @@ const plugins = [
remarkGithub,
remarkValidateLinks,
listOfPresets,
listOfRules
listOfRules,
listOfSettings
]

const preset = {plugins}
Expand Down
75 changes: 71 additions & 4 deletions packages/remark-lint-code-block-style/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* ## API
*
* The following options (default: `'consistent'`) are accepted:
* The following options (default: [`settings.fences`](https://github.com/remarkjs/remark-lint#configure) or `'consistent'`) are accepted:
*
* * `'fenced'`
* — prefer fenced code blocks:
Expand Down Expand Up @@ -37,8 +37,8 @@
* [`remark-stringify`](https://github.com/remarkjs/remark/tree/main/packages/remark-stringify)
* formats code blocks as fenced code when they have a language flag and as
* indented code otherwise.
* Pass
* [`fences: true`](https://github.com/remarkjs/remark/tree/main/packages/remark-stringify#optionsfences)
* Change
* [`settings.fences`](https://github.com/remarkjs/remark-lint#configure) to `true`
* to always use fenced code.
*
* @module code-block-style
Expand All @@ -58,6 +58,15 @@
* bravo()
*
* @example
* {"settings": {"fences": false}, "name": "ok.md"}
*
* alpha()
*
* Paragraph.
*
* bravo()
*
* @example
* {"config": "indented", "name": "not-ok.md", "label": "input"}
*
* ```
Expand All @@ -77,6 +86,25 @@
* 7:1-9:4: Code blocks should be indented
*
* @example
* {"settings": {"fences": false}, "name": "not-ok.md", "label": "input"}
*
* ```
* alpha()
* ```
*
* Paragraph.
*
* ```
* bravo()
* ```
*
* @example
* {"settings": {"fences": false}, "name": "not-ok.md", "label": "output"}
*
* 1:1-3:4: Code blocks should be indented
* 7:1-9:4: Code blocks should be indented
*
* @example
* {"config": "fenced", "name": "ok.md"}
*
* ```
Expand All @@ -90,6 +118,19 @@
* ```
*
* @example
* {"settings": {"fences": true}, "name": "ok.md"}
*
* ```
* alpha()
* ```
*
* Paragraph.
*
* ```
* bravo()
* ```
*
* @example
* {"config": "fenced", "name": "not-ok-fenced.md", "label": "input"}
*
* alpha()
Expand All @@ -105,6 +146,21 @@
* 5:1-5:12: Code blocks should be fenced
*
* @example
* {"settings": {"fences": true}, "name": "not-ok-fenced.md", "label": "input"}
*
* alpha()
*
* Paragraph.
*
* bravo()
*
* @example
* {"settings": {"fences": true}, "name": "not-ok-fenced.md", "label": "output"}
*
* 1:1-1:12: Code blocks should be fenced
* 5:1-5:12: Code blocks should be fenced
*
* @example
* {"name": "not-ok-consistent.md", "label": "input"}
*
* alpha()
Expand Down Expand Up @@ -143,9 +199,20 @@ const remarkLintCodeBlockStyle = lintRule(
url: 'https://github.com/remarkjs/remark-lint/tree/main/packages/remark-lint-code-block-style#readme'
},
/** @type {import('unified-lint-rule').Rule<Root, Options>} */
(tree, file, option = 'consistent') => {
function (tree, file, option) {
const value = String(file)

if (!option) {
/** @type {import('remark-stringify').Options} */
const settings = this.data().settings || {}
option =
settings.fences === true
? 'fenced'
: settings.fences === false
? 'indented'
: 'consistent'
}

if (
option !== 'consistent' &&
option !== 'fenced' &&
Expand Down
16 changes: 8 additions & 8 deletions packages/remark-lint-code-block-style/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ This rule is included in the following presets:
| Preset | Setting |
| - | - |
| [`remark-preset-lint-consistent`](https://github.com/remarkjs/remark-lint/tree/main/packages/remark-preset-lint-consistent) | `'consistent'` |
| [`remark-preset-lint-markdown-style-guide`](https://github.com/remarkjs/remark-lint/tree/main/packages/remark-preset-lint-markdown-style-guide) | `'fenced'` |
| [`remark-preset-lint-markdown-style-guide`](https://github.com/remarkjs/remark-lint/tree/main/packages/remark-preset-lint-markdown-style-guide) | [`settings.fences`](https://github.com/remarkjs/remark-lint#configure) is `true` |

## Install

Expand Down Expand Up @@ -124,7 +124,7 @@ The default export is `remarkLintCodeBlockStyle`.
This rule supports standard configuration that all remark lint rules accept
(such as `false` to turn it off or `[1, options]` to configure it).

The following options (default: `'consistent'`) are accepted:
The following options (default: [`settings.fences`](https://github.com/remarkjs/remark-lint#configure) or `'consistent'`) are accepted:

* `'fenced'`
— prefer fenced code blocks:
Expand Down Expand Up @@ -156,15 +156,15 @@ Due to this, it’s recommended to configure this rule with `'fenced'`.
[`remark-stringify`](https://github.com/remarkjs/remark/tree/main/packages/remark-stringify)
formats code blocks as fenced code when they have a language flag and as
indented code otherwise.
Pass
[`fences: true`](https://github.com/remarkjs/remark/tree/main/packages/remark-stringify#optionsfences)
Change
[`settings.fences`](https://github.com/remarkjs/remark-lint#configure) to `true`
to always use fenced code.

## Examples

##### `ok.md`

When configured with `'indented'`.
When [`settings.fences`](https://github.com/remarkjs/remark-lint#configure) is `false` and the rule is not configured.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • In each readme I've linked to the monorepo Configure section.


###### In

Expand All @@ -182,7 +182,7 @@ No messages.

##### `not-ok.md`

When configured with `'indented'`.
When [`settings.fences`](https://github.com/remarkjs/remark-lint#configure) is `false` and the rule is not configured.

###### In

Expand All @@ -207,7 +207,7 @@ bravo()

##### `ok.md`

When configured with `'fenced'`.
When [`settings.fences`](https://github.com/remarkjs/remark-lint#configure) is `true` and the rule is not configured.

###### In

Expand All @@ -229,7 +229,7 @@ No messages.

##### `not-ok-fenced.md`

When configured with `'fenced'`.
When [`settings.fences`](https://github.com/remarkjs/remark-lint#configure) is `true` and the rule is not configured.

###### In

Expand Down
49 changes: 45 additions & 4 deletions packages/remark-lint-emphasis-marker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* ## API
*
* The following options (default: `'consistent'`) are accepted:
* The following options (default: [`settings.emphasis`](https://github.com/remarkjs/remark-lint#configure) or `'consistent'`) are accepted:
*
* * `'*'`
* — prefer asterisks
Expand All @@ -29,8 +29,8 @@
*
* [`remark-stringify`](https://github.com/remarkjs/remark/tree/main/packages/remark-stringify)
* formats emphasis with asterisks by default.
* Pass
* [`emphasis: '_'`](https://github.com/remarkjs/remark/tree/main/packages/remark-stringify#optionsemphasis)
* Change
* [`settings.emphasis`](https://github.com/remarkjs/remark-lint#configure) to `'_'`
* to always use underscores.
*
* @module emphasis-marker
Expand All @@ -45,6 +45,11 @@
* *foo*
*
* @example
* {"settings": {"emphasis": "*"}, "name": "ok.md"}
*
* *foo*
*
* @example
* {"config": "*", "name": "not-ok.md", "label": "input"}
*
* _foo_
Expand All @@ -55,11 +60,26 @@
* 1:1-1:6: Emphasis should use `*` as a marker
*
* @example
* {"settings": {"emphasis": "*"}, "name": "not-ok.md", "label": "input"}
*
* _foo_
*
* @example
* {"settings": {"emphasis": "*"}, "name": "not-ok.md", "label": "output"}
*
* 1:1-1:6: Emphasis should use `*` as a marker
*
* @example
* {"config": "_", "name": "ok.md"}
*
* _foo_
*
* @example
* {"settings": {"emphasis": "_"}, "name": "ok.md"}
*
* _foo_
*
* @example
* {"config": "_", "name": "not-ok.md", "label": "input"}
*
* *foo*
Expand All @@ -70,6 +90,16 @@
* 1:1-1:6: Emphasis should use `_` as a marker
*
* @example
* {"settings": {"emphasis": "_"}, "name": "not-ok.md", "label": "input"}
*
* *foo*
*
* @example
* {"settings": {"emphasis": "_"}, "name": "not-ok.md", "label": "output"}
*
* 1:1-1:6: Emphasis should use `_` as a marker
*
* @example
* {"name": "not-ok.md", "label": "input"}
*
* *foo*
Expand All @@ -84,6 +114,11 @@
* {"config": "💩", "name": "not-ok.md", "label": "output", "positionless": true}
*
* 1:1: Incorrect emphasis marker `💩`: use either `'consistent'`, `'*'`, or `'_'`
*
* @example
* {"settings": {"emphasis": "💩"}, "name": "not-ok.md", "label": "output", "positionless": true}
*
* 1:1: Incorrect emphasis marker `💩`: use either `'consistent'`, `'*'`, or `'_'`
*/

/**
Expand All @@ -102,9 +137,15 @@ const remarkLintEmphasisMarker = lintRule(
url: 'https://github.com/remarkjs/remark-lint/tree/main/packages/remark-lint-emphasis-marker#readme'
},
/** @type {import('unified-lint-rule').Rule<Root, Options>} */
(tree, file, option = 'consistent') => {
function (tree, file, option) {
const value = String(file)

if (!option) {
/** @type {import('remark-stringify').Options} */
const settings = this.data().settings || {}
option = settings.emphasis || 'consistent'
}

if (option !== '*' && option !== '_' && option !== 'consistent') {
file.fail(
'Incorrect emphasis marker `' +
Expand Down
18 changes: 9 additions & 9 deletions packages/remark-lint-emphasis-marker/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ This rule is included in the following presets:
| Preset | Setting |
| - | - |
| [`remark-preset-lint-consistent`](https://github.com/remarkjs/remark-lint/tree/main/packages/remark-preset-lint-consistent) | `'consistent'` |
| [`remark-preset-lint-markdown-style-guide`](https://github.com/remarkjs/remark-lint/tree/main/packages/remark-preset-lint-markdown-style-guide) | `'*'` |
| [`remark-preset-lint-markdown-style-guide`](https://github.com/remarkjs/remark-lint/tree/main/packages/remark-preset-lint-markdown-style-guide) | [`settings.emphasis`](https://github.com/remarkjs/remark-lint#configure) is `'*'` |

## Install

Expand Down Expand Up @@ -124,7 +124,7 @@ The default export is `remarkLintEmphasisMarker`.
This rule supports standard configuration that all remark lint rules accept
(such as `false` to turn it off or `[1, options]` to configure it).

The following options (default: `'consistent'`) are accepted:
The following options (default: [`settings.emphasis`](https://github.com/remarkjs/remark-lint#configure) or `'consistent'`) are accepted:

* `'*'`
— prefer asterisks
Expand All @@ -148,15 +148,15 @@ can be used for more constructs, it’s recommended to prefer asterisks.

[`remark-stringify`](https://github.com/remarkjs/remark/tree/main/packages/remark-stringify)
formats emphasis with asterisks by default.
Pass
[`emphasis: '_'`](https://github.com/remarkjs/remark/tree/main/packages/remark-stringify#optionsemphasis)
Change
[`settings.emphasis`](https://github.com/remarkjs/remark-lint#configure) to `'_'`
to always use underscores.

## Examples

##### `ok.md`

When configured with `'*'`.
When [`settings.emphasis`](https://github.com/remarkjs/remark-lint#configure) is `'*'` and the rule is not configured.

###### In

Expand All @@ -170,7 +170,7 @@ No messages.

##### `not-ok.md`

When configured with `'*'`.
When [`settings.emphasis`](https://github.com/remarkjs/remark-lint#configure) is `'*'` and the rule is not configured.

###### In

Expand All @@ -186,7 +186,7 @@ _foo_

##### `ok.md`

When configured with `'_'`.
When [`settings.emphasis`](https://github.com/remarkjs/remark-lint#configure) is `'_'` and the rule is not configured.

###### In

Expand All @@ -200,7 +200,7 @@ No messages.

##### `not-ok.md`

When configured with `'_'`.
When [`settings.emphasis`](https://github.com/remarkjs/remark-lint#configure) is `'_'` and the rule is not configured.

###### In

Expand Down Expand Up @@ -231,7 +231,7 @@ _bar_

##### `not-ok.md`

When configured with `'💩'`.
When [`settings.emphasis`](https://github.com/remarkjs/remark-lint#configure) is `'💩'` and the rule is not configured.

###### Out

Expand Down
Loading