UNPKG

2 kBJavaScriptView Raw
1/**
2 * @module
3 * @see {@link https://www.npmjs.com/package/markdownlint|Markdownlint}
4 */
5
6"use strict";
7
8const fs = require("fs");
9const markdownlint = require("markdownlint");
10const SEVERITY = require("../severity");
11
12/**
13 * Initialise un checker pour Markdownlint en cherchant l'éventuelle
14 * configuration.
15 *
16 * @returns {object} Le patron et les options par défaut.
17 */
18const 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 * Vérifie un fichier avec le linter <strong>MarkdownLint</strong>.
34 *
35 * @param {string} file Le fichier qui sera vérifié.
36 * @param {number} level Le niveau de sévérité minimum des notifications
37 * retournées.
38 * @param {object} options Les options qui seront passées au linter.
39 * @returns {Promise.<Array.<object>>} Une promesse retournant la liste des
40 * notifications.
41 */
42const 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
72module.exports = { configure, wrapper };