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 fs = require("fs");
|
||||||
var md = require("markdown-it")();
|
var md = require("markdown-it")();
|
||||||
var rules = require("./rules");
|
var rules = require("./rules");
|
||||||
|
var shared = require("./shared");
|
||||||
|
|
||||||
function numberComparison(a, b) {
|
function numberComparison(a, b) {
|
||||||
return a - b;
|
return a - b;
|
||||||
|
|
@ -18,7 +19,7 @@ function lintFile(file, config, callback) {
|
||||||
callback(err);
|
callback(err);
|
||||||
} else {
|
} else {
|
||||||
var tokens = md.parse(contents, {});
|
var tokens = md.parse(contents, {});
|
||||||
var lines = contents.split(/\r\n|\r|\n/);
|
var lines = contents.split(shared.newLineRe);
|
||||||
tokens.forEach(function forToken(token) {
|
tokens.forEach(function forToken(token) {
|
||||||
if (token.lines) {
|
if (token.lines) {
|
||||||
token.line = lines[token.lines[0]];
|
token.line = lines[token.lines[0]];
|
||||||
|
|
|
||||||
40
lib/rules.js
40
lib/rules.js
|
|
@ -1,5 +1,7 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
var shared = require("./shared");
|
||||||
|
|
||||||
function indentFor(token) {
|
function indentFor(token) {
|
||||||
return token.line.length - token.line.trimLeft().length;
|
return token.line.length - token.line.trimLeft().length;
|
||||||
}
|
}
|
||||||
|
|
@ -253,7 +255,7 @@ module.exports = [
|
||||||
filterTokens(params.tokens, "code_block", "fence")
|
filterTokens(params.tokens, "code_block", "fence")
|
||||||
.forEach(function forToken(token) {
|
.forEach(function forToken(token) {
|
||||||
if (token.content && token.content
|
if (token.content && token.content
|
||||||
.split(/\r\n|\r|\n/)
|
.split(shared.newLineRe)
|
||||||
.filter(function filterLine(line) {
|
.filter(function filterLine(line) {
|
||||||
return line;
|
return line;
|
||||||
}).every(function forLine(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",
|
"name": "MD028",
|
||||||
"desc": "Blank line inside blockquote",
|
"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/;
|
||||||
5
test/headers_good.md
Normal file
5
test/headers_good.md
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
# Heading 1
|
||||||
|
|
||||||
|
## Heading 2
|
||||||
|
|
||||||
|
## Heading 3
|
||||||
9
test/headers_surrounding_space_atx.md
Normal file
9
test/headers_surrounding_space_atx.md
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
# Header 1
|
||||||
|
|
||||||
|
## Header 2 {MD022}
|
||||||
|
Some text
|
||||||
|
## Header 3 {MD022}
|
||||||
|
Some text
|
||||||
|
## Header 4 {MD022}
|
||||||
|
|
||||||
|
## Header 5
|
||||||
15
test/headers_surrounding_space_setext.md
Normal file
15
test/headers_surrounding_space_setext.md
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
Header 1
|
||||||
|
========
|
||||||
|
|
||||||
|
Header 2 {MD022}
|
||||||
|
----------------
|
||||||
|
Some text
|
||||||
|
Header 3 {MD022}
|
||||||
|
================
|
||||||
|
Some text
|
||||||
|
Header 4 {MD022}
|
||||||
|
================
|
||||||
|
Some text
|
||||||
|
|
||||||
|
Header 5
|
||||||
|
--------
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var path = require("path");
|
var path = require("path");
|
||||||
var markdownlint = require("../lib/markdownlint");
|
var markdownlint = require("../lib/markdownlint");
|
||||||
|
var shared = require("../lib/shared");
|
||||||
var Q = require("q");
|
var Q = require("q");
|
||||||
|
|
||||||
function createTestForFile(file) {
|
function createTestForFile(file) {
|
||||||
|
|
@ -31,7 +32,7 @@ function createTestForFile(file) {
|
||||||
var expectedPromise = Q.nfcall(fs.readFile, file, { "encoding": "utf8" })
|
var expectedPromise = Q.nfcall(fs.readFile, file, { "encoding": "utf8" })
|
||||||
.then(
|
.then(
|
||||||
function fileContents(contents) {
|
function fileContents(contents) {
|
||||||
var lines = contents.split(/\r\n|\r|\n/);
|
var lines = contents.split(shared.newLineRe);
|
||||||
var results = {};
|
var results = {};
|
||||||
lines.forEach(function forLine(line, lineNum) {
|
lines.forEach(function forLine(line, lineNum) {
|
||||||
var regex = /\{(MD\d+)(?::(\d+))?\}/g;
|
var regex = /\{(MD\d+)(?::(\d+))?\}/g;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue