mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Add MD003 with tests, add JSON config for rules.
This commit is contained in:
parent
75b63a43ab
commit
a2d42b6208
14 changed files with 266 additions and 36 deletions
|
@ -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 {
|
||||
|
|
45
lib/rules.js
45
lib/rules.js
|
@ -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)/)) {
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
},
|
||||
"scripts": {
|
||||
"test": "nodeunit",
|
||||
"debug": "node debug node_modules/nodeunit/bin/nodeunit",
|
||||
"lint": "eslint lib test"
|
||||
},
|
||||
"dependencies": {
|
||||
|
|
34
styles/all.json
Normal file
34
styles/all.json
Normal file
|
@ -0,0 +1,34 @@
|
|||
{
|
||||
"MD001": true,
|
||||
"MD002": true,
|
||||
"MD003": true,
|
||||
"MD004": true,
|
||||
"MD005": true,
|
||||
"MD006": true,
|
||||
"MD007": true,
|
||||
"MD008": true,
|
||||
"MD009": true,
|
||||
"MD010": true,
|
||||
"MD011": true,
|
||||
"MD012": true,
|
||||
"MD013": true,
|
||||
"MD014": true,
|
||||
"MD015": true,
|
||||
"MD016": true,
|
||||
"MD017": true,
|
||||
"MD018": true,
|
||||
"MD019": true,
|
||||
"MD020": true,
|
||||
"MD021": true,
|
||||
"MD022": true,
|
||||
"MD023": true,
|
||||
"MD024": true,
|
||||
"MD025": true,
|
||||
"MD026": true,
|
||||
"MD027": true,
|
||||
"MD028": true,
|
||||
"MD029": true,
|
||||
"MD030": true,
|
||||
"MD031": true,
|
||||
"MD032": true
|
||||
}
|
36
test/incorrect_header_atx.json
Normal file
36
test/incorrect_header_atx.json
Normal file
|
@ -0,0 +1,36 @@
|
|||
{
|
||||
"MD001": true,
|
||||
"MD002": true,
|
||||
"MD003": {
|
||||
"style": "atx"
|
||||
},
|
||||
"MD004": true,
|
||||
"MD005": true,
|
||||
"MD006": true,
|
||||
"MD007": true,
|
||||
"MD008": true,
|
||||
"MD009": true,
|
||||
"MD010": true,
|
||||
"MD011": true,
|
||||
"MD012": true,
|
||||
"MD013": true,
|
||||
"MD014": true,
|
||||
"MD015": true,
|
||||
"MD016": true,
|
||||
"MD017": true,
|
||||
"MD018": true,
|
||||
"MD019": true,
|
||||
"MD020": true,
|
||||
"MD021": true,
|
||||
"MD022": true,
|
||||
"MD023": true,
|
||||
"MD024": true,
|
||||
"MD025": true,
|
||||
"MD026": true,
|
||||
"MD027": true,
|
||||
"MD028": true,
|
||||
"MD029": true,
|
||||
"MD030": true,
|
||||
"MD031": true,
|
||||
"MD032": true
|
||||
}
|
6
test/incorrect_header_atx.md
Normal file
6
test/incorrect_header_atx.md
Normal file
|
@ -0,0 +1,6 @@
|
|||
# Header 1 {MD003} #
|
||||
|
||||
## Header 2
|
||||
|
||||
Header 3 {MD003}
|
||||
----------------
|
36
test/incorrect_header_atx_closed.json
Normal file
36
test/incorrect_header_atx_closed.json
Normal file
|
@ -0,0 +1,36 @@
|
|||
{
|
||||
"MD001": true,
|
||||
"MD002": true,
|
||||
"MD003": {
|
||||
"style": "atx_closed"
|
||||
},
|
||||
"MD004": true,
|
||||
"MD005": true,
|
||||
"MD006": true,
|
||||
"MD007": true,
|
||||
"MD008": true,
|
||||
"MD009": true,
|
||||
"MD010": true,
|
||||
"MD011": true,
|
||||
"MD012": true,
|
||||
"MD013": true,
|
||||
"MD014": true,
|
||||
"MD015": true,
|
||||
"MD016": true,
|
||||
"MD017": true,
|
||||
"MD018": true,
|
||||
"MD019": true,
|
||||
"MD020": true,
|
||||
"MD021": true,
|
||||
"MD022": true,
|
||||
"MD023": true,
|
||||
"MD024": true,
|
||||
"MD025": true,
|
||||
"MD026": true,
|
||||
"MD027": true,
|
||||
"MD028": true,
|
||||
"MD029": true,
|
||||
"MD030": true,
|
||||
"MD031": true,
|
||||
"MD032": true
|
||||
}
|
6
test/incorrect_header_atx_closed.md
Normal file
6
test/incorrect_header_atx_closed.md
Normal file
|
@ -0,0 +1,6 @@
|
|||
# Header 1 #
|
||||
|
||||
## Header 2 {MD003}
|
||||
|
||||
Header 3 {MD003}
|
||||
----------------
|
36
test/incorrect_header_setext.json
Normal file
36
test/incorrect_header_setext.json
Normal file
|
@ -0,0 +1,36 @@
|
|||
{
|
||||
"MD001": true,
|
||||
"MD002": true,
|
||||
"MD003": {
|
||||
"style": "setext"
|
||||
},
|
||||
"MD004": true,
|
||||
"MD005": true,
|
||||
"MD006": true,
|
||||
"MD007": true,
|
||||
"MD008": true,
|
||||
"MD009": true,
|
||||
"MD010": true,
|
||||
"MD011": true,
|
||||
"MD012": true,
|
||||
"MD013": true,
|
||||
"MD014": true,
|
||||
"MD015": true,
|
||||
"MD016": true,
|
||||
"MD017": true,
|
||||
"MD018": true,
|
||||
"MD019": true,
|
||||
"MD020": true,
|
||||
"MD021": true,
|
||||
"MD022": true,
|
||||
"MD023": true,
|
||||
"MD024": true,
|
||||
"MD025": true,
|
||||
"MD026": true,
|
||||
"MD027": true,
|
||||
"MD028": true,
|
||||
"MD029": true,
|
||||
"MD030": true,
|
||||
"MD031": true,
|
||||
"MD032": true
|
||||
}
|
6
test/incorrect_header_setext.md
Normal file
6
test/incorrect_header_setext.md
Normal file
|
@ -0,0 +1,6 @@
|
|||
# Header 1 {MD003} #
|
||||
|
||||
## Header 2 {MD003}
|
||||
|
||||
Header 3
|
||||
--------
|
|
@ -4,34 +4,50 @@ var fs = require("fs");
|
|||
var path = require("path");
|
||||
var markdownlint = require("../lib/markdownlint");
|
||||
|
||||
var encodingUtf8 = { "encoding": "utf8" };
|
||||
|
||||
function createTestForFile(file) {
|
||||
return function testForFile(test) {
|
||||
test.expect(3);
|
||||
fs.readFile(
|
||||
file,
|
||||
{ "encoding": "utf8" },
|
||||
function readFileCallback(err, contents) {
|
||||
test.expect(4);
|
||||
fs.readFile(file, encodingUtf8, function readFileCallback(err, contents) {
|
||||
test.ifError(err);
|
||||
var lines = contents.split(/\r\n|\r|\n/g);
|
||||
var results = {};
|
||||
lines.forEach(function forLine(line, lineNum) {
|
||||
var match = line.match(/\{(MD\d+)(?::(\d+))?\}/);
|
||||
if (match) {
|
||||
var rule = match[1];
|
||||
var errors = results[rule] || [];
|
||||
errors.push(lineNum + 1);
|
||||
results[rule] = errors;
|
||||
function lintFile(config) {
|
||||
var results = {};
|
||||
lines.forEach(function forLine(line, lineNum) {
|
||||
var match = line.match(/\{(MD\d+)(?::(\d+))?\}/);
|
||||
if (match) {
|
||||
var rule = match[1];
|
||||
var errors = results[rule] || [];
|
||||
errors.push(lineNum + 1);
|
||||
results[rule] = errors;
|
||||
}
|
||||
});
|
||||
markdownlint({
|
||||
"files": [ file ],
|
||||
"config": config
|
||||
}, function markdownlintCallback(errr, actual) {
|
||||
test.ifError(errr);
|
||||
var expected = {};
|
||||
expected[file] = results;
|
||||
test.deepEqual(actual, expected, "Line numbers are not correct.");
|
||||
test.done();
|
||||
});
|
||||
}
|
||||
var configFile = file.replace(/\.md$/, ".json");
|
||||
fs.stat(configFile, function statCallback(errr /*, stats*/) {
|
||||
if (errr) {
|
||||
test.ok(true, "Replacement for ifError of readFile");
|
||||
lintFile();
|
||||
} else {
|
||||
fs.readFile(configFile, encodingUtf8,
|
||||
function readFile(errrr, configContents) {
|
||||
test.ifError(errrr);
|
||||
var config = JSON.parse(configContents);
|
||||
lintFile(config);
|
||||
});
|
||||
}
|
||||
});
|
||||
markdownlint({
|
||||
"files": [ file ]
|
||||
}, function markdownlintCallback(errr, actual) {
|
||||
test.ifError(errr);
|
||||
var expected = {};
|
||||
expected[file] = results;
|
||||
test.deepEqual(actual, expected, "Line numbers are not correct.");
|
||||
test.done();
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
|
|
6
test/mixed_header_types_atx.md
Normal file
6
test/mixed_header_types_atx.md
Normal file
|
@ -0,0 +1,6 @@
|
|||
# Header
|
||||
|
||||
## Header 2 {MD003} ##
|
||||
|
||||
Header 3 {MD003}
|
||||
----------------
|
6
test/mixed_header_types_atx_closed.md
Normal file
6
test/mixed_header_types_atx_closed.md
Normal file
|
@ -0,0 +1,6 @@
|
|||
# Header 1 #
|
||||
|
||||
## Header 2 {MD003}
|
||||
|
||||
Header 3 {MD003}
|
||||
----------------
|
6
test/mixed_header_types_setext.md
Normal file
6
test/mixed_header_types_setext.md
Normal file
|
@ -0,0 +1,6 @@
|
|||
Header 1
|
||||
========
|
||||
|
||||
## Header 2 {MD003}
|
||||
|
||||
## Header 3 {MD003} ##
|
Loading…
Add table
Add a link
Reference in a new issue