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

@ -18,10 +18,6 @@ const inlineCommentStartRe =
/(<!--\s*markdownlint-(disable|enable|capture|restore|disable-file|enable-file|disable-line|disable-next-line|configure-file))(?:\s|-->)/gi;
module.exports.inlineCommentStartRe = inlineCommentStartRe;
// Regular expression for blockquote prefixes
const blockquotePrefixRe = /^[>\s]*/;
module.exports.blockquotePrefixRe = blockquotePrefixRe;
// Regular expression for link reference definitions
const linkReferenceDefinitionRe = /^ {0,3}\[([^\]]*[^\\])\]:/;
module.exports.linkReferenceDefinitionRe = linkReferenceDefinitionRe;
@ -346,34 +342,6 @@ function addErrorContext(
}
module.exports.addErrorContext = addErrorContext;
/**
* Adds an error object with context for a construct missing a blank line.
*
* @param {Object} onError RuleOnError instance.
* @param {string[]} lines Lines of Markdown content.
* @param {number} lineIndex Line index of line.
* @param {number} [lineNumber] Line number for override.
* @returns {void}
*/
function addErrorContextForLine(onError, lines, lineIndex, lineNumber) {
const line = lines[lineIndex];
// @ts-ignore
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
addErrorContext(
onError,
lineIndex + 1,
line.trim(),
undefined,
undefined,
undefined,
{
lineNumber,
"insertText": `${quotePrefix}\n`
}
);
}
module.exports.addErrorContextForLine = addErrorContextForLine;
/**
* Defines a range within a file (start line/column to end line/column, subset of MicromarkToken).
*

View file

@ -133,6 +133,25 @@ function filterByTypes(tokens, types, htmlFlow) {
return filterByPredicate(tokens, predicate);
}
/**
* Gets the blockquote prefix text (if any) for the specified line number.
*
* @param {Token[]} tokens Micromark tokens.
* @param {number} lineNumber Line number to examine.
* @param {number} [count] Number of times to repeat.
* @returns {string} Blockquote prefix text.
*/
function getBlockQuotePrefixText(tokens, lineNumber, count = 1) {
return filterByTypes(tokens, [ "blockQuotePrefix", "linePrefix" ])
.filter((prefix) => prefix.startLine === lineNumber)
.map((prefix) => prefix.text)
.join("")
.trimEnd()
// eslint-disable-next-line unicorn/prefer-spread
.concat("\n")
.repeat(count);
};
/**
* Gets a list of nested Micromark token descendants by type path.
*
@ -267,6 +286,7 @@ module.exports = {
addRangeToSet,
filterByPredicate,
filterByTypes,
getBlockQuotePrefixText,
getDescendantsByType,
getHeadingLevel,
getHeadingStyle,