-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
107 lines (101 loc) · 3.21 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
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
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('vfile').VFile} VFile
* @typedef {import('unified-engine').FileSet} FileSet
* @typedef {import('trough').Callback} Callback
*
* @typedef UrlConfig
* Hosted Git info
* @property {string|undefined} [hostname]
* Domain of URLs (example: `'github.com'`, `'bitbucket.org'`).
* @property {string|undefined} [prefix]
* Path prefix before files (example:
* `'/remarkjs/remark-validate-links/blob/'`,
* `'/remarkjs/remark-validate-links/src/'`).
* @property {string|undefined} [headingPrefix]
* Prefix of headings (example: `'#'`, `'#markdown-header-'`).
* @property {string|undefined} [topAnchor]
* Hash to top of readme (example: `#readme`).
* @property {boolean|undefined} [lines]
* Whether lines in files can be linked.
*
* @typedef Options
* Configuration.
* @property {string|false} [repository]
* URL to hosted Git.
* If `repository` is nullish, the Git origin remote is detected.
* If the repository resolves to something npm understands as a Git host such
* as GitHub, GitLab, or Bitbucket, full URLs to that host (say,
* `https://github.com/remarkjs/remark-validate-links/readme.md#install`) can
* also be checked.
* If you’re not in a Git repository, you must pass `repository: false`
* explicitly.
* @property {string} [root]
* A `root` (`string?`) can also be passed, referencing the local Git root
* directory (the folder that contains `.git`).
* If both `root` and `repository` are nullish, the Git root is detected.
* If `root` is not given but `repository` is, `file.cwd` is used.
* @property {UrlConfig} [urlConfig]
* If your project is hosted on `github.com`, `gitlab.com`, or `bitbucket.org`,
* this plugin can automatically detect the url configuration.
* Otherwise, use `urlConfig` to specify this manually.
*/
import {check} from './check/index.js'
import {find} from './find/index.js'
import {constants} from './constants.js'
cliCompleter.pluginId = constants.sourceId
/**
* Plugin to validate that Markdown links and images reference existing local
* files and headings.
*
* @type {import('unified').Plugin<[Options?, FileSet?]|[Options?]|void[], Root>}
*/
export default function remarkValidateLinks(options, fileSet) {
// Attach a `completer`.
if (fileSet) {
fileSet.use(cliCompleter)
}
// Find references and landmarks.
return (tree, file, next) => {
find.run(
{tree, file, fileSet, options: {...options}},
/** @type {Callback} */
(error) => {
if (error) {
next(error)
} else if (fileSet) {
next()
} else {
checkAll([file], next)
}
}
)
}
}
/**
* Completer for the CLI (multiple files, supports parsing more files).
*
* @param {FileSet} set
* @param {Callback} next
* @returns {void}
*/
function cliCompleter(set, next) {
checkAll(set.valueOf(), next)
}
/**
* Completer for the CLI (multiple files, supports parsing more files).
*
* @param {VFile[]} files
* @param {Callback} next
* @returns {void}
*/
function checkAll(files, next) {
// Check all references and landmarks.
check.run(
{files},
/** @type {Callback} */
(error) => {
next(error)
}
)
}