mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Add MD032 with tests, improve infrastructure.
This commit is contained in:
parent
160146ac3a
commit
aef1524308
3 changed files with 111 additions and 1 deletions
|
@ -3,6 +3,14 @@
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var rules = require("./rules");
|
var rules = require("./rules");
|
||||||
|
|
||||||
|
function numberComparison(a, b) {
|
||||||
|
return a - b;
|
||||||
|
}
|
||||||
|
|
||||||
|
function uniqueFilterForSorted(value, index, array) {
|
||||||
|
return (index === 0) || (value > array[index - 1]);
|
||||||
|
}
|
||||||
|
|
||||||
function lintFile(file, options, callback) {
|
function lintFile(file, options, callback) {
|
||||||
fs.readFile(file, { "encoding": "utf8" }, function readFile(err, contents) {
|
fs.readFile(file, { "encoding": "utf8" }, function readFile(err, contents) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -14,7 +22,8 @@ function lintFile(file, options, callback) {
|
||||||
var rule = rules[name];
|
var rule = rules[name];
|
||||||
var errors = rule(lines);
|
var errors = rule(lines);
|
||||||
if (errors.length) {
|
if (errors.length) {
|
||||||
result[name] = errors;
|
errors.sort(numberComparison);
|
||||||
|
result[name] = errors.filter(uniqueFilterForSorted);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
callback(null, result);
|
callback(null, result);
|
||||||
|
|
26
lib/rules.js
26
lib/rules.js
|
@ -26,5 +26,31 @@ module.exports = {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return errors;
|
return errors;
|
||||||
|
},
|
||||||
|
|
||||||
|
"MD032": function MD032(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) {
|
||||||
|
if (!inCode) {
|
||||||
|
var listMarker = line.trim().match(/^([\*\+\-]|(\d+\.))\s/);
|
||||||
|
if (listMarker && !inList && !prevLine.match(/^($|\s)/)) {
|
||||||
|
errors.push(lineNum + 1);
|
||||||
|
} else if (!listMarker && inList && !line.match(/^($|\s)/)) {
|
||||||
|
errors.push(lineNum);
|
||||||
|
}
|
||||||
|
inList = listMarker;
|
||||||
|
}
|
||||||
|
if (line.trim().match(/^(```|~~~)/)) {
|
||||||
|
inCode = !inCode;
|
||||||
|
inList = false;
|
||||||
|
}
|
||||||
|
prevLine = line;
|
||||||
|
});
|
||||||
|
return errors;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
75
test/lists_without_blank_lines.md
Normal file
75
test/lists_without_blank_lines.md
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
* list (on first line)
|
||||||
|
|
||||||
|
text
|
||||||
|
|
||||||
|
* list
|
||||||
|
|
||||||
|
text
|
||||||
|
* list {MD032}
|
||||||
|
text
|
||||||
|
+ list {MD032}
|
||||||
|
text
|
||||||
|
- list {MD032}
|
||||||
|
text
|
||||||
|
1. list {MD032}
|
||||||
|
text
|
||||||
|
|
||||||
|
* list
|
||||||
|
* list {MD032}
|
||||||
|
text
|
||||||
|
|
||||||
|
text
|
||||||
|
10. list {MD032}
|
||||||
|
20. list
|
||||||
|
|
||||||
|
text
|
||||||
|
|
||||||
|
* list
|
||||||
|
* list
|
||||||
|
* list
|
||||||
|
|
||||||
|
text
|
||||||
|
|
||||||
|
* list
|
||||||
|
with hanging indent
|
||||||
|
* list
|
||||||
|
with hanging indent
|
||||||
|
* list
|
||||||
|
with hanging indent
|
||||||
|
|
||||||
|
Note: list without hanging indent violates MD032
|
||||||
|
|
||||||
|
* list
|
||||||
|
|
||||||
|
item with blank lines
|
||||||
|
|
||||||
|
* list
|
||||||
|
|
||||||
|
item with blank lines
|
||||||
|
|
||||||
|
text
|
||||||
|
|
||||||
|
```js
|
||||||
|
/*
|
||||||
|
* code block
|
||||||
|
* not a list
|
||||||
|
*/
|
||||||
|
```
|
||||||
|
|
||||||
|
text
|
||||||
|
|
||||||
|
* list {MD032}
|
||||||
|
``` {MD031}
|
||||||
|
code
|
||||||
|
```
|
||||||
|
|
||||||
|
text
|
||||||
|
|
||||||
|
```
|
||||||
|
code
|
||||||
|
``` {MD031}
|
||||||
|
* list {MD032}
|
||||||
|
|
||||||
|
text
|
||||||
|
|
||||||
|
* list (on last line without newline)
|
Loading…
Add table
Add a link
Reference in a new issue