diff --git a/lib/markdownlint.js b/lib/markdownlint.js index c9b8fbf6..009accb6 100644 --- a/lib/markdownlint.js +++ b/lib/markdownlint.js @@ -18,12 +18,11 @@ function lintFile(file, options, callback) { } else { var lines = contents.split(/\r\n|\n/g); var result = {}; - Object.keys(rules).forEach(function forRule(name) { - var rule = rules[name]; - var errors = rule(lines); + rules.forEach(function forRule(rule) { + var errors = rule.func(lines); if (errors.length) { errors.sort(numberComparison); - result[name] = errors.filter(uniqueFilterForSorted); + result[rule.name] = errors.filter(uniqueFilterForSorted); } }); callback(null, result); diff --git a/lib/rules.js b/lib/rules.js index 1dc3af20..1ad26739 100644 --- a/lib/rules.js +++ b/lib/rules.js @@ -9,48 +9,56 @@ function padAndTrim(lines) { ""); } -module.exports = { - "MD031": function MD031(lines) { - // Some parsers have trouble detecting fenced code blocks without - // surrounding whitespace, so examine the lines directly. - lines = padAndTrim(lines); - var errors = []; - var inCode = false; - lines.forEach(function forLine(line, lineNum) { - if (line.match(/^(```|~~~)/)) { - inCode = !inCode; - if ((inCode && lines[lineNum - 1].length) || - (!inCode && lines[lineNum + 1].length)) { - errors.push(lineNum); +module.exports = [ + { + "name": "MD031", + "desc": "Fenced code blocks should be surrounded by blank lines", + "func": function MD031(lines) { + // Some parsers have trouble detecting fenced code blocks without + // surrounding whitespace, so examine the lines directly. + lines = padAndTrim(lines); + var errors = []; + var inCode = false; + lines.forEach(function forLine(line, lineNum) { + if (line.match(/^(```|~~~)/)) { + inCode = !inCode; + if ((inCode && lines[lineNum - 1].length) || + (!inCode && lines[lineNum + 1].length)) { + errors.push(lineNum); + } } - } - }); - return errors; + }); + return errors; + } }, - "MD032": function MD032(lines) { - // Some parsers have trouble detecting lists without surrounding - // whitespace, so examine the lines directly. - var errors = []; - var inList = false; - var inCode = false; - var prevLine = ""; - lines.forEach(function forLine(line, lineNum) { - if (!inCode) { - var listMarker = line.trim().match(/^([\*\+\-]|(\d+\.))\s/); - if (listMarker && !inList && !prevLine.match(/^($|\s)/)) { - errors.push(lineNum + 1); - } else if (!listMarker && inList && !line.match(/^($|\s)/)) { - errors.push(lineNum); + { + "name": "MD032", + "desc": "Lists should be surrounded by blank lines", + "func": function MD032(lines) { + // Some parsers have trouble detecting lists without surrounding + // whitespace, so examine the lines directly. + var errors = []; + var inList = false; + var inCode = false; + var prevLine = ""; + lines.forEach(function forLine(line, lineNum) { + if (!inCode) { + var listMarker = line.trim().match(/^([\*\+\-]|(\d+\.))\s/); + if (listMarker && !inList && !prevLine.match(/^($|\s)/)) { + errors.push(lineNum + 1); + } else if (!listMarker && inList && !line.match(/^($|\s)/)) { + errors.push(lineNum); + } + inList = listMarker; } - inList = listMarker; - } - if (line.trim().match(/^(```|~~~)/)) { - inCode = !inCode; - inList = false; - } - prevLine = line; - }); - return errors; + if (line.trim().match(/^(```|~~~)/)) { + inCode = !inCode; + inList = false; + } + prevLine = line; + }); + return errors; + } } -}; +];