-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
178 lines (155 loc) · 4.67 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* @import {Nodes, Resource, Root} from 'mdast'
* @import {Options} from 'remark-lint-no-dead-urls'
* @import {VFile} from 'vfile'
*/
/**
* @typedef {Extract<Nodes, Resource>} Resources
* Resource nodes.
*/
import {deadOrAlive} from 'dead-or-alive'
import {ok as assert} from 'devlop'
import isOnline from 'is-online'
import {lintRule} from 'unified-lint-rule'
import {visit} from 'unist-util-visit'
/** @type {Readonly<Options>} */
const emptyOptions = {}
const defaultSkipUrlPatterns = [/^(?!https?)/i]
const remarkLintNoDeadUrls = lintRule(
{
origin: 'remark-lint:no-dead-urls',
url: 'https://github.com/remarkjs/remark-lint-no-dead-urls'
},
rule
)
export default remarkLintNoDeadUrls
/**
* Warn when URLs are dead.
*
* ###### Notes
*
* To improve performance,
* decrease `maxRetries` in `deadOrAliveOptions` and/or decrease the value used
* for `sleep` in `deadOrAliveOptions`.
* The normal behavior is to assume connections might be flakey and to sleep a
* while and retry a couple times.
*
* If you do not care whether anchors exist and don’t need to support HTML
* redirects,
* you can pass `checkAnchor: false` and `followMetaHttpEquiv: false` in
* `deadOrAliveOptions`,
* which enables a fast path without parsing HTML.
*
* @param {Root} tree
* Tree.
* @param {VFile} file
* File.
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
* @returns {Promise<undefined>}
* Nothing.
*/
async function rule(tree, file, options) {
/** @type {Map<string, Array<Resources>>} */
const nodesByUrl = new Map()
const online = await isOnline()
const settings = options || emptyOptions
const skipUrlPatterns = settings.skipUrlPatterns
? settings.skipUrlPatterns.map(function (d) {
return typeof d === 'string' ? new RegExp(d) : d
})
: [...defaultSkipUrlPatterns]
if (settings.skipLocalhost) {
skipUrlPatterns.push(/^(https?:\/\/)(localhost|127\.0\.0\.1)(:\d+)?/)
}
/* c8 ignore next 9 -- difficult to test */
if (!online) {
if (!settings.skipOffline) {
file.info(
'Unexpected offline connection, expected either an online connection or `skipOffline: true`'
)
}
return
}
const meta = /** @type {Record<string, unknown> | undefined} */ (
file.data.meta
)
const from =
settings.from ||
(meta &&
typeof meta.origin === 'string' &&
typeof meta.pathname === 'string'
? new URL(meta.pathname, meta.origin).href
: undefined)
const deadOrAliveOptions = {
...settings.deadOrAliveOptions,
findUrls: false
}
visit(tree, function (node) {
if ('url' in node && typeof node.url === 'string') {
const value = node.url
const colon = value.indexOf(':')
const questionMark = value.indexOf('?')
const numberSign = value.indexOf('#')
const slash = value.indexOf('/')
let relativeToSomething = false
if (
// If there is no protocol, it’s relative.
colon < 0 ||
// If the first colon is after a `?`, `#`, or `/`, it’s not a protocol.
(slash > -1 && colon > slash) ||
(questionMark > -1 && colon > questionMark) ||
(numberSign > -1 && colon > numberSign)
) {
relativeToSomething = true
}
// We can only check URLs relative to something if `from` is passed.
if (relativeToSomething && !from) {
return
}
const url = new URL(value, from).href
if (
skipUrlPatterns.some(function (skipPattern) {
return skipPattern.test(url)
})
) {
return
}
let list = nodesByUrl.get(url)
if (!list) {
list = []
nodesByUrl.set(url, list)
}
list.push(node)
}
})
const urls = [...nodesByUrl.keys()]
await Promise.all(
urls.map(async function (url) {
const nodes = nodesByUrl.get(url)
assert(nodes)
const result = await deadOrAlive(url, deadOrAliveOptions)
for (const node of nodes) {
for (const message of result.messages) {
const product = file.message(
'Unexpected dead URL `' + url + '`, expected live URL',
{ancestors: [node], cause: message, place: node.position}
)
product.fatal = message.fatal
}
if (result.status === 'alive' && new URL(url).href !== result.url) {
const message = file.message(
'Unexpected redirecting URL `' +
url +
'`, expected final URL `' +
result.url +
'`',
{ancestors: [node], place: node.position}
)
message.actual = url
message.expected = [result.url]
}
}
})
)
}