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

@ -12,17 +12,21 @@ function uniqueFilterForSorted(value, index, array) {
return (index === 0) || (value > array[index - 1]);
}
function lintFile(file, options, callback) {
function lintFile(file, config, callback) {
fs.readFile(file, { "encoding": "utf8" }, function readFile(err, contents) {
if (err) {
callback(err);
} else {
var tokens = md.parse(contents);
var lines = contents.split(/\r\n|\r|\n/g);
var params = {
"tokens": md.parse(contents),
"lines": contents.split(/\r\n|\r|\n/g)
};
var result = {};
rules.forEach(function forRule(rule) {
var ruleConfig = config[rule.name];
params.options = (ruleConfig instanceof Object) ? ruleConfig : {};
var errors = [];
rule.func(errors, tokens, lines);
rule.func(params, errors);
if (errors.length) {
errors.sort(numberComparison);
result[rule.name] = errors.filter(uniqueFilterForSorted);
@ -34,12 +38,14 @@ function lintFile(file, options, callback) {
}
module.exports = function markdownlint(options, callback) {
var results = {};
options = options || {};
var files = options.files || [];
var config = options.config || {};
var results = {};
function lintFiles() {
var file = files.shift();
if (file) {
lintFile(file, options, function lintFileCallback(err, result) {
lintFile(file, config, function lintFileCallback(err, result) {
if (err) {
callback(err);
} else {

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)/)) {