Skip to content

Commit

Permalink
Add rules to prefer collapsed over full references
Browse files Browse the repository at this point in the history
Closes GH-192.
Closes GH-212.

Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
  • Loading branch information
wooorm authored Jun 20, 2019
1 parent c0116c4 commit d7e95ef
Show file tree
Hide file tree
Showing 7 changed files with 512 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ Rules][external].
* [`no-table-indentation`](https://github.com/remarkjs/remark-lint/tree/master/packages/remark-lint-no-table-indentation) — warn when tables are indented
* [`no-tabs`](https://github.com/remarkjs/remark-lint/tree/master/packages/remark-lint-no-tabs) — warn when hard tabs are used instead of spaces
* [`no-undefined-references`](https://github.com/remarkjs/remark-lint/tree/master/packages/remark-lint-no-undefined-references) — warn when references to undefined definitions are found
* [`no-unneeded-full-reference-image`](https://github.com/remarkjs/remark-lint/tree/master/packages/remark-lint-no-unneeded-full-reference-image) — warn when full reference images are used if they can be collapsed
* [`no-unneeded-full-reference-link`](https://github.com/remarkjs/remark-lint/tree/master/packages/remark-lint-no-unneeded-full-reference-link) — warn when full reference links are used if they can be collapsed
* [`no-unused-definitions`](https://github.com/remarkjs/remark-lint/tree/master/packages/remark-lint-no-unused-definitions) — warn when unused definitions are found
* [`ordered-list-marker-style`](https://github.com/remarkjs/remark-lint/tree/master/packages/remark-lint-ordered-list-marker-style) — warn when the markers of ordered lists violate a given style
* [`ordered-list-marker-value`](https://github.com/remarkjs/remark-lint/tree/master/packages/remark-lint-ordered-list-marker-value) — warn when the marker value of ordered lists violates a given style
Expand Down
65 changes: 65 additions & 0 deletions packages/remark-lint-no-unneeded-full-reference-image/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @author Titus Wormer
* @copyright 2019 Titus Wormer
* @license MIT
* @module no-unneeded-full-reference-image
* @fileoverview
* Warn when full reference images are used that could be collapsed.
*
* Full references (such as `[Remark][remark]`) can also be written as a
* collapsed reference (`[Remark][]`) if normalising the reference text yields
* the label.
*
* @example {"name": "valid.md"}
*
* ![alpha][]
* ![Bravo][]
* ![Charlie][delta]
*
* @example {"name": "invalid.md", "label": "input"}
*
* ![alpha][alpha]
* ![Bravo][bravo]
* ![charlie][Charlie]
*
* @example {"name": "invalid.md", "label": "output"}
*
* 1:1-1:16: Remove the image label as it matches the reference text
* 2:1-2:16: Remove the image label as it matches the reference text
* 3:1-3:20: Remove the image label as it matches the reference text
*/

'use strict'

var rule = require('unified-lint-rule')
var visit = require('unist-util-visit')
var generated = require('unist-util-generated')
var collapseWhiteSpace = require('collapse-white-space')

module.exports = rule(
'remark-lint:no-unneeded-full-reference-image',
noUnneededFullReferenceImage
)

var reason = 'Remove the image label as it matches the reference text'

function noUnneededFullReferenceImage(tree, file) {
visit(tree, 'imageReference', visitor)

function visitor(node) {
if (
generated(node) ||
node.referenceType !== 'full' ||
normalize(node.alt) !== node.identifier
) {
return
}

file.message(reason, node)
}
}

// See: <https://github.com/remarkjs/remark/blob/cc7867b/packages/remark-parse/lib/util/normalize.js>
function normalize(value) {
return collapseWhiteSpace(value).toLowerCase()
}
31 changes: 31 additions & 0 deletions packages/remark-lint-no-unneeded-full-reference-image/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "remark-lint-no-unneeded-full-reference-image",
"version": "0.0.0",
"description": "remark-lint rule to warn when full reference images are used if they can be collapsed",
"license": "MIT",
"keywords": [
"remark",
"lint",
"rule",
"full",
"collapsed",
"reference",
"image"
],
"repository": "https://github.com/remarkjs/remark-lint/tree/master/packages/remark-lint-no-unneeded-full-reference-image",
"bugs": "https://github.com/remarkjs/remark-lint/issues",
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"files": [
"index.js"
],
"dependencies": {
"collapse-white-space": "^1.0.0",
"unified-lint-rule": "^1.0.0",
"unist-util-generated": "^1.1.0",
"unist-util-visit": "^1.4.0"
},
"xo": false
}
154 changes: 154 additions & 0 deletions packages/remark-lint-no-unneeded-full-reference-image/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<!--This file is generated-->

# remark-lint-no-unneeded-full-reference-image

[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]

Warn when full reference images are used that could be collapsed.

Full references (such as `[Remark][remark]`) can also be written as a
collapsed reference (`[Remark][]`) if normalising the reference text yields
the label.

## Presets

This rule is not included in any default preset

## Example

##### `valid.md`

###### In

```markdown
![alpha][]
![Bravo][]
![Charlie][delta]
```

###### Out

No messages.

##### `invalid.md`

###### In

```markdown
![alpha][alpha]
![Bravo][bravo]
![charlie][Charlie]
```

###### Out

```text
1:1-1:16: Remove the image label as it matches the reference text
2:1-2:16: Remove the image label as it matches the reference text
3:1-3:20: Remove the image label as it matches the reference text
```

## Install

[npm][]:

```sh
npm install remark-lint-no-unneeded-full-reference-image
```

## Use

You probably want to use it on the CLI through a config file:

```diff
...
"remarkConfig": {
"plugins": [
...
"lint",
+ "lint-no-unneeded-full-reference-image",
...
]
}
...
```

Or use it on the CLI directly

```sh
remark -u lint -u lint-no-unneeded-full-reference-image readme.md
```

Or use this on the API:

```diff
var remark = require('remark');
var report = require('vfile-reporter');

remark()
.use(require('remark-lint'))
+ .use(require('remark-lint-no-unneeded-full-reference-image'))
.process('_Emphasis_ and **importance**', function (err, file) {
console.error(report(err || file));
});
```

## Contribute

See [`contributing.md`][contributing] in [`remarkjs/.github`][health] for ways
to get started.
See [`support.md`][support] for ways to get help.

This project has a [Code of Conduct][coc].
By interacting with this repository, organisation, or community you agree to
abide by its terms.

## License

[MIT][license] © [Titus Wormer][author]

[build-badge]: https://img.shields.io/travis/remarkjs/remark-lint/master.svg

[build]: https://travis-ci.org/remarkjs/remark-lint

[coverage-badge]: https://img.shields.io/codecov/c/github/remarkjs/remark-lint.svg

[coverage]: https://codecov.io/github/remarkjs/remark-lint

[downloads-badge]: https://img.shields.io/npm/dm/remark-lint-no-unneeded-full-reference-image.svg

[downloads]: https://www.npmjs.com/package/remark-lint-no-unneeded-full-reference-image

[size-badge]: https://img.shields.io/bundlephobia/minzip/remark-lint-no-unneeded-full-reference-image.svg

[size]: https://bundlephobia.com/result?p=remark-lint-no-unneeded-full-reference-image

[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg

[backers-badge]: https://opencollective.com/unified/backers/badge.svg

[collective]: https://opencollective.com/unified

[chat-badge]: https://img.shields.io/badge/join%20the%20community-on%20spectrum-7b16ff.svg

[chat]: https://spectrum.chat/unified/remark

[npm]: https://docs.npmjs.com/cli/install

[health]: https://github.com/remarkjs/.github

[contributing]: https://github.com/remarkjs/.github/blob/master/contributing.md

[support]: https://github.com/remarkjs/.github/blob/master/support.md

[coc]: https://github.com/remarkjs/.github/blob/master/code-of-conduct.md

[license]: https://github.com/remarkjs/remark-lint/blob/master/license

[author]: https://wooorm.com
71 changes: 71 additions & 0 deletions packages/remark-lint-no-unneeded-full-reference-link/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @author Titus Wormer
* @copyright 2019 Titus Wormer
* @license MIT
* @module no-unneeded-full-reference-link
* @fileoverview
* Warn when full reference links are used that could be collapsed.
*
* Full references (such as `[Remark][remark]`) can also be written as a
* collapsed reference (`[Remark][]`) if normalising the reference text yields
* the label.
*
* @example {"name": "valid.md"}
*
* [alpha][]
* [Bravo][]
* [Charlie][delta]
*
* This only works if the link text is a `text` node:
* [`echo`][]
* [*foxtrot*][]
*
* @example {"name": "invalid.md", "label": "input"}
*
* [alpha][alpha]
* [Bravo][bravo]
* [charlie][Charlie]
*
* @example {"name": "invalid.md", "label": "output"}
*
* 1:1-1:15: Remove the link label as it matches the reference text
* 2:1-2:15: Remove the link label as it matches the reference text
* 3:1-3:19: Remove the link label as it matches the reference text
*/

'use strict'

var rule = require('unified-lint-rule')
var visit = require('unist-util-visit')
var generated = require('unist-util-generated')
var collapseWhiteSpace = require('collapse-white-space')

module.exports = rule(
'remark-lint:no-unneeded-full-reference-link',
noUnneededFullReferenceLink
)

var reason = 'Remove the link label as it matches the reference text'

function noUnneededFullReferenceLink(tree, file) {
visit(tree, 'linkReference', visitor)

function visitor(node) {
if (
generated(node) ||
node.referenceType !== 'full' ||
node.children.length !== 1 ||
node.children[0].type !== 'text' ||
normalize(node.children[0].value) !== node.identifier
) {
return
}

file.message(reason, node)
}
}

// See: <https://github.com/remarkjs/remark/blob/cc7867b/packages/remark-parse/lib/util/normalize.js>
function normalize(value) {
return collapseWhiteSpace(value).toLowerCase()
}
31 changes: 31 additions & 0 deletions packages/remark-lint-no-unneeded-full-reference-link/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "remark-lint-no-unneeded-full-reference-link",
"version": "0.0.0",
"description": "remark-lint rule to warn when full reference links are used if they can be collapsed",
"license": "MIT",
"keywords": [
"remark",
"lint",
"rule",
"full",
"collapsed",
"reference",
"link"
],
"repository": "https://github.com/remarkjs/remark-lint/tree/master/packages/remark-lint-no-unneeded-full-reference-link",
"bugs": "https://github.com/remarkjs/remark-lint/issues",
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"files": [
"index.js"
],
"dependencies": {
"collapse-white-space": "^1.0.0",
"unified-lint-rule": "^1.0.0",
"unist-util-generated": "^1.1.0",
"unist-util-visit": "^1.4.0"
},
"xo": false
}
Loading

0 comments on commit d7e95ef

Please sign in to comment.