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
52
lib/rules.js
52
lib/rules.js
|
@ -1,5 +1,9 @@
|
|||
"use strict";
|
||||
|
||||
function lineNumberFrom(token) {
|
||||
return token.lines[0] + 1;
|
||||
}
|
||||
|
||||
function padAndTrim(lines) {
|
||||
return [].concat(
|
||||
"",
|
||||
|
@ -10,61 +14,74 @@ function padAndTrim(lines) {
|
|||
}
|
||||
|
||||
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",
|
||||
"desc": "First header should be a h1 header",
|
||||
"func": function MD002(tokens) {
|
||||
var errors = [];
|
||||
"func": function MD002(errors, tokens) {
|
||||
tokens.every(function forToken(token) {
|
||||
if ((token.type === "heading_open") && (token.hLevel !== 1)) {
|
||||
errors.push(token.lines[0] + 1);
|
||||
if (token.type === "heading_open") {
|
||||
if (token.hLevel !== 1) {
|
||||
errors.push(lineNumberFrom(token));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return errors;
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"name": "MD031",
|
||||
"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
|
||||
// surrounding whitespace, so examine the lines directly.
|
||||
lines = padAndTrim(lines);
|
||||
var errors = [];
|
||||
var inCode = false;
|
||||
lines.forEach(function forLine(line, lineNum) {
|
||||
lines.forEach(function forLine(line, lineNumber) {
|
||||
if (line.match(/^(```|~~~)/)) {
|
||||
inCode = !inCode;
|
||||
if ((inCode && lines[lineNum - 1].length) ||
|
||||
(!inCode && lines[lineNum + 1].length)) {
|
||||
errors.push(lineNum);
|
||||
if ((inCode && lines[lineNumber - 1].length) ||
|
||||
(!inCode && lines[lineNumber + 1].length)) {
|
||||
errors.push(lineNumber);
|
||||
}
|
||||
}
|
||||
});
|
||||
return errors;
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"name": "MD032",
|
||||
"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
|
||||
// whitespace, so examine the lines directly.
|
||||
var errors = [];
|
||||
var inList = false;
|
||||
var inCode = false;
|
||||
var prevLine = "";
|
||||
lines.forEach(function forLine(line, lineNum) {
|
||||
lines.forEach(function forLine(line, lineNumber) {
|
||||
if (!inCode) {
|
||||
var listMarker = line.trim().match(/^([\*\+\-]|(\d+\.))\s/);
|
||||
if (listMarker && !inList && !prevLine.match(/^($|\s)/)) {
|
||||
errors.push(lineNum + 1);
|
||||
errors.push(lineNumber + 1);
|
||||
} else if (!listMarker && inList && !line.match(/^($|\s)/)) {
|
||||
errors.push(lineNum);
|
||||
errors.push(lineNumber);
|
||||
}
|
||||
inList = listMarker;
|
||||
}
|
||||
|
@ -74,7 +91,6 @@ module.exports = [
|
|||
}
|
||||
prevLine = line;
|
||||
});
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue