Skip to content

Commit

Permalink
Update MD022/blanks-around-headings to allow passing -1 for lines_abo…
Browse files Browse the repository at this point in the history
…ve/lines_below to allow any number of blank lines in that direction (fixes #546).
  • Loading branch information
DavidAnson committed Aug 6, 2023
1 parent f079df1 commit 8098410
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 58 deletions.
38 changes: 23 additions & 15 deletions demo/markdownlint-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4177,25 +4177,33 @@ module.exports = {
var startLine = heading.startLine,
endLine = heading.endLine;
var line = lines[startLine - 1].trim();
var actualAbove = 0;
for (var i = 0; i < linesAbove; i++) {
if (isBlankLine(lines[startLine - 2 - i])) {
actualAbove++;

// Check lines above
if (linesAbove >= 0) {
var actualAbove = 0;
for (var i = 0; i < linesAbove; i++) {
if (isBlankLine(lines[startLine - 2 - i])) {
actualAbove++;
}
}
addErrorDetailIf(onError, startLine, linesAbove, actualAbove, "Above", line, null, {
"insertText": getBlockQuote(lines[startLine - 2], linesAbove - actualAbove)
});
}
addErrorDetailIf(onError, startLine, linesAbove, actualAbove, "Above", line, null, {
"insertText": getBlockQuote(lines[startLine - 2], linesAbove - actualAbove)
});
var actualBelow = 0;
for (var _i = 0; _i < linesBelow; _i++) {
if (isBlankLine(lines[endLine + _i])) {
actualBelow++;

// Check lines below
if (linesBelow >= 0) {
var actualBelow = 0;
for (var _i = 0; _i < linesBelow; _i++) {
if (isBlankLine(lines[endLine + _i])) {
actualBelow++;
}
}
addErrorDetailIf(onError, startLine, linesBelow, actualBelow, "Below", line, null, {
"lineNumber": endLine + 1,
"insertText": getBlockQuote(lines[endLine], linesBelow - actualBelow)
});
}
addErrorDetailIf(onError, startLine, linesBelow, actualBelow, "Below", line, null, {
"lineNumber": endLine + 1,
"insertText": getBlockQuote(lines[endLine], linesBelow - actualBelow)
});
}
} catch (err) {
_iterator.e(err);
Expand Down
4 changes: 3 additions & 1 deletion doc-build/md022.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ Some more text
```

The `lines_above` and `lines_below` parameters can be used to specify a
different number of blank lines (including 0) above or below each heading.
different number of blank lines (including `0`) above or below each heading. If
the value `-1` is used for either parameter, any number of blank lines is
allowed.

Note: If `lines_above` or `lines_below` are configured to require more than one
blank line, [MD012/no-multiple-blanks](md012.md) should also be customized.
Expand Down
4 changes: 3 additions & 1 deletion doc/Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,9 @@ Some more text
```

The `lines_above` and `lines_below` parameters can be used to specify a
different number of blank lines (including 0) above or below each heading.
different number of blank lines (including `0`) above or below each heading. If
the value `-1` is used for either parameter, any number of blank lines is
allowed.

Note: If `lines_above` or `lines_below` are configured to require more than one
blank line, [MD012/no-multiple-blanks](md012.md) should also be customized.
Expand Down
4 changes: 3 additions & 1 deletion doc/md022.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ Some more text
```

The `lines_above` and `lines_below` parameters can be used to specify a
different number of blank lines (including 0) above or below each heading.
different number of blank lines (including `0`) above or below each heading. If
the value `-1` is used for either parameter, any number of blank lines is
allowed.

Note: If `lines_above` or `lines_below` are configured to require more than one
blank line, [MD012/no-multiple-blanks](md012.md) should also be customized.
Expand Down
78 changes: 43 additions & 35 deletions lib/md022.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,45 +32,53 @@ module.exports = {
for (const heading of headings) {
const { startLine, endLine } = heading;
const line = lines[startLine - 1].trim();
let actualAbove = 0;
for (let i = 0; i < linesAbove; i++) {
if (isBlankLine(lines[startLine - 2 - i])) {
actualAbove++;

// Check lines above
if (linesAbove >= 0) {
let actualAbove = 0;
for (let i = 0; i < linesAbove; i++) {
if (isBlankLine(lines[startLine - 2 - i])) {
actualAbove++;
}
}
addErrorDetailIf(
onError,
startLine,
linesAbove,
actualAbove,
"Above",
line,
null,
{
"insertText":
getBlockQuote(lines[startLine - 2], linesAbove - actualAbove)
}
);
}
addErrorDetailIf(
onError,
startLine,
linesAbove,
actualAbove,
"Above",
line,
null,
{
"insertText":
getBlockQuote(lines[startLine - 2], linesAbove - actualAbove)
}
);
let actualBelow = 0;
for (let i = 0; i < linesBelow; i++) {
if (isBlankLine(lines[endLine + i])) {
actualBelow++;

// Check lines below
if (linesBelow >= 0) {
let actualBelow = 0;
for (let i = 0; i < linesBelow; i++) {
if (isBlankLine(lines[endLine + i])) {
actualBelow++;
}
}
addErrorDetailIf(
onError,
startLine,
linesBelow,
actualBelow,
"Below",
line,
null,
{
"lineNumber": endLine + 1,
"insertText":
getBlockQuote(lines[endLine], linesBelow - actualBelow)
}
);
}
addErrorDetailIf(
onError,
startLine,
linesBelow,
actualBelow,
"Below",
line,
null,
{
"lineNumber": endLine + 1,
"insertText":
getBlockQuote(lines[endLine], linesBelow - actualBelow)
}
);
}
}
};
4 changes: 2 additions & 2 deletions schema/build-config-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,13 @@ for (const rule of rules) {
"lines_above": {
"description": "Blank lines above heading",
"type": "integer",
"minimum": 0,
"minimum": -1,
"default": 1
},
"lines_below": {
"description": "Blank lines below heading",
"type": "integer",
"minimum": 0,
"minimum": -1,
"default": 1
}
};
Expand Down
4 changes: 2 additions & 2 deletions schema/markdownlint-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,13 @@
"lines_above": {
"description": "Blank lines above heading",
"type": "integer",
"minimum": 0,
"minimum": -1,
"default": 1
},
"lines_below": {
"description": "Blank lines below heading",
"type": "integer",
"minimum": 0,
"minimum": -1,
"default": 1
}
},
Expand Down
33 changes: 33 additions & 0 deletions test/blanks-around-headings--1-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Blanks Around Headings

## Apple

Text
## Banana

Text

## Cherry

Text


## Durian ##

Text

Elderberry {MD022}
------------------
Text

---
## Fig

<!-- markdownlint-configure-file {
"heading-style": false,
"no-multiple-blanks": false,
"blanks-around-headings": {
"lines_above": -1,
"lines_below": 1
}
} -->
35 changes: 35 additions & 0 deletions test/blanks-around-headings-1--1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Blanks Around Headings

## Apple
Text

## Banana

Text

## Cherry


Text

---
## Durian {MD022} ##

Text

---

Elderberry
------------------
Text

## Fig

<!-- markdownlint-configure-file {
"heading-style": false,
"no-multiple-blanks": false,
"blanks-around-headings": {
"lines_above": 1,
"lines_below": -1
}
} -->
2 changes: 1 addition & 1 deletion test/markdownlint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ test("readme", (t) => new Promise((resolve) => {
}));

test("validateJsonUsingConfigSchemaStrict", (t) => {
t.plan(156);
t.plan(158);
const configRe =
/^[\s\S]*<!-- markdownlint-configure-file ([\s\S]*) -->[\s\S]*$/;
const ignoreFiles = new Set([
Expand Down
Loading

0 comments on commit 8098410

Please sign in to comment.