/
no-dead-link.js
226 lines (223 loc) · 8.13 KB
/
no-dead-link.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* eslint-disable max-len */
import TextlintTester from "textlint-tester";
import fs from "fs";
import path from "path";
import rule from "../src/no-dead-link";
const tester = new TextlintTester();
tester.run("no-dead-link", rule, {
valid: [
"should ignore non-http url [email address](mailto:mail.example.com) by default",
"should ignore non-http url [ftp](ftp://example.com) by default",
"should ignore non-http url [websockets](ws://example.com) by default",
"should be able to check a link in Markdown: [example](https://example.com/)",
"should be able to check a link in Markdown: [example](https://dev.mysql.com/downloads/mysql/)",
"should be able to check a URL in Markdown: https://example.com/",
"should success with retrying on error: [npm results for textlint](https://www.npmjs.com/search?q=textlint)",
"should treat 200 OK as alive: https://httpstat.us/200",
"should treat 200 OK. It require User-Agent: Navigate to [MySQL distribution](https://dev.mysql.com/downloads/mysql/) to install MySQL `5.7`.",
"should treat 200 OK. It require User-Agent: https://datatracker.ietf.org/doc/html/rfc6749",
{
text: "should be able to check a URL in a plain text: https://example.com/",
ext: ".txt"
},
{
text: "should be able to check multiple URLs in a plain text: https://example.com/, https://httpstat.us/200",
ext: ".txt"
},
{
text: "should be able to check relative pathes when checkRelative is true: ![robot](index.html)",
options: {
baseURI: "https://example.com/"
}
},
{
text: 'should ignore URLs in the "ignore" option: https://example.com/404.html shouldn\'t be checked.',
options: {
ignore: ["https://example.com/404.html"]
}
},
{
text: 'should ignore URLs in the "ignore" option that glob formatted: https://example.com/404.html shouldn\'t be checked.',
options: {
ignore: ["https://example.com/*"]
}
},
{
text: "should ignore relative URIs when `checkRelative` is false: [test](./a.md).",
options: {
checkRelative: false
}
},
{
text: fs.readFileSync(path.join(__dirname, "fixtures/a.md"), "utf-8"),
options: {
baseURI: path.join(__dirname, "fixtures/")
}
},
{
inputPath: path.join(__dirname, "fixtures/a.md"),
options: {
baseURI: path.join(__dirname, "fixtures/")
}
},
{
inputPath: path.join(__dirname, "fixtures/a.md")
},
{
text: "should success with GET method: [npm results for textlint](https://www.npmjs.com/search?q=textlint)",
options: {
preferGET: ["https://www.npmjs.com"]
}
},
{
text: "should success with GET method whether the option is specific URL: [npm results for textlint](https://www.npmjs.com/search?q=textlint)",
options: {
preferGET: ["https://www.npmjs.com/search?q=textlint-rule"]
}
},
{
text: "should not treat https://httpstat.us/301 when `ignoreRedirects` is true",
options: {
ignoreRedirects: true
}
},
{
text: "should preserve hash while ignoring redirect: [BDD](http://mochajs.org/#bdd)",
options: {
ignoreRedirects: true
}
},
// https://github.com/textlint-rule/textlint-rule-no-dead-link/issues/125
{
text: "ignore redirect https://www.consul.io/intro/getting-started/kv.html",
options: {
ignoreRedirects: true
}
},
// https://github.com/textlint-rule/textlint-rule-no-dead-link/issues/128
{
text: "should treat 200 OK. It requires browser-like User-Agent: https://issues.jenkins.io/browse/JENKINS-59261",
options: {
userAgent:
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36"
}
}
],
invalid: [
{
text: "should treat 301 https://httpstat.us/301",
output: "should treat 301 https://httpstat.us/",
errors: [
{
message: "https://httpstat.us/301 is redirected to https://httpstat.us/. (301 Moved Permanently)",
line: 1,
column: 18
}
]
},
{
text: "should treat 301 [link](https://httpstat.us/301)",
output: "should treat 301 [link](https://httpstat.us/)",
errors: [
{
message: "https://httpstat.us/301 is redirected to https://httpstat.us/. (301 Moved Permanently)",
line: 1,
column: 25
}
]
},
{
text: "should treat 302 [link](https://httpstat.us/302)",
output: "should treat 302 [link](https://httpstat.us/)",
errors: [
{
message: "https://httpstat.us/302 is redirected to https://httpstat.us/. (302 Found)",
line: 1,
column: 25
}
]
},
{
text: "should treat 404 Not Found as dead: https://httpstat.us/404",
errors: [
{
message: "https://httpstat.us/404 is dead. (404 Not Found)",
line: 1,
column: 37
}
]
},
{
text: "should treat 500 Internal Server Error as dead: https://httpstat.us/500",
errors: [
{
message: "https://httpstat.us/500 is dead. (500 Internal Server Error)",
line: 1,
column: 49
}
]
},
{
text: "should locate the exact index of a URL in a plain text: https://httpstat.us/404",
ext: ".txt",
errors: [
{
message: "https://httpstat.us/404 is dead. (404 Not Found)",
line: 1,
column: 57
}
]
},
{
text: "should throw when a relative URI cannot be resolved: [test](./a.md).",
errors: [
{
message: "Unable to resolve the relative URI. Please check if the base URI is correctly specified.",
line: 1,
column: 61
}
]
},
{
inputPath: path.join(__dirname, "fixtures/b.md"),
errors: [
{
line: 1,
column: 14
},
{
line: 2,
column: 14
},
{
line: 3,
column: 14
}
]
},
{
text: "should preserve hash while redirecting: [BDD](http://mochajs.org/#bdd)",
output: "should preserve hash while redirecting: [BDD](https://mochajs.org/#bdd)",
errors: [
{
message:
"http://mochajs.org/#bdd is redirected to https://mochajs.org/#bdd. (301 Moved Permanently)",
index: 46,
line: 1,
column: 47
}
]
},
{
text: `Support Reference link[^1] in Markdown.
[^1] https://httpstat.us/404`,
errors: [
{
message: "https://httpstat.us/404 is dead. (404 Not Found)",
line: 3,
column: 6
}
]
}
]
});