-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rules to prefer collapsed over full references
- Loading branch information
Showing
7 changed files
with
512 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/remark-lint-no-unneeded-full-reference-image/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
packages/remark-lint-no-unneeded-full-reference-image/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
154
packages/remark-lint-no-unneeded-full-reference-image/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
71
packages/remark-lint-no-unneeded-full-reference-link/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
packages/remark-lint-no-unneeded-full-reference-link/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.