-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
57 lines (50 loc) · 1.13 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
/**
* @typedef {import('unist').Node} Node
* @typedef {import('vfile').VFile} VFile
*/
import normalizeUrl from 'normalize-url';
import { lintRule } from 'unified-lint-rule';
import { visit } from 'unist-util-visit';
export default lintRule(
'remark-lint:double-link',
processor,
);
/**
* Check for repeated links.
*
* @param {Node} tree
* @param {VFile} file
* @returns {void}
*/
function processor(tree, file) {
const links = new Map();
/**
* @param {any} node
*/
function visitor(node) {
const hashCount = node.url.split("#").length - 1
if (hashCount > 1) {
return
}
const url = node.url.startsWith('#') ? node.url : normalizeUrl(node.url, {
removeDirectoryIndex: [/^index\.[a-z]+$/],
stripHash: true,
stripProtocol: true,
// removeQueryParameters: [/\.*/i]
});
if (links.has(url)) {
links.get(url).push(node)
} else {
links.set(url, [node])
}
}
visit(tree, 'link', visitor)
links.forEach(nodes => {
if (nodes.length > 1) {
// @ts-ignore
nodes.forEach((node) => {
file.message(node.url, node)
})
}
})
}