-
Notifications
You must be signed in to change notification settings - Fork 707
/
Copy pathlint-markdown.js
82 lines (73 loc) · 2.88 KB
/
lint-markdown.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
/**
* Run markdownlint via the API in order to add custom rules for our needs.
*
* Custom rules:
*
* * `validate-reference-links`: Validates that there are not any typos when
* using reference links.
*
*/
const path = require('path');
const markdownlint = require('markdownlint');
const globby = require('globby');
const files = globby.sync(['**/*.md', '!**/CHANGELOG.md', '!**/node_modules/**'], {
cwd: process.cwd(),
gitignore: true
});
const config = markdownlint.readConfigSync(path.join(__dirname, '..', '.markdownlintrc')); // eslint-disable-line no-sync
const result = markdownlint.sync({
config,
customRules: [
{
description: 'Invalid reference link',
function: (params, onError) => {
/**
* markdown-lint automatically transforms founded reference links when
* passing the tokens and removing the markdown code. E.g.:
* * `[something][somewhere]` → Is a child of type `text` with content
* set to `something`
*
* If the reference is not found it will return the following:
* `[something][somewhere]` → Is a child of type `text` with content
* set to `[something][somewhere]`
*
* To know if a reference is found or not, we search all the children
* of type `text` and we check if their content matches the RegExp.
* If it is we know the reference is invalid.
*
*/
/**
* This matches the ending part of a reference link. E.g: `][somewhere]`
* Taking into account new lines, etc.
*/
const refLinkRegExp = /\](\[(.|\s)*?\])/gi;
params.tokens.filter((token) => {
return token.type === 'inline';
}).forEach((token) => {
return token.children.filter((child) => {
return child.type === 'text';
}).forEach((text) => {
const invalidRefLink = refLinkRegExp.exec(text.content);
if (invalidRefLink !== null) {
onError({
// context: invalidRefLink[1],
detail: `Reference: ${invalidRefLink[1]}`,
lineNumber: text.lineNumber
});
}
});
});
},
names: ['valid-reference-links'],
tags: ['links']
}
],
files,
resultVersion: 1
});
const resultString = result.toString();
const returnCode = resultString ? 1 : 0;
if (resultString) {
console.error(resultString);
}
process.exit(returnCode); // eslint-disable-line