-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (34 loc) · 1.25 KB
/
index.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
const fs = require('node:fs');
const path = require('node:path');
const fixFile = require('./lib/fix');
const lintFile = require('./lib/lint');
const { getFilesByPath } = require('./lib/utils');
const appConfig = require('./.markdownlintrc');
function markdownLint({ paths = [], fix, ext, recursive, config, typograph }) {
const dirs = paths.filter(p => fs.existsSync(p) && fs.statSync(p).isDirectory());
const extensions = ext.join('|');
const extensionsRegExp = new RegExp(`.+\\.(${extensions})$`, 'i');
const files = paths
.filter(p => extensionsRegExp.test(p) && fs.existsSync(p) && fs.statSync(p).isFile())
.concat(...(dirs.map(d => getFilesByPath(d, extensions, recursive))));
// eslint-disable-next-line import/no-dynamic-require
const externalConfig = config && require(path.resolve(config));
// eslint-disable-next-line no-restricted-syntax
for (const filePath of files) {
let fileContent = fs.readFileSync(filePath, 'utf8');
if (fix) {
fileContent = fixFile(fileContent, {
appConfig,
externalConfig,
typograph,
});
fs.writeFileSync(filePath, fileContent);
}
lintFile(fileContent, {
appConfig,
externalConfig,
filePath,
});
}
}
module.exports = markdownLint;