mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Add MD001 with test, refactor to remove common code.
This commit is contained in:
parent
82caaa9407
commit
75b63a43ab
3 changed files with 43 additions and 19 deletions
|
@ -21,7 +21,8 @@ function lintFile(file, options, callback) {
|
||||||
var lines = contents.split(/\r\n|\r|\n/g);
|
var lines = contents.split(/\r\n|\r|\n/g);
|
||||||
var result = {};
|
var result = {};
|
||||||
rules.forEach(function forRule(rule) {
|
rules.forEach(function forRule(rule) {
|
||||||
var errors = rule.func(tokens, lines);
|
var errors = [];
|
||||||
|
rule.func(errors, tokens, lines);
|
||||||
if (errors.length) {
|
if (errors.length) {
|
||||||
errors.sort(numberComparison);
|
errors.sort(numberComparison);
|
||||||
result[rule.name] = errors.filter(uniqueFilterForSorted);
|
result[rule.name] = errors.filter(uniqueFilterForSorted);
|
||||||
|
|
52
lib/rules.js
52
lib/rules.js
|
@ -1,5 +1,9 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
function lineNumberFrom(token) {
|
||||||
|
return token.lines[0] + 1;
|
||||||
|
}
|
||||||
|
|
||||||
function padAndTrim(lines) {
|
function padAndTrim(lines) {
|
||||||
return [].concat(
|
return [].concat(
|
||||||
"",
|
"",
|
||||||
|
@ -10,61 +14,74 @@ function padAndTrim(lines) {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = [
|
module.exports = [
|
||||||
|
{
|
||||||
|
"name": "MD001",
|
||||||
|
"desc": "Header levels should only increment by one level at a time",
|
||||||
|
"func": function MD001(errors, tokens) {
|
||||||
|
var prevLevel = 0;
|
||||||
|
tokens.filter(function filterToken(token) {
|
||||||
|
return (token.type === "heading_open");
|
||||||
|
}).forEach(function forToken(token) {
|
||||||
|
if (prevLevel && (token.hLevel > prevLevel + 1)) {
|
||||||
|
errors.push(lineNumberFrom(token));
|
||||||
|
}
|
||||||
|
prevLevel = token.hLevel;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"name": "MD002",
|
"name": "MD002",
|
||||||
"desc": "First header should be a h1 header",
|
"desc": "First header should be a h1 header",
|
||||||
"func": function MD002(tokens) {
|
"func": function MD002(errors, tokens) {
|
||||||
var errors = [];
|
|
||||||
tokens.every(function forToken(token) {
|
tokens.every(function forToken(token) {
|
||||||
if ((token.type === "heading_open") && (token.hLevel !== 1)) {
|
if (token.type === "heading_open") {
|
||||||
errors.push(token.lines[0] + 1);
|
if (token.hLevel !== 1) {
|
||||||
|
errors.push(lineNumberFrom(token));
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
return errors;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"name": "MD031",
|
"name": "MD031",
|
||||||
"desc": "Fenced code blocks should be surrounded by blank lines",
|
"desc": "Fenced code blocks should be surrounded by blank lines",
|
||||||
"func": function MD031(tokens, lines) {
|
"func": function MD031(errors, tokens, lines) {
|
||||||
// Some parsers have trouble detecting fenced code blocks without
|
// Some parsers have trouble detecting fenced code blocks without
|
||||||
// surrounding whitespace, so examine the lines directly.
|
// surrounding whitespace, so examine the lines directly.
|
||||||
lines = padAndTrim(lines);
|
lines = padAndTrim(lines);
|
||||||
var errors = [];
|
|
||||||
var inCode = false;
|
var inCode = false;
|
||||||
lines.forEach(function forLine(line, lineNum) {
|
lines.forEach(function forLine(line, lineNumber) {
|
||||||
if (line.match(/^(```|~~~)/)) {
|
if (line.match(/^(```|~~~)/)) {
|
||||||
inCode = !inCode;
|
inCode = !inCode;
|
||||||
if ((inCode && lines[lineNum - 1].length) ||
|
if ((inCode && lines[lineNumber - 1].length) ||
|
||||||
(!inCode && lines[lineNum + 1].length)) {
|
(!inCode && lines[lineNumber + 1].length)) {
|
||||||
errors.push(lineNum);
|
errors.push(lineNumber);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return errors;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"name": "MD032",
|
"name": "MD032",
|
||||||
"desc": "Lists should be surrounded by blank lines",
|
"desc": "Lists should be surrounded by blank lines",
|
||||||
"func": function MD032(tokens, lines) {
|
"func": function MD032(errors, tokens, lines) {
|
||||||
// Some parsers have trouble detecting lists without surrounding
|
// Some parsers have trouble detecting lists without surrounding
|
||||||
// whitespace, so examine the lines directly.
|
// whitespace, so examine the lines directly.
|
||||||
var errors = [];
|
|
||||||
var inList = false;
|
var inList = false;
|
||||||
var inCode = false;
|
var inCode = false;
|
||||||
var prevLine = "";
|
var prevLine = "";
|
||||||
lines.forEach(function forLine(line, lineNum) {
|
lines.forEach(function forLine(line, lineNumber) {
|
||||||
if (!inCode) {
|
if (!inCode) {
|
||||||
var listMarker = line.trim().match(/^([\*\+\-]|(\d+\.))\s/);
|
var listMarker = line.trim().match(/^([\*\+\-]|(\d+\.))\s/);
|
||||||
if (listMarker && !inList && !prevLine.match(/^($|\s)/)) {
|
if (listMarker && !inList && !prevLine.match(/^($|\s)/)) {
|
||||||
errors.push(lineNum + 1);
|
errors.push(lineNumber + 1);
|
||||||
} else if (!listMarker && inList && !line.match(/^($|\s)/)) {
|
} else if (!listMarker && inList && !line.match(/^($|\s)/)) {
|
||||||
errors.push(lineNum);
|
errors.push(lineNumber);
|
||||||
}
|
}
|
||||||
inList = listMarker;
|
inList = listMarker;
|
||||||
}
|
}
|
||||||
|
@ -74,7 +91,6 @@ module.exports = [
|
||||||
}
|
}
|
||||||
prevLine = line;
|
prevLine = line;
|
||||||
});
|
});
|
||||||
return errors;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
7
test/headers_bad.md
Normal file
7
test/headers_bad.md
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# Header
|
||||||
|
|
||||||
|
### Header 3 {MD001}
|
||||||
|
|
||||||
|
## Header 2
|
||||||
|
|
||||||
|
#### Header 4 {MD001}
|
Loading…
Add table
Add a link
Reference in a new issue