-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
find.js
305 lines (246 loc) · 7.37 KB
/
find.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
'use strict'
var fs = require('fs')
var path = require('path')
var URL = require('url').URL // Node 8 support
var vfile = require('to-vfile')
var visit = require('unist-util-visit')
var toString = require('mdast-util-to-string')
var xtend = require('xtend')
var slugs = require('github-slugger')()
var constants = require('../constants')
module.exports = find
var slash = '/'
var numberSign = '#'
var questionMark = '?'
var https = 'https:'
var http = 'http:'
var slashes = '//'
var lineExpression = /^#l\d/i
// List from: https://github.com/github/markup#markups
var readmeExtensions = ['.markdown', '.mdown', '.mkdn', '.md']
var readmeBasename = /^readme$/i
function find(ctx, next) {
var file = ctx.file
var fileSet = ctx.fileSet
var absolute = file.path ? path.resolve(file.cwd, file.path) : ''
var space = file.data
var references = {}
var landmarks = {}
var actual = 0
var expected = 0
var statted = []
var added = []
var config = xtend(ctx.urlConfig, {
path: absolute,
base: absolute ? path.dirname(absolute) : file.cwd,
root: ctx.root
})
space[constants.referenceId] = references
space[constants.landmarkId] = landmarks
addLandmarks(absolute, '')
slugs.reset()
visit(ctx.tree, mark)
if (expected === 0) {
next()
}
function mark(node) {
var data = node.data || {}
var props = data.hProperties || {}
var id = props.name || props.id || data.id
var info = node.url ? urlToPath(node.url, config, node.type) : null
var fp
var hash
if (!id && node.type === 'heading') {
id = slugs.slug(toString(node))
}
if (id) {
addLandmarks(absolute, id)
}
if (info) {
fp = info.filePath
hash = info.hash
addReference(fp, '', node)
if (hash) {
if (fileSet || fp === absolute) {
addReference(fp, hash, node)
}
if (fileSet && fp && statted.indexOf(fp) === -1) {
addFile(fp)
}
}
}
}
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)
}
}
function addLandmark(filePath, hash) {
var marks = landmarks[filePath] || (landmarks[filePath] = {})
marks[hash] = true
}
function addReference(filePath, hash, node) {
var refs = references[filePath] || (references[filePath] = {})
var hashes = refs[hash] || (refs[hash] = [])
hashes.push(node)
}
function addFile(fileOrFolderPath) {
expected++
statted.push(fileOrFolderPath)
fs.stat(fileOrFolderPath, onstat)
function onstat(_, stats) {
if (stats && stats.isDirectory()) {
fs.readdir(fileOrFolderPath, onreaddir)
} else {
done(fileOrFolderPath)
}
}
function onreaddir(_, entries) {
/* istanbul ignore next - unlikely that it is an unreadable directory. */
var files = (entries || []).sort()
var length = files.length
var index = -1
var entry
var file
var filePath
while (++index < length) {
entry = entries[index]
if (readme(entry)) {
file = entry
break
}
}
// If there is no readme that we can parse, add the directory.
filePath = fileOrFolderPath
// To do: test for no readme in directory.
if (file) {
filePath = path.join(fileOrFolderPath, file)
statted.push(filePath)
}
done(filePath)
}
function done(filePath) {
if (added.indexOf(filePath) === -1) {
added.push(filePath)
fileSet.add(
vfile({cwd: file.cwd, path: path.relative(file.cwd, filePath)})
)
}
actual++
if (actual === expected) {
next()
}
}
}
}
function urlToPath(value, config, type) {
var url
var questionMarkIndex
var numberSignIndex
// Absolute paths: `/wooorm/test/blob/main/directory/example.md`.
if (value.charAt(0) === slash) {
if (!config.hostname) {
return
}
// Create a URL.
value = https + slashes + config.hostname + value
}
try {
url = new URL(value)
} catch (_) {}
// URLs: `https://github.com/wooorm/test/blob/main/directory/example.md`.
if (url) {
// Exit if we don’t have hosted Git info or this is not a URL to the repo.
if (
!config.prefix ||
!config.hostname ||
(url.protocol !== https && url.protocol !== http) ||
url.hostname !== config.hostname ||
url.pathname.slice(0, config.prefix.length) !== config.prefix
) {
return
}
value = url.pathname.slice(config.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?`.
numberSignIndex = value.indexOf(numberSign)
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)
}
function normalize(url, config) {
var numberSignIndex = url.indexOf(numberSign)
var lines = config.lines
var prefix = config.headingPrefix
var topAnchor = config.topAnchor
var filePath
var 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 (
(lines && lineExpression.test(hash)) ||
hash.slice(0, prefix.length) !== prefix
) {
hash = undefined
}
// Use the hash if it starts with a heading prefix.
else {
hash = hash.slice(prefix.length)
}
}
return {filePath: decodeURIComponent(filePath), hash: hash}
}
function readme(filePath) {
var ext = path.extname(filePath)
return (
readmeExtensions.indexOf(ext) !== -1 &&
readmeBasename.test(path.basename(filePath, ext))
)
}