1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const fs = require("fs");
|
9 | const markdownlint = require("markdownlint");
|
10 | const SEVERITY = require("../severity");
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const configure = function () {
|
19 | let config = null;
|
20 | for (const file of fs.readdirSync(".")) {
|
21 | if ([".markdownlint.json", ".markdownlintrc"].includes(file)) {
|
22 | config = "../" + file;
|
23 | break;
|
24 | }
|
25 | }
|
26 | return {
|
27 | "patterns": "*.md",
|
28 | "linters": { "markdownlint": config }
|
29 | };
|
30 | };
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | const wrapper = async function (file, level, options) {
|
43 | if (SEVERITY.ERROR > level) {
|
44 | return [];
|
45 | }
|
46 |
|
47 | const config = {
|
48 | "files": file,
|
49 | "config": options
|
50 | };
|
51 | const results = await new Promise((resolve) => {
|
52 | markdownlint(config, (_, r) => resolve(r));
|
53 | });
|
54 | if ("" === results.toString()) {
|
55 | return [];
|
56 | }
|
57 |
|
58 | return results.toString().split("\n")
|
59 | .map((result) => {
|
60 | const parts = result.match(/[^ ]+ (\d+): ([^ ]+) (.*)/u);
|
61 |
|
62 | return {
|
63 | "file": file,
|
64 | "linter": "markdownlint",
|
65 | "rule": parts[2],
|
66 | "message": parts[3],
|
67 | "locations": [{ "line": parseInt(parts[1], 10) }]
|
68 | };
|
69 | });
|
70 | };
|
71 |
|
72 | module.exports = { configure, wrapper };
|