mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 14:00:13 +01:00
Add MD002 with test, incorporate markdown-it parser.
This commit is contained in:
parent
5d35b8dfea
commit
82caaa9407
6 changed files with 29 additions and 5 deletions
|
|
@ -1,6 +1,7 @@
|
|||
"use strict";
|
||||
|
||||
var fs = require("fs");
|
||||
var md = require("markdown-it")();
|
||||
var rules = require("./rules");
|
||||
|
||||
function numberComparison(a, b) {
|
||||
|
|
@ -16,10 +17,11 @@ function lintFile(file, options, callback) {
|
|||
if (err) {
|
||||
callback(err);
|
||||
} else {
|
||||
var lines = contents.split(/\r\n|\n/g);
|
||||
var tokens = md.parse(contents);
|
||||
var lines = contents.split(/\r\n|\r|\n/g);
|
||||
var result = {};
|
||||
rules.forEach(function forRule(rule) {
|
||||
var errors = rule.func(lines);
|
||||
var errors = rule.func(tokens, lines);
|
||||
if (errors.length) {
|
||||
errors.sort(numberComparison);
|
||||
result[rule.name] = errors.filter(uniqueFilterForSorted);
|
||||
|
|
|
|||
20
lib/rules.js
20
lib/rules.js
|
|
@ -10,10 +10,26 @@ function padAndTrim(lines) {
|
|||
}
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
"name": "MD002",
|
||||
"desc": "First header should be a h1 header",
|
||||
"func": function MD002(tokens) {
|
||||
var errors = [];
|
||||
tokens.every(function forToken(token) {
|
||||
if ((token.type === "heading_open") && (token.hLevel !== 1)) {
|
||||
errors.push(token.lines[0] + 1);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return errors;
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"name": "MD031",
|
||||
"desc": "Fenced code blocks should be surrounded by blank lines",
|
||||
"func": function MD031(lines) {
|
||||
"func": function MD031(tokens, lines) {
|
||||
// Some parsers have trouble detecting fenced code blocks without
|
||||
// surrounding whitespace, so examine the lines directly.
|
||||
lines = padAndTrim(lines);
|
||||
|
|
@ -35,7 +51,7 @@ module.exports = [
|
|||
{
|
||||
"name": "MD032",
|
||||
"desc": "Lists should be surrounded by blank lines",
|
||||
"func": function MD032(lines) {
|
||||
"func": function MD032(tokens, lines) {
|
||||
// Some parsers have trouble detecting lists without surrounding
|
||||
// whitespace, so examine the lines directly.
|
||||
var errors = [];
|
||||
|
|
|
|||
|
|
@ -17,6 +17,9 @@
|
|||
"test": "nodeunit",
|
||||
"lint": "eslint lib test"
|
||||
},
|
||||
"dependencies": {
|
||||
"markdown-it": "^3.0.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "^0.15.0",
|
||||
"nodeunit": "^0.9.0"
|
||||
|
|
|
|||
1
test/first_header_bad_atx.md
Normal file
1
test/first_header_bad_atx.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
## Header {MD002}
|
||||
2
test/first_header_bad_setext.md
Normal file
2
test/first_header_bad_setext.md
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Header {MD002}
|
||||
--------------
|
||||
|
|
@ -12,7 +12,7 @@ function createTestForFile(file) {
|
|||
{ "encoding": "utf8" },
|
||||
function readFileCallback(err, contents) {
|
||||
test.ifError(err);
|
||||
var lines = contents.split(/\r\n|\n/g);
|
||||
var lines = contents.split(/\r\n|\r|\n/g);
|
||||
var results = {};
|
||||
lines.forEach(function forLine(line, lineNum) {
|
||||
var match = line.match(/\{(MD\d+)(?::(\d+))?\}/);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue