-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlint-markdown.js
More file actions
170 lines (148 loc) · 6.19 KB
/
lint-markdown.js
File metadata and controls
170 lines (148 loc) · 6.19 KB
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
const fs = require('fs');
const path = require('path');
const process = require('process');
const textlintPluginMarkdown = require('@textlint/textlint-plugin-markdown').default;
const textLintFilterRuleComments = require('textlint-filter-rule-comments');
const textLintRuleCommonMisspellings = require('textlint-rule-common-misspellings').default;
const textLintRuleDoubledSpaces = require('textlint-rule-doubled-spaces').default;
const textLintRuleMaxComma = require('textlint-rule-max-comma').default;
const textLintRuleNoEmptySection = require('textlint-rule-no-empty-section');
const textLintRuleNoTodo = require('textlint-rule-no-todo').default;
const textLintRuleNoZeroWidthSpaces = require('textlint-rule-no-zero-width-spaces').default;
const findRecursively = require('./find-recursively');
const { handleErrorObject, handleWarningObject } = require('./handle-error');
const markdownlintConfig = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '..', 'config', 'lombiq.markdownlint.json'), 'utf-8'));
const textLintConfig = {
exclude: [
// License files are full of legalese, which can't and shouldn't be analyzed with tools made for normal prose.
'License.md',
],
rules: [
'common-misspellings',
'max-comma',
'no-empty-section',
'no-todo',
'no-zero-width-spaces',
// 'no-start-duplicated-conjunction', // TODO: enable together with fix for HL/docs/Extensions.md
],
filterRules: [
'comments',
],
};
const textLintRules = {
'textlint-filter-rule-comments': textLintFilterRuleComments,
'textlint-rule-common-misspellings': textLintRuleCommonMisspellings,
'textlint-rule-doubled-spaces': textLintRuleDoubledSpaces,
'textlint-rule-max-comma': textLintRuleMaxComma,
'textlint-rule-no-empty-section': textLintRuleNoEmptySection,
'textlint-rule-no-todo': textLintRuleNoTodo,
'textlint-rule-no-zero-width-spaces': textLintRuleNoZeroWidthSpaces,
};
if (process.platform !== 'win32') {
// The doubled-spaces rule generates a lot of false positives on Windows. False negatives are avoided by linting
// on Linux too.
textLintConfig.rules.push('doubled-spaces');
}
function getMarkdownPaths() {
const rootDirectory = process.argv.length > 2 ? process.argv[2] : '.';
return findRecursively(
rootDirectory,
[/\.md$/i],
[/^node_modules$/, /^\.git$/, /^\.vs$/, /^\.vscode$/, /^\.idea$/, /^obj$/, /^bin$/, /^wwwroot$/, /^ThirdParty$/]); // codespell:ignore
}
function handleError(error) {
handleErrorObject(error);
process.exit(1);
}
/**
* Lints the provided files with markdownlint.
* @param files {string[]} The paths of the Markdown files.
*/
async function useMarkdownLint(files) {
const { lint } = await import('markdownlint/promise');
const results = await lint({ files: files, config: markdownlintConfig });
Object.keys(results).forEach((fileName) => {
results[fileName].forEach((warning) => {
const column = (Array.isArray(warning.errorRange) && !Number.isNaN(warning.errorRange[0]))
? warning.errorRange[0]
: 1;
const [code, name] = Array.isArray(warning.ruleNames)
? warning.ruleNames
: ['WARN', 'unknown-warning'];
// License files don't need a title.
if (code === 'MD041' && fileName.toLowerCase().endsWith('license.md')) return;
let message = `${name || code}: ${warning.ruleDescription.trim()}`;
if (!message.endsWith('.')) message += '.';
if (warning.fixInfo) message += ' An automatic fix is available with markdownlint-cli.';
if (warning.ruleInformation) message += ` Rule information: ${warning.ruleInformation}`;
handleWarningObject({
path: fileName,
line: warning.lineNumber,
column: column,
code: code,
message: message,
});
});
});
}
/**
* Processes the provided textlint configuration into a format the low level kernel can understand.
*/
function newTextlintKernelOptions(config) {
return {
...config,
plugins: [
{
pluginId: 'markdown',
plugin: textlintPluginMarkdown,
},
],
rules: config.rules.map((id) => ({ ruleId: id, rule: textLintRules['textlint-rule-' + id] })),
filterRules: config.filterRules.map((id) => ({ ruleId: id, rule: textLintRules['textlint-filter-rule-' + id] })),
};
}
/**
* Lints the provided files with textlint.
* @param files {string[]} The paths of the Markdown files.
*/
async function useTextLint(files) {
const { TextlintKernel } = await import('@textlint/kernel');
const kernel = new TextlintKernel();
const options = newTextlintKernelOptions(textLintConfig);
const excludeLowerCase = Array.isArray(textLintConfig.exclude)
? textLintConfig.exclude.map((name) => name.toLowerCase())
: [];
const targetFiles = files
.filter((file) => {
const fileLower = file.toLowerCase();
return !excludeLowerCase.some((exclude) => fileLower.includes(exclude));
})
.map((file) => fs.promises.readFile(file, 'utf-8')
.then((fileContent) => kernel.lintText(fileContent, { ...options, filePath: file, ext: '.md' }))
.then((result) => ({ file: file, messages: result.messages })));
(await Promise.all(targetFiles))
.forEach((result) => {
const { file, messages } = result;
messages
.filter((message) => message.severity > 0)
.forEach((message) => {
const start = message.loc.start;
handleWarningObject({
path: file,
line: start.line,
column: start.column,
code: message.ruleId,
message: message.message,
});
});
});
}
try {
const files = getMarkdownPaths();
const tasks = [useMarkdownLint, useTextLint];
Promise.all(tasks.map((task) => task(files).catch(handleError)));
}
catch (error) {
handleError(error);
}