mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Add MD022 with tests, create shared module.
This commit is contained in:
parent
5591cf4587
commit
9bedd25234
7 changed files with 75 additions and 3 deletions
|
|
@ -3,6 +3,7 @@
|
|||
var fs = require("fs");
|
||||
var md = require("markdown-it")();
|
||||
var rules = require("./rules");
|
||||
var shared = require("./shared");
|
||||
|
||||
function numberComparison(a, b) {
|
||||
return a - b;
|
||||
|
|
@ -18,7 +19,7 @@ function lintFile(file, config, callback) {
|
|||
callback(err);
|
||||
} else {
|
||||
var tokens = md.parse(contents, {});
|
||||
var lines = contents.split(/\r\n|\r|\n/);
|
||||
var lines = contents.split(shared.newLineRe);
|
||||
tokens.forEach(function forToken(token) {
|
||||
if (token.lines) {
|
||||
token.line = lines[token.lines[0]];
|
||||
|
|
|
|||
40
lib/rules.js
40
lib/rules.js
|
|
@ -1,5 +1,7 @@
|
|||
"use strict";
|
||||
|
||||
var shared = require("./shared");
|
||||
|
||||
function indentFor(token) {
|
||||
return token.line.length - token.line.trimLeft().length;
|
||||
}
|
||||
|
|
@ -253,7 +255,7 @@ module.exports = [
|
|||
filterTokens(params.tokens, "code_block", "fence")
|
||||
.forEach(function forToken(token) {
|
||||
if (token.content && token.content
|
||||
.split(/\r\n|\r|\n/)
|
||||
.split(shared.newLineRe)
|
||||
.filter(function filterLine(line) {
|
||||
return line;
|
||||
}).every(function forLine(line) {
|
||||
|
|
@ -318,6 +320,42 @@ module.exports = [
|
|||
}
|
||||
},
|
||||
|
||||
{
|
||||
"name": "MD022",
|
||||
"desc": "Headers should be surrounded by blank lines",
|
||||
"func": function MD022(params, errors) {
|
||||
var prevHeadingLineNumber = 0;
|
||||
var prevMaxLineIndex = -1;
|
||||
var needBlankLine = false;
|
||||
params.tokens.forEach(function forToken(token) {
|
||||
if (token.type === "heading_open") {
|
||||
if ((token.lines[0] - prevMaxLineIndex) === 0) {
|
||||
errors.push(token.lineNumber);
|
||||
}
|
||||
prevHeadingLineNumber = token.lineNumber;
|
||||
} else if (token.type === "heading_close") {
|
||||
needBlankLine = true;
|
||||
} else if (token.type === "inline") {
|
||||
token.content.split(shared.newLineRe)
|
||||
.forEach(function forLine(line, offset) {
|
||||
if (/^(-+|=+)\s*$/.test(line)) {
|
||||
errors.push(token.lines[0] + offset);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (token.lines) {
|
||||
if (needBlankLine) {
|
||||
if ((token.lines[0] - prevMaxLineIndex) === 0) {
|
||||
errors.push(prevHeadingLineNumber);
|
||||
}
|
||||
needBlankLine = false;
|
||||
}
|
||||
prevMaxLineIndex = Math.max(prevMaxLineIndex, token.lines[1]);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"name": "MD028",
|
||||
"desc": "Blank line inside blockquote",
|
||||
|
|
|
|||
3
lib/shared.js
Normal file
3
lib/shared.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
"use strict";
|
||||
|
||||
module.exports.newLineRe = /\r\n|\r|\n/;
|
||||
Loading…
Add table
Add a link
Reference in a new issue