-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathmarkdownlint.js
executable file
·328 lines (286 loc) · 10.7 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env node
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const Module = require('node:module');
const os = require('node:os');
const process = require('node:process');
const program = require('commander');
const glob = require('glob');
const markdownlint = require('markdownlint');
const rc = require('run-con');
const minimatch = require('minimatch');
const pkg = require('./package.json');
const options = program.opts();
function posixPath(p) {
return p.split(path.sep).join(path.posix.sep);
}
function jsoncParse(text) {
return JSON.parse(require('jsonc-parser').stripComments(text));
}
function jsYamlSafeLoad(text) {
return require('js-yaml').load(text);
}
const exitCodes = {
lintFindings: 1,
failedToWriteOutputFile: 2,
failedToLoadCustomRules: 3,
unexpectedError: 4
};
const projectConfigFiles = ['.markdownlint.jsonc', '.markdownlint.json', '.markdownlint.yaml', '.markdownlint.yml'];
const configParsers = [jsoncParse, jsYamlSafeLoad];
const fsOptions = {encoding: 'utf8'};
const processCwd = process.cwd();
function readConfiguration(userConfigFile) {
const jsConfigFile = /\.c?js$/i.test(userConfigFile);
// Load from well-known config files
let config = rc('markdownlint', {});
for (const projectConfigFile of projectConfigFiles) {
try {
fs.accessSync(projectConfigFile, fs.R_OK);
const projectConfig = markdownlint.readConfigSync(projectConfigFile, configParsers);
config = {...config, ...projectConfig};
break;
} catch {
// Ignore failure
}
}
// Normally parsing this file is not needed, because it is already parsed by rc package.
// However I have to do it to overwrite configuration from .markdownlint.{json,yaml,yml}.
if (userConfigFile) {
try {
const userConfig = jsConfigFile ? require(path.resolve(processCwd, userConfigFile)) : markdownlint.readConfigSync(userConfigFile, configParsers);
config = require('deep-extend')(config, userConfig);
} catch (error) {
console.error(`Cannot read or parse config file '${userConfigFile}': ${error.message}`);
process.exitCode = exitCodes.unexpectedError;
}
}
return config;
}
function prepareFileList(files, fileExtensions, previousResults) {
const globOptions = {
dot: Boolean(options.dot),
nodir: true
};
let extensionGlobPart = '*.';
if (fileExtensions.length === 1) {
// Glob seems not to match patterns like 'foo.{js}'
extensionGlobPart += fileExtensions[0];
} else {
extensionGlobPart += '{' + fileExtensions.join(',') + '}';
}
files = files.map(file => {
try {
if (fs.lstatSync(file).isDirectory()) {
// Directory (file falls through to below)
if (previousResults) {
const matcher = new minimatch.Minimatch(posixPath(path.resolve(processCwd, path.join(file, '**', extensionGlobPart))), globOptions);
return previousResults.filter(fileInfo => matcher.match(fileInfo.absolute)).map(fileInfo => fileInfo.original);
}
return glob.sync(posixPath(path.join(file, '**', extensionGlobPart)), globOptions);
}
} catch {
// Not a directory, not a file, may be a glob
if (previousResults) {
const matcher = new minimatch.Minimatch(posixPath(path.resolve(processCwd, file)), globOptions);
return previousResults.filter(fileInfo => matcher.match(fileInfo.absolute)).map(fileInfo => fileInfo.original);
}
return glob.sync(file, globOptions);
}
// File
return file;
});
return files.flat().map(file => ({
original: file,
relative: path.relative(processCwd, file),
absolute: path.resolve(file)
}));
}
function printResult(lintResult) {
const results = Object.keys(lintResult).flatMap(file =>
lintResult[file].map(result => {
if (options.json) {
return {
fileName: file,
...result
};
}
return {
file: file,
lineNumber: result.lineNumber,
column: (result.errorRange && result.errorRange[0]) || 0,
names: result.ruleNames.join('/'),
description: result.ruleDescription + (result.errorDetail ? ' [' + result.errorDetail + ']' : '') + (result.errorContext ? ' [Context: "' + result.errorContext + '"]' : '')
};
})
);
let lintResultString = '';
if (results.length > 0) {
if (options.json) {
results.sort((a, b) => a.fileName.localeCompare(b.fileName) || a.lineNumber - b.lineNumber || a.ruleDescription.localeCompare(b.ruleDescription));
lintResultString = JSON.stringify(results, null, 2);
} else {
results.sort((a, b) => a.file.localeCompare(b.file) || a.lineNumber - b.lineNumber || a.names.localeCompare(b.names) || a.description.localeCompare(b.description));
lintResultString = results
.map(result => {
const {file, lineNumber, column, names, description} = result;
const columnText = column ? `:${column}` : '';
return `${file}:${lineNumber}${columnText} ${names} ${description}`;
})
.join('\n');
}
// Note: process.exit(1) will end abruptly, interrupting asynchronous IO
// streams (e.g., when the output is being piped). Just set the exit code
// and let the program terminate normally.
// @see {@link https://nodejs.org/dist/latest-v8.x/docs/api/process.html#process_process_exit_code}
// @see {@link https://github.com/igorshubovych/markdownlint-cli/pull/29#issuecomment-343535291}
process.exitCode = exitCodes.lintFindings;
}
if (options.output) {
lintResultString = lintResultString.length > 0 ? lintResultString + os.EOL : lintResultString;
try {
fs.writeFileSync(options.output, lintResultString);
} catch (error) {
console.warn('Cannot write to output file ' + options.output + ': ' + error.message);
process.exitCode = exitCodes.failedToWriteOutputFile;
}
} else if (lintResultString && !options.quiet) {
console.error(lintResultString);
}
}
function concatArray(item, array) {
array.push(item);
return array;
}
program
.version(pkg.version)
.description(pkg.description)
.usage('[options] <files|directories|globs>')
.option('-c, --config [configFile]', 'configuration file (JSON, JSONC, JS, or YAML)')
.option('-d, --dot', 'include files/folders with a dot (for example `.github`)')
.option('-f, --fix', 'fix basic errors (does not work with STDIN)')
.option('-i, --ignore [file|directory|glob]', 'file(s) to ignore/exclude', concatArray, [])
.option('-j, --json', 'write issues in json format')
.option('-o, --output [outputFile]', 'write issues to file (no console)')
.option('-p, --ignore-path [file]', 'path to file with ignore pattern(s)')
.option('-q, --quiet', 'do not write issues to STDOUT')
.option('-r, --rules [file|directory|glob|package]', 'include custom rule files', concatArray, [])
.option('-s, --stdin', 'read from STDIN (does not work with files)')
.option('--enable [rules...]', 'Enable certain rules, e.g. --enable MD013 MD041 --')
.option('--disable [rules...]', 'Disable certain rules, e.g. --disable MD013 MD041 --');
program.parse(process.argv);
function tryResolvePath(filepath) {
try {
if (path.basename(filepath) === filepath && path.extname(filepath) === '') {
// Looks like a package name, resolve it relative to cwd
// Get list of directories, where requested module can be.
let paths = Module._nodeModulePaths(processCwd);
// eslint-disable-next-line unicorn/prefer-spread
paths = paths.concat(Module.globalPaths);
if (require.resolve.paths) {
// Node >= 8.9.0
return require.resolve(filepath, {paths: paths});
}
return Module._resolveFilename(filepath, {paths: paths});
}
// Maybe it is a path to package installed locally
return require.resolve(path.join(processCwd, filepath));
} catch {
return filepath;
}
}
function loadCustomRules(rules) {
return rules.flatMap(rule => {
try {
const resolvedPath = [tryResolvePath(rule)];
const fileList = prepareFileList(resolvedPath, ['js']).flatMap(filepath => require(filepath.absolute));
if (fileList.length === 0) {
throw new Error('No such rule');
}
return fileList;
} catch (error) {
console.error('Cannot load custom rule ' + rule + ': ' + error.message);
return process.exit(exitCodes.failedToLoadCustomRules);
}
});
}
let ignorePath = '.markdownlintignore';
let {existsSync} = fs;
if (options.ignorePath) {
ignorePath = options.ignorePath;
existsSync = () => true;
}
let ignoreFilter = () => true;
if (existsSync(ignorePath)) {
const ignoreText = fs.readFileSync(ignorePath, fsOptions);
const ignore = require('ignore');
const ignoreInstance = ignore().add(ignoreText);
ignoreFilter = fileInfo => !ignoreInstance.ignores(fileInfo.relative);
}
const files = prepareFileList(program.args, ['md', 'markdown']).filter(value => ignoreFilter(value));
const ignores = prepareFileList(options.ignore, ['md', 'markdown'], files);
const customRules = loadCustomRules(options.rules);
const diff = files.filter(file => !ignores.some(ignore => ignore.absolute === file.absolute)).map(paths => paths.original);
function lintAndPrint(stdin, files) {
files = files || [];
const config = readConfiguration(options.config);
for (const rule of options.enable || []) {
// Leave default values in place if rule is an object
if (!config[rule]) {
config[rule] = true;
}
}
for (const rule of options.disable || []) {
config[rule] = false;
}
const lintOptions = {
config,
configParsers,
customRules,
files
};
if (stdin) {
lintOptions.strings = {
stdin
};
}
if (options.json) {
lintOptions.resultVersion = 3;
}
if (options.fix) {
const fixOptions = {
...lintOptions,
resultVersion: 3
};
const markdownlintRuleHelpers = require('markdownlint/helpers');
for (const file of files) {
fixOptions.files = [file];
const fixResult = markdownlint.sync(fixOptions);
const fixes = fixResult[file].filter(error => error.fixInfo);
if (fixes.length > 0) {
const originalText = fs.readFileSync(file, fsOptions);
const fixedText = markdownlintRuleHelpers.applyFixes(originalText, fixes);
if (originalText !== fixedText) {
fs.writeFileSync(file, fixedText, fsOptions);
}
}
}
}
const lintResult = markdownlint.sync(lintOptions);
printResult(lintResult);
}
try {
if (files.length > 0 && !options.stdin) {
lintAndPrint(null, diff);
} else if (files.length === 0 && options.stdin && !options.fix) {
import('get-stdin')
.then(module => module.default())
.then(lintAndPrint);
} else {
program.help();
}
} catch (error) {
console.error(error);
process.exit(exitCodes.unexpectedError);
}