mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 14:00:13 +01:00
Update MD031/blanks-around-fences and MD032/blanks-around-lists to ignore comments and blockquotes (fixes #171, fixes #172).
This commit is contained in:
parent
df2507f030
commit
1db87ef0c6
5 changed files with 120 additions and 16 deletions
12
lib/md031.js
12
lib/md031.js
|
|
@ -3,17 +3,19 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const shared = require("./shared");
|
const shared = require("./shared");
|
||||||
|
const { addErrorContext, forEachLine, isBlankLine } = shared;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": [ "MD031", "blanks-around-fences" ],
|
"names": [ "MD031", "blanks-around-fences" ],
|
||||||
"description": "Fenced code blocks should be surrounded by blank lines",
|
"description": "Fenced code blocks should be surrounded by blank lines",
|
||||||
"tags": [ "code", "blank_lines" ],
|
"tags": [ "code", "blank_lines" ],
|
||||||
"function": function MD031(params, onError) {
|
"function": function MD031(params, onError) {
|
||||||
const lines = params.lines;
|
const { lines } = params;
|
||||||
shared.forEachLine(function forLine(line, i, inCode, onFence) {
|
const { length } = lines;
|
||||||
if (((onFence > 0) && (i - 1 >= 0) && lines[i - 1].length) ||
|
forEachLine(function forLine(line, i, inCode, onFence) {
|
||||||
((onFence < 0) && (i + 1 < lines.length) && lines[i + 1].length)) {
|
if (((onFence > 0) && (i - 1 >= 0) && !isBlankLine(lines[i - 1])) ||
|
||||||
shared.addErrorContext(onError, i + 1, lines[i].trim());
|
((onFence < 0) && (i + 1 < length) && !isBlankLine(lines[i + 1]))) {
|
||||||
|
addErrorContext(onError, i + 1, lines[i].trim());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
20
lib/md032.js
20
lib/md032.js
|
|
@ -3,24 +3,22 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const shared = require("./shared");
|
const shared = require("./shared");
|
||||||
|
const { addErrorContext, flattenLists, isBlankLine } = shared;
|
||||||
const blankLineRe = /^[\s>]*$/;
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": [ "MD032", "blanks-around-lists" ],
|
"names": [ "MD032", "blanks-around-lists" ],
|
||||||
"description": "Lists should be surrounded by blank lines",
|
"description": "Lists should be surrounded by blank lines",
|
||||||
"tags": [ "bullet", "ul", "ol", "blank_lines" ],
|
"tags": [ "bullet", "ul", "ol", "blank_lines" ],
|
||||||
"function": function MD032(params, onError) {
|
"function": function MD032(params, onError) {
|
||||||
shared.flattenLists().filter((list) => !list.nesting).forEach((list) => {
|
const { lines } = params;
|
||||||
const firstLineIndex = list.open.map[0];
|
flattenLists().filter((list) => !list.nesting).forEach((list) => {
|
||||||
if (!blankLineRe.test(params.lines[firstLineIndex - 1] || "")) {
|
const firstIndex = list.open.map[0];
|
||||||
shared.addErrorContext(
|
if (!isBlankLine(lines[firstIndex - 1])) {
|
||||||
onError, firstLineIndex + 1, params.lines[firstLineIndex].trim());
|
addErrorContext(onError, firstIndex + 1, lines[firstIndex].trim());
|
||||||
}
|
}
|
||||||
const bottomLineIndex = list.lastLineIndex - 1;
|
const lastIndex = list.lastLineIndex - 1;
|
||||||
if (!blankLineRe.test(params.lines[bottomLineIndex + 1] || "")) {
|
if (!isBlankLine(lines[lastIndex + 1])) {
|
||||||
shared.addErrorContext(
|
addErrorContext(onError, lastIndex + 1, lines[lastIndex].trim());
|
||||||
onError, bottomLineIndex + 1, params.lines[bottomLineIndex].trim());
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,13 @@ module.exports.isEmptyString = function isEmptyString(str) {
|
||||||
return str.length === 0;
|
return str.length === 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Returns true iff the input line is blank (no content)
|
||||||
|
// Example: Contains nothing, whitespace, or comments
|
||||||
|
const blankLineRe = />|(?:<!--.*?-->)/g;
|
||||||
|
module.exports.isBlankLine = function isBlankLine(line) {
|
||||||
|
return !line || !line.trim() || !line.replace(blankLineRe, "").trim();
|
||||||
|
};
|
||||||
|
|
||||||
// Replaces the text of all properly-formatted HTML comments with whitespace
|
// Replaces the text of all properly-formatted HTML comments with whitespace
|
||||||
// This preserves the line/column information for the rest of the document
|
// This preserves the line/column information for the rest of the document
|
||||||
// Trailing whitespace is avoided with a '\' character in the last column
|
// Trailing whitespace is avoided with a '\' character in the last column
|
||||||
|
|
|
||||||
61
test/blanks-around.md
Normal file
61
test/blanks-around.md
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
# Blanks Around
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## MD022/blanks-around-headings
|
||||||
|
|
||||||
|
>
|
||||||
|
### Alpha {MD022}
|
||||||
|
> >
|
||||||
|
|
||||||
|
<!-- comment -->
|
||||||
|
### Beta {MD022}
|
||||||
|
<!-- comments --><!-- comments -->
|
||||||
|
|
||||||
|
> Text
|
||||||
|
>
|
||||||
|
> ### Gamma
|
||||||
|
> >
|
||||||
|
> > Text
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## MD031/blanks-around-fences
|
||||||
|
|
||||||
|
> >
|
||||||
|
```js
|
||||||
|
console.log();
|
||||||
|
```
|
||||||
|
>
|
||||||
|
|
||||||
|
<!-- prettier-ignore -->
|
||||||
|
```js
|
||||||
|
console.log();
|
||||||
|
```
|
||||||
|
<!-- prettier-ignore -->
|
||||||
|
|
||||||
|
> Text
|
||||||
|
>
|
||||||
|
> ```js
|
||||||
|
> console.log();
|
||||||
|
> ```
|
||||||
|
> >
|
||||||
|
> >Text
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## MD032/blanks-around-lists
|
||||||
|
|
||||||
|
>
|
||||||
|
- List item
|
||||||
|
>>
|
||||||
|
|
||||||
|
<!--comments--><!-- comments -->
|
||||||
|
- List item
|
||||||
|
<!--comment-->
|
||||||
|
|
||||||
|
> Text
|
||||||
|
>
|
||||||
|
> - List item
|
||||||
|
>>
|
||||||
|
>> Text
|
||||||
|
|
@ -1396,6 +1396,42 @@ function clearHtmlCommentTextEmbedded(test) {
|
||||||
test.done();
|
test.done();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.isBlankLine = function isBlankLine(test) {
|
||||||
|
test.expect(25);
|
||||||
|
const blankLines = [
|
||||||
|
null,
|
||||||
|
"",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
"\t\t\t",
|
||||||
|
"\r",
|
||||||
|
"\n",
|
||||||
|
"\t\r\n",
|
||||||
|
" <!-- text --> ",
|
||||||
|
"<!--text-->",
|
||||||
|
"<!---->",
|
||||||
|
"<!-- text -->\t<!-- text -->",
|
||||||
|
">",
|
||||||
|
"> ",
|
||||||
|
"> > > \t",
|
||||||
|
"> <!--text-->",
|
||||||
|
">><!--text-->"
|
||||||
|
];
|
||||||
|
blankLines.forEach((line) => test.ok(shared.isBlankLine(line), line));
|
||||||
|
const nonBlankLines = [
|
||||||
|
"text",
|
||||||
|
" text ",
|
||||||
|
".",
|
||||||
|
"> .",
|
||||||
|
"<!--text--> text",
|
||||||
|
"<!--->",
|
||||||
|
"<!--",
|
||||||
|
"-->"
|
||||||
|
];
|
||||||
|
nonBlankLines.forEach((line) => test.ok(!shared.isBlankLine(line), line));
|
||||||
|
test.done();
|
||||||
|
};
|
||||||
|
|
||||||
module.exports.trimLeftRight = function trimLeftRight(test) {
|
module.exports.trimLeftRight = function trimLeftRight(test) {
|
||||||
const inputs = [
|
const inputs = [
|
||||||
"text text",
|
"text text",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue