This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
lint.js
81 lines (66 loc) · 2.07 KB
/
lint.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
var markdownlint = require("markdownlint");
var glob = require("glob");
var fs = require("fs");
var markdownConfig = require("./.markdownlint.json");
var inputFiles = glob.sync("**/*.md", { ignore: "node_modules/**/*" });
var options = {
files: inputFiles,
config: markdownConfig,
};
var result = markdownlint.sync(options);
console.log(result.toString());
var exitCode = 0;
Object.keys(result).forEach(function (file) {
var fileResults = result[file];
Object.keys(fileResults).forEach(function (rule) {
var ruleResults = fileResults[rule];
exitCode += ruleResults.length;
});
});
inputFiles.forEach(function(fileName) {
var text = fs.readFileSync(fileName, "utf8")
exitCode += checkForImproperlyIndentedFencedCodeBlocks(fileName, text);
});
process.exit(exitCode);
/**
* @param {string} fileName
* @param {string} text
*/
function checkForImproperlyIndentedFencedCodeBlocks(fileName, text) {
var lines = text.split(/\r?\n/g);
var numErrors = 0;
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
var codeBlockMatch = line.match(/^(\s*)```\S+/);
if (codeBlockMatch) {
var startingColumn = codeBlockMatch[1].length;
if (startingColumn === 0 || startingColumn === getCorrectStartingColumnForLine(lines, i)) {
continue;
}
numErrors++;
console.log(fileName + ": " +
i + 1 + ": A fenced code block following a list item must be indented to the first non-whitespace character of the list item.")
}
}
return numErrors;
}
/**
* @param {string[]} line
* @param {number} lineIndex
*/
function getCorrectStartingColumnForLine(lines, lineIndex) {
for (var i = lineIndex - 1; i >= 0; i--) {
var line = lines[i];
if (line.length === 0) {
continue;
}
var m;
if (m = line.match(/^\s*([\*\-]|(\d+\.))\s*/)) {
return m[0].length;
}
if (m = line.match(/^(\s*)/)) {
return m[0].length;
}
}
return 0;
}