Add MD043 required-headers "Required header structure" (fixes #22).

This commit is contained in:
David Anson 2016-07-02 22:37:52 -07:00
parent 2612a96ae8
commit c8ecec1953
26 changed files with 316 additions and 10 deletions

View file

@ -898,7 +898,7 @@ module.exports = [
"desc": "No empty links",
"tags": [ "links" ],
"aliases": [ "no-empty-links" ],
"func": function MD034(params, errors) {
"func": function MD042(params, errors) {
forEachInlineChild(params, "link_open", function forToken(token) {
token.attrs.forEach(function forAttr(attr) {
if (attr[0] === "href" && (!attr[1] || (attr[1] === "#"))) {
@ -907,5 +907,41 @@ module.exports = [
});
});
}
},
{
"name": "MD043",
"desc": "Required header structure",
"tags": [ "headers" ],
"aliases": [ "required-headers" ],
"func": function MD043(params, errors) {
var requiredHeaders = params.options.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;
forEachHeading(params, function forHeading(heading, content) {
if (!errors.length) {
var actual = levels[heading.tag] + " " + content;
var expected = requiredHeaders[i++] || "";
if (expected === "*") {
optional = true;
} else if (expected.toLowerCase() === actual.toLowerCase()) {
optional = false;
} else if (optional) {
i--;
} else {
errors.push(heading.lineNumber);
}
}
});
if ((i < requiredHeaders.length) && !errors.length) {
errors.push(params.lines.length);
}
}
}
}
];