mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Re-implement MD022/blanks-around-headings to ignore comments and blockquotes.
This commit is contained in:
parent
1db87ef0c6
commit
debc08bca1
5 changed files with 16 additions and 35 deletions
30
lib/md022.js
30
lib/md022.js
|
@ -3,36 +3,18 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const shared = require("./shared");
|
const shared = require("./shared");
|
||||||
|
const { addErrorContext, filterTokens, isBlankLine } = shared;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": [ "MD022", "blanks-around-headings", "blanks-around-headers" ],
|
"names": [ "MD022", "blanks-around-headings", "blanks-around-headers" ],
|
||||||
"description": "Headings should be surrounded by blank lines",
|
"description": "Headings should be surrounded by blank lines",
|
||||||
"tags": [ "headings", "headers", "blank_lines" ],
|
"tags": [ "headings", "headers", "blank_lines" ],
|
||||||
"function": function MD022(params, onError) {
|
"function": function MD022(params, onError) {
|
||||||
let prevHeadingLineNumber = 0;
|
const { lines } = params;
|
||||||
let prevMaxLineIndex = -1;
|
filterTokens(params, "heading_open", (token) => {
|
||||||
let needBlankLine = false;
|
const [ topIndex, nextIndex ] = token.map;
|
||||||
params.tokens.forEach(function forToken(token) {
|
if (!isBlankLine(lines[topIndex - 1]) || !isBlankLine(lines[nextIndex])) {
|
||||||
if (token.type === "heading_open") {
|
addErrorContext(onError, topIndex + 1, lines[topIndex].trim());
|
||||||
if ((token.map[0] - prevMaxLineIndex) === 0) {
|
|
||||||
shared.addErrorContext(onError, token.lineNumber,
|
|
||||||
token.line.trim());
|
|
||||||
}
|
|
||||||
} else if (token.type === "heading_close") {
|
|
||||||
needBlankLine = true;
|
|
||||||
}
|
|
||||||
if (token.map) {
|
|
||||||
if (needBlankLine) {
|
|
||||||
if ((token.map[0] - prevMaxLineIndex) === 0) {
|
|
||||||
shared.addErrorContext(onError, prevHeadingLineNumber,
|
|
||||||
params.lines[prevHeadingLineNumber - 1].trim());
|
|
||||||
}
|
|
||||||
needBlankLine = false;
|
|
||||||
}
|
|
||||||
prevMaxLineIndex = Math.max(prevMaxLineIndex, token.map[1]);
|
|
||||||
}
|
|
||||||
if (token.type === "heading_open") {
|
|
||||||
prevHeadingLineNumber = token.lineNumber;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,10 +11,9 @@ module.exports = {
|
||||||
"tags": [ "code", "blank_lines" ],
|
"tags": [ "code", "blank_lines" ],
|
||||||
"function": function MD031(params, onError) {
|
"function": function MD031(params, onError) {
|
||||||
const { lines } = params;
|
const { lines } = params;
|
||||||
const { length } = lines;
|
|
||||||
forEachLine(function forLine(line, i, inCode, onFence) {
|
forEachLine(function forLine(line, i, inCode, onFence) {
|
||||||
if (((onFence > 0) && (i - 1 >= 0) && !isBlankLine(lines[i - 1])) ||
|
if (((onFence > 0) && !isBlankLine(lines[i - 1])) ||
|
||||||
((onFence < 0) && (i + 1 < length) && !isBlankLine(lines[i + 1]))) {
|
((onFence < 0) && !isBlankLine(lines[i + 1]))) {
|
||||||
addErrorContext(onError, i + 1, lines[i].trim());
|
addErrorContext(onError, i + 1, lines[i].trim());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -5,11 +5,11 @@
|
||||||
## MD022/blanks-around-headings
|
## MD022/blanks-around-headings
|
||||||
|
|
||||||
>
|
>
|
||||||
### Alpha {MD022}
|
### Alpha
|
||||||
> >
|
> >
|
||||||
|
|
||||||
<!-- comment -->
|
<!-- comment -->
|
||||||
### Beta {MD022}
|
### Beta
|
||||||
<!-- comments --><!-- comments -->
|
<!-- comments --><!-- comments -->
|
||||||
|
|
||||||
> Text
|
> Text
|
||||||
|
|
|
@ -20,17 +20,17 @@ Text
|
||||||
|
|
||||||
- Item
|
- Item
|
||||||
item
|
item
|
||||||
> # Quoted heading in list {MD025}
|
> # Quoted heading in list {MD022} {MD025}
|
||||||
- Item
|
- Item
|
||||||
item
|
item
|
||||||
> > # Double-quoted heading in list {MD025}
|
> > # Double-quoted heading in list {MD022} {MD025}
|
||||||
- Item
|
- Item
|
||||||
item
|
item
|
||||||
> ## Quoted sub-heading in list
|
> ## Quoted sub-heading in list {MD022}
|
||||||
- Item
|
- Item
|
||||||
- Item
|
- Item
|
||||||
item
|
item
|
||||||
> ## Quoted indented sub-heading in list {MD023}
|
> ## Quoted indented sub-heading in list {MD022} {MD023}
|
||||||
- Item
|
- Item
|
||||||
|
|
||||||
Text
|
Text
|
||||||
|
|
|
@ -16,5 +16,5 @@ Some text
|
||||||
```
|
```
|
||||||
|
|
||||||
* This is another case where MD023 shouldn't be triggered
|
* This is another case where MD023 shouldn't be triggered
|
||||||
# Test {MD023} Valid heading for CommonMark (see section 5.2)
|
# Test {MD022} {MD023} Valid heading for CommonMark (see section 5.2)
|
||||||
# Test {MD023} Also valid heading for CommonMark
|
# Test {MD022} {MD023} Also valid heading for CommonMark
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue