Move each rule implementation into its own file (fixes #83).

This commit is contained in:
David Anson 2018-01-21 21:44:25 -08:00
parent 49e36f817c
commit 9ba143555d
42 changed files with 1289 additions and 1028 deletions

44
lib/md043.js Normal file
View file

@ -0,0 +1,44 @@
// @ts-check
"use strict";
var shared = require("./shared");
module.exports = {
"names": [ "MD043", "required-headers" ],
"description": "Required header structure",
"tags": [ "headers" ],
"function": function MD043(params, onError) {
var requiredHeaders = params.config.headers;
if (requiredHeaders) {
var levels = {};
[ 1, 2, 3, 4, 5, 6 ].forEach(function forLevel(level) {
levels["h" + level] = "######".substr(-level);
});
var i = 0;
var optional = false;
var errorCount = 0;
shared.forEachHeading(params, function forHeading(heading, content) {
if (!errorCount) {
var actual = levels[heading.tag] + " " + content;
var expected = requiredHeaders[i++] || "[None]";
if (expected === "*") {
optional = true;
} else if (expected.toLowerCase() === actual.toLowerCase()) {
optional = false;
} else if (optional) {
i--;
} else {
shared.addErrorDetailIf(onError, heading.lineNumber,
expected, actual);
errorCount++;
}
}
});
if ((i < requiredHeaders.length) && !errorCount) {
shared.addErrorContext(onError, params.lines.length,
requiredHeaders[i]);
}
}
}
};