Skip to content

Commit

Permalink
Add better messages, rewrite and improve rules
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 19, 2024
1 parent ccea691 commit 45aeac2
Show file tree
Hide file tree
Showing 192 changed files with 8,772 additions and 4,455 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"remark-cli": "^12.0.0",
"remark-comment-config": "^8.0.0",
"remark-directive": "^3.0.0",
"remark-frontmatter": "^5.0.0",
"remark-gfm": "^4.0.0",
"remark-github": "^12.0.0",
"remark-math": "^6.0.0",
Expand Down
105 changes: 68 additions & 37 deletions packages/remark-lint-blockquote-indentation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,41 +65,52 @@
* @author Titus Wormer
* @copyright 2015 Titus Wormer
* @license MIT
*
* @example
* {"name": "ok.md", "config": 4}
* {"config": 2, "name": "ok-2.md"}
*
* > Mercury.
*
* > Hello
* Venus.
*
* Paragraph.
* > Earth.
*
* > World
* @example
* {"name": "ok.md", "config": 2}
* {"config": 4, "name": "ok-4.md"}
*
* > Hello
* > Mercury.
*
* Paragraph.
* Venus.
*
* > World
* > Earth.
*
* @example
* {"name": "not-ok.md", "label": "input"}
* { "name": "ok-tab.md"}
*
* >␉Mercury.
*
* > Hello
* @example
* {"label": "input", "name": "not-ok.md"}
*
* Paragraph.
* > Mercury.
*
* > World
* Venus.
*
* Paragraph.
* > Earth.
*
* > World
* Mars.
*
* > Jupiter
* @example
* {"name": "not-ok.md", "label": "output"}
* {"label": "output", "name": "not-ok.md"}
*
* 5:5: Unexpected `4` spaces between block quote marker and content, expected `3` spaces, remove `1` space
* 9:3: Unexpected `2` spaces between block quote marker and content, expected `3` spaces, add `1` space
*
* 5:5: Remove 1 space between block quote and content
* 9:3: Add 1 space between block quote and content
* @example
* {"config": "🌍", "label": "output", "name": "not-ok-options.md", "positionless": true}
*
* 1:1: Unexpected value `🌍` for `options`, expected `number` or `'consistent'`
*/

/**
Expand All @@ -114,7 +125,7 @@
import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import {pointStart} from 'unist-util-position'
import {visit} from 'unist-util-visit'
import {visitParents} from 'unist-util-visit-parents'

const remarkLintBlockquoteIndentation = lintRule(
{
Expand All @@ -130,33 +141,53 @@ const remarkLintBlockquoteIndentation = lintRule(
* Nothing.
*/
function (tree, file, options) {
let option = options || 'consistent'
/** @type {number | undefined} */
let expected

if (options === null || options === undefined || options === 'consistent') {
// Empty.
} else if (typeof options === 'number') {
expected = options
} else {
file.fail(
'Unexpected value `' +
options +
"` for `options`, expected `number` or `'consistent'`"
)
}

visit(tree, 'blockquote', function (node) {
visitParents(tree, 'blockquote', function (node, parents) {
const start = pointStart(node)
const head = pointStart(node.children[0])
const headStart = pointStart(node.children[0])

if (head && start) {
const count = head.column - start.column
if (headStart && start) {
const actual = headStart.column - start.column

if (option === 'consistent') {
option = count
} else {
const diff = option - count

if (diff !== 0) {
const abs = Math.abs(diff)
if (expected) {
const difference = expected - actual
const differenceAbsolute = Math.abs(difference)

if (difference !== 0) {
file.message(
(diff > 0 ? 'Add' : 'Remove') +
' ' +
abs +
' ' +
pluralize('space', abs) +
' between block quote and content',
head
'Unexpected `' +
actual +
'` ' +
pluralize('space', actual) +
' between block quote marker and content, expected `' +
expected +
'` ' +
pluralize('space', expected) +
', ' +
(difference > 0 ? 'add' : 'remove') +
' `' +
differenceAbsolute +
'` ' +
pluralize('space', differenceAbsolute),
{ancestors: [...parents, node], place: headStart}
)
}
} else {
expected = actual
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion packages/remark-lint-blockquote-indentation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"pluralize": "^8.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit": "^5.0.0"
"unist-util-visit-parents": "^6.0.0"
},
"scripts": {},
"typeCoverage": {
Expand Down
56 changes: 39 additions & 17 deletions packages/remark-lint-blockquote-indentation/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,36 +171,48 @@ Due to this, it’s recommended to configure this rule with `2`.
## Examples
##### `ok.md`
##### `ok-2.md`
When configured with `4`.
When configured with `2`.
###### In
```markdown
> Hello
> Mercury.

Paragraph.
Venus.

> World
> Earth.
```

###### Out

No messages.

##### `ok.md`
##### `ok-4.md`

When configured with `2`.
When configured with `4`.

###### In

```markdown
> Hello
> Mercury.

Paragraph.
Venus.

> World
> Earth.
```

###### Out

No messages.

##### `ok-tab.md`

###### In

```markdown
>␉Mercury.
```

###### Out
Expand All @@ -212,22 +224,32 @@ No messages.
###### In

```markdown
> Hello
> Mercury.

Venus.

> Earth.

Paragraph.
Mars.

> World
> Jupiter
```

Paragraph.
###### Out

> World
```text
5:5: Unexpected `4` spaces between block quote marker and content, expected `3` spaces, remove `1` space
9:3: Unexpected `2` spaces between block quote marker and content, expected `3` spaces, add `1` space
```

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

When configured with `'🌍'`.

###### Out

```text
5:5: Remove 1 space between block quote and content
9:3: Add 1 space between block quote and content
1:1: Unexpected value `🌍` for `options`, expected `number` or `'consistent'`
```

## Compatibility
Expand Down
Loading

0 comments on commit 45aeac2

Please sign in to comment.