2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-04-27 22:05:34 -07:00
|
|
|
const shared = require("./shared");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
module.exports = {
|
2018-03-19 23:39:42 +01:00
|
|
|
"names": [ "MD043", "required-headings", "required-headers" ],
|
|
|
|
"description": "Required heading structure",
|
|
|
|
"tags": [ "headings", "headers" ],
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD043(params, onError) {
|
2018-04-27 22:05:34 -07:00
|
|
|
const requiredHeadings = params.config.headings || params.config.headers;
|
2018-03-19 23:39:42 +01:00
|
|
|
if (requiredHeadings) {
|
2018-04-27 22:05:34 -07:00
|
|
|
const levels = {};
|
2018-01-21 21:44:25 -08:00
|
|
|
[ 1, 2, 3, 4, 5, 6 ].forEach(function forLevel(level) {
|
|
|
|
levels["h" + level] = "######".substr(-level);
|
|
|
|
});
|
2018-04-27 22:05:34 -07:00
|
|
|
let i = 0;
|
|
|
|
let optional = false;
|
|
|
|
let errorCount = 0;
|
2018-01-21 21:44:25 -08:00
|
|
|
shared.forEachHeading(params, function forHeading(heading, content) {
|
|
|
|
if (!errorCount) {
|
2018-04-27 22:05:34 -07:00
|
|
|
const actual = levels[heading.tag] + " " + content;
|
|
|
|
const expected = requiredHeadings[i++] || "[None]";
|
2018-01-21 21:44:25 -08:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-03-19 23:39:42 +01:00
|
|
|
if ((i < requiredHeadings.length) && !errorCount) {
|
2018-01-21 21:44:25 -08:00
|
|
|
shared.addErrorContext(onError, params.lines.length,
|
2018-03-19 23:39:42 +01:00
|
|
|
requiredHeadings[i]);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|