-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathlint.js
169 lines (136 loc) Β· 4.19 KB
/
lint.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
import { readFileSync } from 'fs'
import { parse, join, resolve, relative, dirname } from 'path'
import walkSync from 'walk-sync'
import { unified } from 'unified'
import remarkParse from 'remark-parse'
import remarkMdx from 'remark-mdx'
import remarkRehype from 'remark-rehype'
import { visit } from 'unist-util-visit'
import { toString } from 'hast-util-to-string'
import Slugger from 'github-slugger'
import { closest } from 'fastest-levenshtein'
const pathPrefix = '/cadence/'
const urlPrefix = 'https://docs.onflow.org/'
function findPaths() {
return walkSync('.', {
directories: false,
globs: ['**/*.md', '**/*.mdx'],
ignore: ['**/node_modules']
})
}
const processor = unified()
.use(remarkParse)
.use(remarkMdx)
.use(remarkRehype)
function parseDocument(path) {
const contents = readFileSync(path).toString()
try {
return processor.runSync(processor.parse(contents))
} catch (e) {
console.error(`failed to parse ${path}`)
throw e
}
}
const slugger = new Slugger()
let warningCount = 0
const targets = {}
function basename(path) {
const {name, dir} = parse(path)
if (name === 'index') {
return dir
}
return join(dir, name)
}
function index(document, path) {
slugger.reset()
visit(document, 'element', (node) => {
if (!['h1', 'h2', 'h3'].includes(node.tagName)) {
return
}
let id = node.properties.id
if (!id) {
id = slugger.slug(toString(node))
}
const target = basename(path)
let ids = targets[target]
if (!ids) {
ids = new Set()
targets[target] = ids
}
// TODO: check if exists
ids.add(id.toLowerCase())
})
}
function check(document, path) {
function warn(node, message, suggestion) {
const { href } = node.properties
const { line, column } = node.position.start
suggestion = suggestion ? `. ${suggestion}` : ''
console.warn(`${path}:${line}:${column}: ${message}: ${href}${suggestion}`)
warningCount++
}
const checkedTarget = basename(path)
visit(document, 'element', (node) => {
if (node.tagName !== 'a') {
return
}
let { href } = node.properties
if (href.startsWith(urlPrefix)) {
warn(node, "link includes domain")
return
}
if (href.startsWith('http:')) {
warn(node, "link with insecure HTTP URL")
return
}
if (href.startsWith('https:')) {
return
}
let [linkedTarget, linkedID] = href.split('#')
if (linkedTarget === '') {
linkedTarget = checkedTarget
} else {
if (linkedTarget.match(/^\.\.?/)) {
warn(node, "relative link")
return
}
if (linkedTarget.match(/\.mdx?$/)) {
warn(node, "link with extension")
return
}
linkedTarget = linkedTarget.replace(/\/$/, "")
if (linkedTarget.startsWith(pathPrefix)) {
linkedTarget = linkedTarget.substr(pathPrefix.length)
} else {
if (linkedTarget.startsWith('/language')) {
warn(node, "suspicious absolute link")
return
}
if (linkedTarget.startsWith('/')) {
return
}
linkedTarget = relative('.', resolve(dirname(path), linkedTarget))
}
}
const ids = targets[linkedTarget]
if (!ids) {
warn(node, "unknown link target")
return
}
if (linkedID && !ids.has(linkedID.toLowerCase())) {
let suggestion = ''
if (ids.size > 0) {
suggestion = `did you mean #${closest(linkedID, Array.from(ids))}?`
}
warn(node, "unknown link target ID", suggestion)
return
}
})
}
const paths = findPaths()
const documents = new Map(paths.map(path =>
[path, parseDocument(path)]
))
documents.forEach(index)
documents.forEach(check)
process.exit(warningCount === 0 ? 0 : 1)