remark-lint-no-reference-like-url
Advanced tools
Comparing version
@@ -1,7 +0,8 @@ | ||
export default remarkLintNoReferenceLikeUrl | ||
export type Root = import('mdast').Root | ||
declare const remarkLintNoReferenceLikeUrl: import('unified').Plugin< | ||
void[] | [unknown], | ||
import('mdast').Root, | ||
import('mdast').Root | ||
> | ||
export default remarkLintNoReferenceLikeUrl; | ||
export type Nodes = import('mdast').Nodes; | ||
export type Root = import('mdast').Root; | ||
declare const remarkLintNoReferenceLikeUrl: { | ||
(config?: unknown): ((tree: import("mdast").Root, file: import("vfile").VFile, next: import("unified").TransformCallback<import("mdast").Root>) => undefined) | undefined; | ||
readonly name: string; | ||
}; | ||
//# sourceMappingURL=index.d.ts.map |
134
index.js
/** | ||
* remark-lint rule to warn when URLs are also defined identifiers. | ||
* | ||
* ## What is this? | ||
* | ||
* This package checks for likely broken URLs that should probably have been | ||
* references. | ||
* | ||
* ## When should I use this? | ||
* | ||
* You can use this package to check for broken URLs that should likely | ||
* have been references. | ||
* You can use this package to check links. | ||
* | ||
* ## API | ||
* | ||
* ### `unified().use(remarkLintNoReferenceLikeUrl)` | ||
* | ||
* Warn when URLs are also defined identifiers. | ||
* | ||
* ###### Parameters | ||
* | ||
* There are no options. | ||
* | ||
* ###### Returns | ||
* | ||
* Transform ([`Transformer` from `unified`][github-unified-transformer]). | ||
* | ||
* ## Recommendation | ||
* | ||
* While full URLs for definition identifiers are okay | ||
* (`[https://example.com]: https://example.com`), and what looks like an | ||
* identifier could be an actual URL (`[text](alpha)`), the more common case | ||
* is that, assuming a definition `[alpha]: https://example.com`, then a link | ||
* of (`[text](alpha)`) should instead’ve been `[text][alpha]`. | ||
* (`[https://example.com]: https://example.com`), | ||
* and what looks like an identifier could be an actual URL (`[text](alpha)`), | ||
* the more common case is that, | ||
* assuming a definition `[alpha]: https://example.com`, | ||
* then a link `[text](alpha)` should instead have been `[text][alpha]`. | ||
* | ||
* [api-remark-lint-no-reference-like-url]: #unifieduseremarklintnoreferencelikeurl | ||
* [github-unified-transformer]: https://github.com/unifiedjs/unified#transformer | ||
* | ||
* @module no-reference-like-url | ||
* @summary | ||
* remark-lint rule to warn when URLs are also defined identifiers. | ||
* @author Titus Wormer | ||
* @copyright 2016 Titus Wormer | ||
* @license MIT | ||
* | ||
* @example | ||
* {"name": "ok.md"} | ||
* | ||
* [Alpha](http://example.com). | ||
* [**Mercury**][mercury] is the first planet from the sun. | ||
* | ||
* [bravo]: https://example.com | ||
* [mercury]: https://example.com/mercury/ | ||
* | ||
* @example | ||
* {"name": "not-ok.md", "label": "input"} | ||
* {"label": "input", "name": "not-ok.md"} | ||
* | ||
* [Charlie](delta). | ||
* [**Mercury**](mercury) is the first planet from the sun. | ||
* | ||
* [delta]: https://example.com | ||
* [mercury]: https://example.com/mercury/ | ||
* @example | ||
* {"label": "output", "name": "not-ok.md"} | ||
* | ||
* 1:1-1:23: Unexpected resource link (`[text](url)`) with URL that matches a definition identifier (as `mercury`), expected reference (`[text][id]`) | ||
* | ||
* @example | ||
* {"name": "not-ok.md", "label": "output"} | ||
* {"label": "input", "name": "image.md"} | ||
* | ||
* 1:1-1:17: Did you mean to use `[delta]` instead of `(delta)`, a reference? | ||
* . | ||
* | ||
* [mercury]: https://example.com/mercury.jpg | ||
* @example | ||
* {"label": "output", "name": "image.md"} | ||
* | ||
* 1:1-1:36: Unexpected resource image (``) with URL that matches a definition identifier (as `mercury`), expected reference (`![text][id]`) | ||
*/ | ||
/** | ||
* @typedef {import('mdast').Nodes} Nodes | ||
* @typedef {import('mdast').Root} Root | ||
*/ | ||
import {ok as assert} from 'devlop' | ||
import {lintRule} from 'unified-lint-rule' | ||
import {generated} from 'unist-util-generated' | ||
import {visit} from 'unist-util-visit' | ||
import {visitParents} from 'unist-util-visit-parents' | ||
import {VFileMessage} from 'vfile-message' | ||
@@ -58,29 +89,58 @@ const remarkLintNoReferenceLikeUrl = lintRule( | ||
}, | ||
/** @type {import('unified-lint-rule').Rule<Root, void>} */ | ||
(tree, file) => { | ||
/** @type {Array<string>} */ | ||
const identifiers = [] | ||
/** | ||
* @param {Root} tree | ||
* Tree. | ||
* @returns {undefined} | ||
* Nothing. | ||
*/ | ||
function (tree, file) { | ||
/** @type {Map<string, Array<Nodes>>} */ | ||
const definitions = new Map() | ||
/** @type {Array<Array<Nodes>>} */ | ||
const references = [] | ||
visit(tree, 'definition', (node) => { | ||
if (!generated(node)) { | ||
identifiers.push(node.identifier.toLowerCase()) | ||
visitParents(tree, function (node, ancestors) { | ||
if (node.type === 'definition') { | ||
definitions.set(node.identifier.toLowerCase(), [...ancestors, node]) | ||
} else if (node.type === 'image' || node.type === 'link') { | ||
references.push([...ancestors, node]) | ||
} | ||
}) | ||
visit(tree, (node) => { | ||
if ( | ||
(node.type === 'image' || node.type === 'link') && | ||
identifiers.includes(node.url.toLowerCase()) | ||
) { | ||
for (const ancestors of references) { | ||
const node = ancestors.at(-1) | ||
assert(node) // Always defined. | ||
assert(node.type === 'image' || node.type === 'link') // Always media. | ||
const maybeIdentifier = node.url.toLowerCase() | ||
const definitionAncestors = definitions.get(maybeIdentifier) | ||
if (node.position && definitionAncestors) { | ||
const definition = definitionAncestors.at(-1) | ||
assert(definition) // Always defined. | ||
assert(definition.type === 'definition') // Always definition. | ||
const prefix = node.type === 'image' ? '!' : '' | ||
file.message( | ||
'Did you mean to use `[' + | ||
node.url + | ||
']` instead of ' + | ||
'`(' + | ||
node.url + | ||
')`, a reference?', | ||
node | ||
'Unexpected resource ' + | ||
node.type + | ||
' (`' + | ||
prefix + | ||
'[text](url)`) with URL that matches a definition identifier (as `' + | ||
definition.identifier + | ||
'`), expected reference (`' + | ||
prefix + | ||
'[text][id]`)', | ||
{ | ||
ancestors, | ||
cause: new VFileMessage('Definition defined here', { | ||
ancestors: definitionAncestors, | ||
place: definition.position, | ||
source: 'remark-lint', | ||
ruleId: 'no-reference-like-url' | ||
}), | ||
place: node.position | ||
} | ||
) | ||
} | ||
}) | ||
} | ||
} | ||
@@ -87,0 +147,0 @@ ) |
{ | ||
"name": "remark-lint-no-reference-like-url", | ||
"version": "3.1.2", | ||
"version": "4.0.0", | ||
"description": "remark-lint rule to warn when URLs are also defined identifiers", | ||
"license": "MIT", | ||
"keywords": [ | ||
"definition", | ||
"lint", | ||
"referece", | ||
"remark", | ||
"lint", | ||
"remark-lint", | ||
"remark-lint-rule", | ||
"rule", | ||
"remark-lint-rule", | ||
"referece", | ||
"url", | ||
"definition" | ||
"url" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/remarkjs/remark-lint", | ||
"directory": "packages/remark-lint-no-reference-like-url" | ||
}, | ||
"repository": "https://github.com/remarkjs/remark-lint/tree/main/packages/remark-lint-no-reference-like-url", | ||
"bugs": "https://github.com/remarkjs/remark-lint/issues", | ||
@@ -27,27 +24,33 @@ "funding": { | ||
"contributors": [ | ||
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)" | ||
"Titus Wormer <tituswormer@gmail.com>" | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"exports": "./index.js", | ||
"files": [ | ||
"index.d.ts", | ||
"index.d.ts.map", | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"@types/mdast": "^3.0.0", | ||
"unified": "^10.0.0", | ||
"unified-lint-rule": "^2.0.0", | ||
"unist-util-generated": "^2.0.0", | ||
"unist-util-visit": "^4.0.0" | ||
"@types/mdast": "^4.0.0", | ||
"devlop": "^1.0.0", | ||
"unified-lint-rule": "^3.0.0", | ||
"unist-util-position": "^5.0.0", | ||
"unist-util-visit-parents": "^6.0.0", | ||
"vfile-message": "^4.0.0" | ||
}, | ||
"scripts": {}, | ||
"xo": false, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true, | ||
"ignoreCatch": true | ||
"ignoreCatch": true, | ||
"strict": true | ||
}, | ||
"xo": { | ||
"prettier": true, | ||
"rules": { | ||
"capitalized-comments": "off" | ||
} | ||
} | ||
} |
209
readme.md
@@ -5,46 +5,45 @@ <!--This file is generated--> | ||
[![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] | ||
[![Build][badge-build-image]][badge-build-url] | ||
[![Coverage][badge-coverage-image]][badge-coverage-url] | ||
[![Downloads][badge-downloads-image]][badge-downloads-url] | ||
[![Size][badge-size-image]][badge-size-url] | ||
[![Sponsors][badge-funding-sponsors-image]][badge-funding-url] | ||
[![Backers][badge-funding-backers-image]][badge-funding-url] | ||
[![Chat][badge-chat-image]][badge-chat-url] | ||
[`remark-lint`][mono] rule to warn when URLs are also defined identifiers. | ||
[`remark-lint`][github-remark-lint] rule to warn when URLs are also defined identifiers. | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Presets](#presets) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`unified().use(remarkLintNoReferenceLikeUrl[, config])`](#unifieduseremarklintnoreferencelikeurl-config) | ||
* [Recommendation](#recommendation) | ||
* [Examples](#examples) | ||
* [Compatibility](#compatibility) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Presets](#presets) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`unified().use(remarkLintNoReferenceLikeUrl)`](#unifieduseremarklintnoreferencelikeurl) | ||
* [Recommendation](#recommendation) | ||
* [Examples](#examples) | ||
* [Compatibility](#compatibility) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This package is a [unified][] ([remark][]) plugin, specifically a `remark-lint` | ||
rule. | ||
Lint rules check markdown code style. | ||
This package checks for likely broken URLs that should probably have been | ||
references. | ||
## When should I use this? | ||
You can use this package to check for broken URLs that should likely | ||
have been references. | ||
You can use this package to check links. | ||
## Presets | ||
This rule is not included in a preset maintained here. | ||
This plugin is not included in presets maintained here. | ||
## Install | ||
This package is [ESM only][esm]. | ||
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]: | ||
This package is [ESM only][github-gist-esm]. | ||
In Node.js (version 16+), | ||
install with [npm][npm-install]: | ||
@@ -55,13 +54,13 @@ ```sh | ||
In Deno with [`esm.sh`][esmsh]: | ||
In Deno with [`esm.sh`][esm-sh]: | ||
```js | ||
import remarkLintNoReferenceLikeUrl from 'https://esm.sh/remark-lint-no-reference-like-url@3' | ||
import remarkLintNoReferenceLikeUrl from 'https://esm.sh/remark-lint-no-reference-like-url@4' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
In browsers with [`esm.sh`][esm-sh]: | ||
```html | ||
<script type="module"> | ||
import remarkLintNoReferenceLikeUrl from 'https://esm.sh/remark-lint-no-reference-like-url@3?bundle' | ||
import remarkLintNoReferenceLikeUrl from 'https://esm.sh/remark-lint-no-reference-like-url@4?bundle' | ||
</script> | ||
@@ -75,18 +74,20 @@ ``` | ||
```js | ||
import remarkLint from 'remark-lint' | ||
import remarkLintNoReferenceLikeUrl from 'remark-lint-no-reference-like-url' | ||
import remarkParse from 'remark-parse' | ||
import remarkStringify from 'remark-stringify' | ||
import {read} from 'to-vfile' | ||
import {unified} from 'unified' | ||
import {reporter} from 'vfile-reporter' | ||
import {remark} from 'remark' | ||
import remarkLint from 'remark-lint' | ||
import remarkLintNoReferenceLikeUrl from 'remark-lint-no-reference-like-url' | ||
main() | ||
const file = await read('example.md') | ||
async function main() { | ||
const file = await remark() | ||
.use(remarkLint) | ||
.use(remarkLintNoReferenceLikeUrl) | ||
.process(await read('example.md')) | ||
await unified() | ||
.use(remarkParse) | ||
.use(remarkLint) | ||
.use(remarkLintNoReferenceLikeUrl) | ||
.use(remarkStringify) | ||
.process(file) | ||
console.error(reporter(file)) | ||
} | ||
console.error(reporter(file)) | ||
``` | ||
@@ -97,3 +98,3 @@ | ||
```sh | ||
remark --use remark-lint --use remark-lint-no-reference-like-url example.md | ||
remark --frail --use remark-lint --use remark-lint-no-reference-like-url . | ||
``` | ||
@@ -119,18 +120,26 @@ | ||
This package exports no identifiers. | ||
The default export is `remarkLintNoReferenceLikeUrl`. | ||
It exports no additional [TypeScript][typescript] types. | ||
The default export is | ||
[`remarkLintNoReferenceLikeUrl`][api-remark-lint-no-reference-like-url]. | ||
### `unified().use(remarkLintNoReferenceLikeUrl[, config])` | ||
### `unified().use(remarkLintNoReferenceLikeUrl)` | ||
This rule supports standard configuration that all remark lint rules accept | ||
(such as `false` to turn it off or `[1, options]` to configure it). | ||
Warn when URLs are also defined identifiers. | ||
###### Parameters | ||
There are no options. | ||
###### Returns | ||
Transform ([`Transformer` from `unified`][github-unified-transformer]). | ||
## Recommendation | ||
While full URLs for definition identifiers are okay | ||
(`[https://example.com]: https://example.com`), and what looks like an | ||
identifier could be an actual URL (`[text](alpha)`), the more common case | ||
is that, assuming a definition `[alpha]: https://example.com`, then a link | ||
of (`[text](alpha)`) should instead’ve been `[text][alpha]`. | ||
(`[https://example.com]: https://example.com`), | ||
and what looks like an identifier could be an actual URL (`[text](alpha)`), | ||
the more common case is that, | ||
assuming a definition `[alpha]: https://example.com`, | ||
then a link `[text](alpha)` should instead have been `[text][alpha]`. | ||
@@ -144,5 +153,5 @@ ## Examples | ||
```markdown | ||
[Alpha](http://example.com). | ||
[**Mercury**][mercury] is the first planet from the sun. | ||
[bravo]: https://example.com | ||
[mercury]: https://example.com/mercury/ | ||
``` | ||
@@ -159,5 +168,5 @@ | ||
```markdown | ||
[Charlie](delta). | ||
[**Mercury**](mercury) is the first planet from the sun. | ||
[delta]: https://example.com | ||
[mercury]: https://example.com/mercury/ | ||
``` | ||
@@ -168,19 +177,39 @@ | ||
```text | ||
1:1-1:17: Did you mean to use `[delta]` instead of `(delta)`, a reference? | ||
1:1-1:23: Unexpected resource link (`[text](url)`) with URL that matches a definition identifier (as `mercury`), expected reference (`[text][id]`) | ||
``` | ||
##### `image.md` | ||
###### In | ||
```markdown | ||
. | ||
[mercury]: https://example.com/mercury.jpg | ||
``` | ||
###### Out | ||
```text | ||
1:1-1:36: Unexpected resource image (``) with URL that matches a definition identifier (as `mercury`), expected reference (`![text][id]`) | ||
``` | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with all maintained | ||
Projects maintained by the unified collective are compatible with maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 12.20+, 14.14+, and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
When we cut a new major release, we drop support for unmaintained versions of | ||
Node. | ||
This means we try to keep the current release line, | ||
`remark-lint-no-reference-like-url@4`, | ||
compatible with Node.js 16. | ||
## Contribute | ||
See [`contributing.md`][contributing] in [`remarkjs/.github`][health] for ways | ||
See [`contributing.md`][github-dotfiles-contributing] in [`remarkjs/.github`][github-dotfiles-health] for ways | ||
to get started. | ||
See [`support.md`][support] for ways to get help. | ||
See [`support.md`][github-dotfiles-support] for ways to get help. | ||
This project has a [code of conduct][coc]. | ||
This project has a [code of conduct][github-dotfiles-coc]. | ||
By interacting with this repository, organization, or community you agree to | ||
@@ -191,52 +220,54 @@ abide by its terms. | ||
[MIT][license] © [Titus Wormer][author] | ||
[MIT][file-license] © [Titus Wormer][author] | ||
[build-badge]: https://github.com/remarkjs/remark-lint/workflows/main/badge.svg | ||
[api-remark-lint-no-reference-like-url]: #unifieduseremarklintnoreferencelikeurl | ||
[build]: https://github.com/remarkjs/remark-lint/actions | ||
[author]: https://wooorm.com | ||
[coverage-badge]: https://img.shields.io/codecov/c/github/remarkjs/remark-lint.svg | ||
[badge-build-image]: https://github.com/remarkjs/remark-lint/workflows/main/badge.svg | ||
[coverage]: https://codecov.io/github/remarkjs/remark-lint | ||
[badge-build-url]: https://github.com/remarkjs/remark-lint/actions | ||
[downloads-badge]: https://img.shields.io/npm/dm/remark-lint-no-reference-like-url.svg | ||
[badge-chat-image]: https://img.shields.io/badge/chat-discussions-success.svg | ||
[downloads]: https://www.npmjs.com/package/remark-lint-no-reference-like-url | ||
[badge-chat-url]: https://github.com/remarkjs/remark/discussions | ||
[size-badge]: https://img.shields.io/bundlephobia/minzip/remark-lint-no-reference-like-url.svg | ||
[badge-coverage-image]: https://img.shields.io/codecov/c/github/remarkjs/remark-lint.svg | ||
[size]: https://bundlephobia.com/result?p=remark-lint-no-reference-like-url | ||
[badge-coverage-url]: https://codecov.io/github/remarkjs/remark-lint | ||
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg | ||
[badge-downloads-image]: https://img.shields.io/npm/dm/remark-lint-no-reference-like-url.svg | ||
[backers-badge]: https://opencollective.com/unified/backers/badge.svg | ||
[badge-downloads-url]: https://www.npmjs.com/package/remark-lint-no-reference-like-url | ||
[collective]: https://opencollective.com/unified | ||
[badge-funding-backers-image]: https://opencollective.com/unified/backers/badge.svg | ||
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg | ||
[badge-funding-sponsors-image]: https://opencollective.com/unified/sponsors/badge.svg | ||
[chat]: https://github.com/remarkjs/remark/discussions | ||
[badge-funding-url]: https://opencollective.com/unified | ||
[unified]: https://github.com/unifiedjs/unified | ||
[badge-size-image]: https://img.shields.io/bundlejs/size/remark-lint-no-reference-like-url | ||
[remark]: https://github.com/remarkjs/remark | ||
[badge-size-url]: https://bundlejs.com/?q=remark-lint-no-reference-like-url | ||
[mono]: https://github.com/remarkjs/remark-lint | ||
[esm-sh]: https://esm.sh | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[file-license]: https://github.com/remarkjs/remark-lint/blob/main/license | ||
[esmsh]: https://esm.sh | ||
[github-dotfiles-coc]: https://github.com/remarkjs/.github/blob/main/code-of-conduct.md | ||
[npm]: https://docs.npmjs.com/cli/install | ||
[github-dotfiles-contributing]: https://github.com/remarkjs/.github/blob/main/contributing.md | ||
[health]: https://github.com/remarkjs/.github | ||
[github-dotfiles-health]: https://github.com/remarkjs/.github | ||
[contributing]: https://github.com/remarkjs/.github/blob/main/contributing.md | ||
[github-dotfiles-support]: https://github.com/remarkjs/.github/blob/main/support.md | ||
[support]: https://github.com/remarkjs/.github/blob/main/support.md | ||
[github-gist-esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[coc]: https://github.com/remarkjs/.github/blob/main/code-of-conduct.md | ||
[github-remark-lint]: https://github.com/remarkjs/remark-lint | ||
[license]: https://github.com/remarkjs/remark-lint/blob/main/license | ||
[github-unified-transformer]: https://github.com/unifiedjs/unified#transformer | ||
[author]: https://wooorm.com | ||
[npm-install]: https://docs.npmjs.com/cli/install | ||
[typescript]: https://www.typescriptlang.org |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
13136
40.21%5
25%147
65.17%265
13.25%6
20%2
100%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated