Refactor to remove helpers addErrorContextForLine and blockquotePrefixRe, add micromark helper getBlockQuotePrefixText.

This commit is contained in:
David Anson 2024-10-04 22:41:34 -07:00
parent 29ebb28f10
commit 5182911acc
6 changed files with 170 additions and 152 deletions

View file

@ -2,8 +2,8 @@
"use strict";
const { addErrorDetailIf, blockquotePrefixRe, isBlankLine } = require("../helpers");
const { getHeadingLevel } = require("../helpers/micromark-helpers.cjs");
const { addErrorDetailIf, isBlankLine } = require("../helpers");
const { getBlockQuotePrefixText, getHeadingLevel } = require("../helpers/micromark-helpers.cjs");
const { filterByTypesCached } = require("./cache");
const defaultLines = 1;
@ -21,15 +21,6 @@ const getLinesFunction = (linesParam) => {
return () => lines;
};
const getBlockQuote = (str, count) => (
(str || "")
.match(blockquotePrefixRe)[0]
.trimEnd()
// eslint-disable-next-line unicorn/prefer-spread
.concat("\n")
.repeat(count)
);
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
module.exports = {
@ -41,6 +32,7 @@ module.exports = {
const getLinesAbove = getLinesFunction(params.config.lines_above);
const getLinesBelow = getLinesFunction(params.config.lines_below);
const { lines } = params;
const blockQuotePrefixes = filterByTypesCached([ "blockQuotePrefix", "linePrefix" ]);
for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
const { startLine, endLine } = heading;
const line = lines[startLine - 1].trim();
@ -65,8 +57,9 @@ module.exports = {
line,
undefined,
{
"insertText": getBlockQuote(
lines[startLine - 2],
"insertText": getBlockQuotePrefixText(
blockQuotePrefixes,
startLine - 1,
linesAbove - actualAbove
)
}
@ -94,8 +87,9 @@ module.exports = {
undefined,
{
"lineNumber": endLine + 1,
"insertText": getBlockQuote(
lines[endLine],
"insertText": getBlockQuotePrefixText(
blockQuotePrefixes,
endLine + 1,
linesBelow - actualBelow
)
}