mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Reimplement MD022/blanks-around-headings using micromark tokens.
This commit is contained in:
parent
1eb40d3c4c
commit
f079df140c
2 changed files with 60 additions and 43 deletions
40
lib/md022.js
40
lib/md022.js
|
|
@ -2,8 +2,9 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addErrorDetailIf, blockquotePrefixRe, filterTokens, isBlankLine } =
|
||||
const { addErrorDetailIf, blockquotePrefixRe, isBlankLine } =
|
||||
require("../helpers");
|
||||
const { filterByTypes } = require("../helpers/micromark.cjs");
|
||||
|
||||
const getBlockQuote = (str, count) => (
|
||||
(str || "")
|
||||
|
|
@ -23,46 +24,53 @@ module.exports = {
|
|||
linesAbove = Number((linesAbove === undefined) ? 1 : linesAbove);
|
||||
let linesBelow = params.config.lines_below;
|
||||
linesBelow = Number((linesBelow === undefined) ? 1 : linesBelow);
|
||||
const { lines } = params;
|
||||
filterTokens(params, "heading_open", (token) => {
|
||||
const [ topIndex, nextIndex ] = token.map;
|
||||
const { lines, parsers } = params;
|
||||
const headings = filterByTypes(
|
||||
parsers.micromark.tokens,
|
||||
[ "atxHeading", "setextHeading" ]
|
||||
);
|
||||
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[topIndex - i - 1])) {
|
||||
if (isBlankLine(lines[startLine - 2 - i])) {
|
||||
actualAbove++;
|
||||
}
|
||||
}
|
||||
addErrorDetailIf(
|
||||
onError,
|
||||
topIndex + 1,
|
||||
startLine,
|
||||
linesAbove,
|
||||
actualAbove,
|
||||
"Above",
|
||||
lines[topIndex].trim(),
|
||||
line,
|
||||
null,
|
||||
{
|
||||
"insertText":
|
||||
getBlockQuote(lines[topIndex - 1], linesAbove - actualAbove)
|
||||
});
|
||||
getBlockQuote(lines[startLine - 2], linesAbove - actualAbove)
|
||||
}
|
||||
);
|
||||
let actualBelow = 0;
|
||||
for (let i = 0; i < linesBelow; i++) {
|
||||
if (isBlankLine(lines[nextIndex + i])) {
|
||||
if (isBlankLine(lines[endLine + i])) {
|
||||
actualBelow++;
|
||||
}
|
||||
}
|
||||
addErrorDetailIf(
|
||||
onError,
|
||||
topIndex + 1,
|
||||
startLine,
|
||||
linesBelow,
|
||||
actualBelow,
|
||||
"Below",
|
||||
lines[topIndex].trim(),
|
||||
line,
|
||||
null,
|
||||
{
|
||||
"lineNumber": nextIndex + 1,
|
||||
"lineNumber": endLine + 1,
|
||||
"insertText":
|
||||
getBlockQuote(lines[nextIndex], linesBelow - actualBelow)
|
||||
});
|
||||
});
|
||||
getBlockQuote(lines[endLine], linesBelow - actualBelow)
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue