-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
find-references.js
349 lines (295 loc) · 9.26 KB
/
find-references.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('hast').Properties} Properties
* @typedef {import('vfile').VFile} VFile
* @typedef {import('unified-engine').FileSet} FileSet
* @typedef {import('../types.js').Landmarks} Landmarks
* @typedef {import('../types.js').References} References
* @typedef {import('../types.js').ReferenceMap} ReferenceMap
* @typedef {import('../types.js').Resource} Resource
* @typedef {import('../index.js').UrlConfig} UrlConfig
* @typedef {import('../index.js').Options} Options
*/
import {promises as fs} from 'node:fs'
import path from 'node:path'
import {URL} from 'node:url'
import {toVFile} from 'to-vfile'
import {visit} from 'unist-util-visit'
import {toString} from 'mdast-util-to-string'
import BananaSlug from 'github-slugger'
import {constants} from '../constants.js'
const slugs = new BananaSlug()
const slash = '/'
const numberSign = '#'
const questionMark = '?'
const https = 'https:'
const http = 'http:'
const slashes = '//'
const lineExpression = /^#l\d/i
// List from: https://github.com/github/markup#markups
const readmeExtensions = new Set(['.markdown', '.mdown', '.mkdn', '.md'])
const readmeBasename = /^readme$/i
/**
* @param {{tree: Root, file: VFile, fileSet?: FileSet, options: Options}} ctx
*/
export async function findReferences(ctx) {
const file = ctx.file
const fileSet = ctx.fileSet
const absolute = file.path ? path.resolve(file.cwd, file.path) : ''
const space = file.data
/** @type {Record<string, Record<string, Resource[]>>} */
const references = Object.create(null)
/** @type {Landmarks} */
const landmarks = Object.create(null)
const config = {
// Always set at this point.
/* c8 ignore next */
urlConfig: ctx.options.urlConfig || {},
path: absolute,
base: absolute ? path.dirname(absolute) : file.cwd,
root: ctx.options.root
}
/** @type {string[]} */
const statted = []
/** @type {string[]} */
const added = []
/** @type {Array.<Promise<void>>} */
const promises = []
space[constants.referenceId] = references
space[constants.landmarkId] = landmarks
addLandmarks(absolute, '')
slugs.reset()
visit(ctx.tree, (node) => {
const data = node.data || {}
const props = /** @type {Properties} */ (data.hProperties || {})
let id = String(props.name || props.id || data.id || '')
if (!id && node.type === 'heading') {
id = slugs.slug(
toString(node, {includeImageAlt: false, includeHtml: false})
)
}
if (id) {
addLandmarks(absolute, id)
}
if ('url' in node && node.url) {
const info = urlToPath(node.url, config, node.type)
if (info) {
const fp = info.filePath
const hash = info.hash
addReference(fp, '', node)
if (hash) {
if (fileSet || fp === absolute) {
addReference(fp, hash, node)
}
if (fileSet && fp && !statted.includes(fp)) {
promises.push(addFile(fp))
}
}
}
}
})
await Promise.all(promises)
/**
* @param {string} filePath
* @param {string} hash
*/
function addLandmarks(filePath, hash) {
addLandmark(filePath, hash)
// Note: this may add marks too many anchors as defined.
// For example, if there is both a `readme.md` and a `readme.markdown` in a
// folder, both their landmarks will be defined for their parent folder.
// To solve this, we could check whichever sorts first, and ignore the
// others.
// This is an unlikely scenario though, and adds a lot of complexity, so
// we’re ignoring it.
if (readme(filePath)) {
addLandmark(path.dirname(filePath), hash)
}
}
/**
* @param {string} filePath
* @param {string} hash
*/
function addLandmark(filePath, hash) {
/** @type {Record<string, boolean>} */
const marks =
filePath in landmarks
? landmarks[filePath]
: (landmarks[filePath] = Object.create(null))
marks[hash] = true
}
/**
* @param {string} filePath
* @param {string} hash
* @param {Resource} node
*/
function addReference(filePath, hash, node) {
/** @type {Record<string, Resource[]>} */
const refs =
filePath in references
? references[filePath]
: (references[filePath] = Object.create(null))
const hashes = hash in refs ? refs[hash] : (refs[hash] = [])
hashes.push(node)
}
/**
* @param {string} filePath
*/
async function addFile(filePath) {
statted.push(filePath)
try {
const stats = await fs.stat(filePath)
if (stats.isDirectory()) {
/** @type {string[]} */
let entries = []
try {
entries = await fs.readdir(filePath)
// Seems to never happen after a stat.
/* c8 ignore next */
} catch {}
const files = entries.sort()
let index = -1
/** @type {string|undefined} */
let file
while (++index < files.length) {
const entry = entries[index]
if (readme(entry)) {
file = entry
break
}
}
// To do: test for no readme in directory.
// Else, there’s no readme that we can parse, so add the directory.
if (file) {
filePath = path.join(filePath, file)
statted.push(filePath)
}
}
} catch {}
if (fileSet && !added.includes(filePath)) {
added.push(filePath)
fileSet.add(
toVFile({cwd: file.cwd, path: path.relative(file.cwd, filePath)})
)
}
}
}
/**
* @param {string} value
* @param {{urlConfig: UrlConfig, path: string, base: string, root: string|undefined}} config
* @param {string} type
*/
// eslint-disable-next-line complexity
function urlToPath(value, config, type) {
// Absolute paths: `/wooorm/test/blob/main/directory/example.md`.
if (value.charAt(0) === slash) {
if (!config.urlConfig.hostname) {
return
}
// Create a URL.
value = https + slashes + config.urlConfig.hostname + value
}
/** @type {URL|undefined} */
let url
try {
url = new URL(value)
} catch {}
// URLs: `https://github.com/wooorm/test/blob/main/directory/example.md`.
if (url && config.root) {
// Exit if we don’t have hosted Git info or this is not a URL to the repo.
if (
!config.urlConfig.prefix ||
!config.urlConfig.hostname ||
(url.protocol !== https && url.protocol !== http) ||
url.hostname !== config.urlConfig.hostname ||
url.pathname.slice(0, config.urlConfig.prefix.length) !==
config.urlConfig.prefix
) {
return
}
value = url.pathname.slice(config.urlConfig.prefix.length)
// Things get interesting here: branches: `foo/bar/baz` could be `baz` on
// the `foo/bar` branch, or, `baz` in the `bar` directory on the `foo`
// branch.
// Currently, we’re ignoring this and just not supporting branches.
value = value.split(slash).slice(1).join(slash)
return normalize(
path.resolve(config.root, value + (type === 'image' ? '' : url.hash)),
config
)
}
// Remove the search: `?foo=bar`.
// But don’t remove stuff if it’s in the hash: `readme.md#heading?`.
let numberSignIndex = value.indexOf(numberSign)
const questionMarkIndex = value.indexOf(questionMark)
if (
questionMarkIndex !== -1 &&
(numberSignIndex === -1 || numberSignIndex > questionMarkIndex)
) {
value =
value.slice(0, questionMarkIndex) +
(numberSignIndex === -1 ? '' : value.slice(numberSignIndex))
numberSignIndex = value.indexOf(numberSign)
}
// Ignore "headings" in image links: `image.png#metadata`
if (numberSignIndex !== -1 && type === 'image') {
value = value.slice(0, numberSignIndex)
}
// Local: `#heading`.
if (value.charAt(0) === numberSign) {
value = config.path ? config.path + value : value
}
// Anything else, such as `readme.md`.
else {
value = config.path ? path.resolve(config.base, value) : ''
}
return normalize(value, config)
}
/**
* @param {string} url
* @param {{urlConfig: UrlConfig, path: string, base: string, root: string|undefined}} config
*/
function normalize(url, config) {
const numberSignIndex = url.indexOf(numberSign)
const lines = config.urlConfig.lines
const prefix = config.urlConfig.headingPrefix
const topAnchor = config.urlConfig.topAnchor
/** @type {string} */
let filePath
/** @type {string|undefined} */
let hash
if (numberSignIndex === -1) {
filePath = url
} else {
filePath = url.slice(0, numberSignIndex)
hash = url.slice(numberSignIndex).toLowerCase()
// Ignore the hash if it references the top anchor of the environment
if (topAnchor && hash === topAnchor) {
hash = undefined
}
// Ignore the hash if it references lines in a file or doesn’t start
// with a heading prefix.
else if (
prefix &&
((lines && lineExpression.test(hash)) ||
hash.slice(0, prefix.length) !== prefix)
) {
hash = undefined
}
// Use the hash if it starts with a heading prefix.
else if (prefix) {
hash = hash.slice(prefix.length)
}
}
return {filePath: decodeURIComponent(filePath), hash}
}
/**
* @param {string} filePath
*/
function readme(filePath) {
const ext = path.extname(filePath)
return (
readmeExtensions.has(ext) &&
readmeBasename.test(path.basename(filePath, ext))
)
}