Add MD003 with tests, add JSON config for rules.

This commit is contained in:
David Anson 2015-02-27 22:06:54 -08:00
parent 75b63a43ab
commit a2d42b6208
14 changed files with 266 additions and 36 deletions

View file

@ -4,6 +4,16 @@ function lineNumberFrom(token) {
return token.lines[0] + 1;
}
function headingStyleFrom(token, lines) {
if ((token.lines[1] - token.lines[0]) === 1) {
if (lines[token.lines[0]].match(/#\s*$/)) {
return "atx_closed";
}
return "atx";
}
return "setext";
}
function padAndTrim(lines) {
return [].concat(
"",
@ -17,9 +27,9 @@ module.exports = [
{
"name": "MD001",
"desc": "Header levels should only increment by one level at a time",
"func": function MD001(errors, tokens) {
"func": function MD001(params, errors) {
var prevLevel = 0;
tokens.filter(function filterToken(token) {
params.tokens.filter(function filterToken(token) {
return (token.type === "heading_open");
}).forEach(function forToken(token) {
if (prevLevel && (token.hLevel > prevLevel + 1)) {
@ -33,8 +43,8 @@ module.exports = [
{
"name": "MD002",
"desc": "First header should be a h1 header",
"func": function MD002(errors, tokens) {
tokens.every(function forToken(token) {
"func": function MD002(params, errors) {
params.tokens.every(function forToken(token) {
if (token.type === "heading_open") {
if (token.hLevel !== 1) {
errors.push(lineNumberFrom(token));
@ -46,13 +56,32 @@ module.exports = [
}
},
{
"name": "MD003",
"desc": "Header style",
"func": function MD003(params, errors) {
var style = params.options.style || "consistent";
var headings = params.tokens.filter(function filterToken(token) {
return (token.type === "heading_open");
});
if ((style === "consistent") && headings.length) {
style = headingStyleFrom(headings[0], params.lines);
}
headings.forEach(function forToken(token) {
if (headingStyleFrom(token, params.lines) !== style) {
errors.push(lineNumberFrom(token));
}
});
}
},
{
"name": "MD031",
"desc": "Fenced code blocks should be surrounded by blank lines",
"func": function MD031(errors, tokens, lines) {
"func": function MD031(params, errors) {
// Some parsers have trouble detecting fenced code blocks without
// surrounding whitespace, so examine the lines directly.
lines = padAndTrim(lines);
var lines = padAndTrim(params.lines);
var inCode = false;
lines.forEach(function forLine(line, lineNumber) {
if (line.match(/^(```|~~~)/)) {
@ -69,13 +98,13 @@ module.exports = [
{
"name": "MD032",
"desc": "Lists should be surrounded by blank lines",
"func": function MD032(errors, tokens, lines) {
"func": function MD032(params, errors) {
// Some parsers have trouble detecting lists without surrounding
// whitespace, so examine the lines directly.
var inList = false;
var inCode = false;
var prevLine = "";
lines.forEach(function forLine(line, lineNumber) {
params.lines.forEach(function forLine(line, lineNumber) {
if (!inCode) {
var listMarker = line.trim().match(/^([\*\+\-]|(\d+\.))\s/);
if (listMarker && !inList && !prevLine.match(/^($|\s)/)) {