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,9 @@
"use strict";
const { addErrorContextForLine, isBlankLine } = require("../helpers");
const { filterByPredicate, nonContentTokens } = require("../helpers/micromark-helpers.cjs");
const { addErrorContext, isBlankLine } = require("../helpers");
const { filterByPredicate, getBlockQuotePrefixText, nonContentTokens } = require("../helpers/micromark-helpers.cjs");
const { filterByTypesCached } = require("./cache");
const isList = (token) => (
(token.type === "listOrdered") || (token.type === "listUnordered")
@ -18,6 +19,7 @@ module.exports = {
"parser": "micromark",
"function": function MD032(params, onError) {
const { lines, parsers } = params;
const blockQuotePrefixes = filterByTypesCached([ "blockQuotePrefix", "linePrefix" ]);
// For every top-level list...
const topLevelLists = filterByPredicate(
@ -30,13 +32,18 @@ module.exports = {
for (const list of topLevelLists) {
// Look for a blank line above the list
const firstIndex = list.startLine - 1;
if (!isBlankLine(lines[firstIndex - 1])) {
addErrorContextForLine(
const firstLineNumber = list.startLine;
if (!isBlankLine(lines[firstLineNumber - 2])) {
addErrorContext(
onError,
// @ts-ignore
lines,
firstIndex
firstLineNumber,
lines[firstLineNumber - 1].trim(),
undefined,
undefined,
undefined,
{
"insertText": getBlockQuotePrefixText(blockQuotePrefixes, firstLineNumber)
}
);
}
@ -51,14 +58,19 @@ module.exports = {
}
// Look for a blank line below the list
const lastIndex = endLine - 1;
if (!isBlankLine(lines[lastIndex + 1])) {
addErrorContextForLine(
const lastLineNumber = endLine;
if (!isBlankLine(lines[lastLineNumber])) {
addErrorContext(
onError,
// @ts-ignore
lines,
lastIndex,
lastIndex + 2
lastLineNumber,
lines[lastLineNumber - 1].trim(),
undefined,
undefined,
undefined,
{
"lineNumber": lastLineNumber + 1,
"insertText": getBlockQuotePrefixText(blockQuotePrefixes, lastLineNumber)
}
);
}
}