/
index.js
74 lines (69 loc) · 2.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @author Titus Wormer
* @copyright 2015 Titus Wormer
* @license MIT
* @module no-auto-link-without-protocol
* @fileoverview
* Warn for autolinks without protocol.
* Autolinks are URLs enclosed in `<` (less than) and `>` (greater than)
* characters.
*
* ## Fix
*
* [`remark-stringify`](https://github.com/remarkjs/remark/tree/HEAD/packages/remark-stringify)
* adds a protocol where needed.
*
* See [Using remark to fix your Markdown](https://github.com/remarkjs/remark-lint#using-remark-to-fix-your-markdown)
* on how to automatically fix warnings for this rule.
*
* @example
* {"name": "ok.md"}
*
* <http://www.example.com>
* <mailto:foo@bar.com>
*
* Most Markdown vendors don’t recognize the following as a link:
* <www.example.com>
*
* @example
* {"name": "not-ok.md", "label": "input"}
*
* <foo@bar.com>
*
* @example
* {"name": "not-ok.md", "label": "output"}
*
* 1:1-1:14: All automatic links must start with a protocol
*/
/**
* @typedef {import('mdast').Root} Root
*/
import {lintRule} from 'unified-lint-rule'
import {visit} from 'unist-util-visit'
import {pointStart, pointEnd} from 'unist-util-position'
import {generated} from 'unist-util-generated'
import {toString} from 'mdast-util-to-string'
// Protocol expression.
// See: <https://en.wikipedia.org/wiki/URI_scheme#Generic_syntax>.
const protocol = /^[a-z][a-z+.-]+:\/?/i
const remarkLintNoAutoLinkWithoutProtocol = lintRule(
{
origin: 'remark-lint:no-auto-link-without-protocol',
url: 'https://github.com/remarkjs/remark-lint/tree/main/packages/remark-lint-no-auto-link-without-protocol#readme'
},
/** @type {import('unified-lint-rule').Rule<Root, void>} */
(tree, file) => {
visit(tree, 'link', (node) => {
if (
!generated(node) &&
pointStart(node).column === pointStart(node.children[0]).column - 1 &&
pointEnd(node).column ===
pointEnd(node.children[node.children.length - 1]).column + 1 &&
!protocol.test(toString(node))
) {
file.message('All automatic links must start with a protocol', node)
}
})
}
)
export default remarkLintNoAutoLinkWithoutProtocol