-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmarkdownlint.js
137 lines (104 loc) · 3.13 KB
/
markdownlint.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
const markdownlint = require("markdownlint");
const glob = require("glob");
const fs = require('fs');
const YAML = require('yaml');
const ruleURLs = {
'hc': 'https://github.com/arduino/help-center-content/blob/main/_linter/markdownlint/Rules.md',
'md': 'https://github.com/DavidAnson/markdownlint/blob/main/doc/Rules.md'
};
// Get markdown file paths
let files = glob.sync('content/**/*.md');
// Get custom rules
const customRules = require('./markdownlint/rules/rules.js');
// get config
const configJSON = fs.readFileSync('./_linter/markdownlint.yml', 'utf8')
const configYAML = YAML.parse(configJSON);
const options = {
"config": configYAML,
"files": files,
"customRules": customRules
};
console.log('🕵️ Validating ' + files.length + ' articles...');
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
function pad(num, size) {
num = num.toString();
while (num.length < size) num = "0" + num;
return num;
}
function assign(obj, keyPath, value) {
lastKeyIndex = keyPath.length-1;
for (var i = 0; i < lastKeyIndex; ++ i) {
key = keyPath[i];
if (!(key in obj)){
obj[key] = {}
}
obj = obj[key];
}
obj[keyPath[lastKeyIndex]] = value;
}
function getErrorMessage(error) {
errorMessage = ' ' + error.lineNumber + ': '
+ error.ruleNames[0] + ': ' + error.ruleDescription;
if (error.errorContext !== null) {
errorMessage += ' [' + error.errorContext + ']';
}
return errorMessage;
}
markdownlint(options, function callback(err, result) {
if (!err) {
var violatedRules = new Set();
var errorCount = 0;
var errorArticleCount = 0;
var errorLines = "";
var resultObject = {};
for (var file in result) {
var errors = result[file];
if(errors.length > 0 ) {
// Sort by line number
errors.sort(function(a, b) {
return a.lineNumber - b.lineNumber;
});
let fileSplit = file.split('/');
var errorSet = {};
errorLines += file + '\n';
for (var i = 0; i < errors.length; i++) {
var error = errors[i];
errorLines += getErrorMessage(error) + '\n';
// make rule violation row
const anchor = error.ruleNames[0].toLowerCase();
const URL = ruleURLs[anchor.substring(0,2)];
violatedRules.add(
error.ruleNames[0] + ': ' + URL + '#' + anchor
);
errorCount++;
}
// Assign errors
assign(resultObject, fileSplit, errorSet);
errorArticleCount++;
}
}
const errorMsg = errorCount + ' errors found in '
+ errorArticleCount + ' articles';
if (errorCount === 0) {
console.log('\x1b[42m' + errorMsg + '\x1b[0m');
process.exit(0);
} else {
console.log();
console.log('🚫 ' + '\x1b[31m\x1b[4m' + errorMsg + '\x1b[0m');
console.log(errorLines);
if (violatedRules.size > 0) {
console.log();
const msg1 = '📝 The following rules were violated:';
console.log(msg1);
for (var rule of violatedRules) {
console.log('\x1b[36m%s\x1b[0m', rule);
}
}
process.exit(-1);
}
} else {
throw err;
}
});