This commit is contained in:
David Anson 2025-11-25 16:20:19 -08:00
parent eb83b49845
commit aab5833166

View file

@ -4,10 +4,8 @@ import { addErrorDetailIf } from "../helpers/helpers.cjs";
import { filterByTypesCached, getReferenceLinkImageData } from "./cache.mjs"; import { filterByTypesCached, getReferenceLinkImageData } from "./cache.mjs";
import { addRangeToSet, getDescendantsByType } from "../helpers/micromark-helpers.cjs"; import { addRangeToSet, getDescendantsByType } from "../helpers/micromark-helpers.cjs";
const longLineRePrefix = "^.{"; // Regular expression for a line that is not wrappable
const longLineRePostfixRelaxed = "}.*\\s.*$"; const notWrappableRe = /^(?:[#>\s]*\s)?\S*$/;
const longLineRePostfixStrict = "}.+$";
const sternModeRe = /^(?:[#>\s]*\s)?\S*$/;
/** @typedef {import("micromark-extension-gfm-autolink-literal")} */ /** @typedef {import("micromark-extension-gfm-autolink-literal")} */
/** @typedef {import("micromark-extension-gfm-table")} */ /** @typedef {import("micromark-extension-gfm-table")} */
@ -20,20 +18,10 @@ export default {
"parser": "micromark", "parser": "micromark",
"function": function MD013(params, onError) { "function": function MD013(params, onError) {
const lineLength = Number(params.config.line_length || 80); const lineLength = Number(params.config.line_length || 80);
const headingLineLength = const headingLineLength = Number(params.config.heading_line_length || lineLength);
Number(params.config.heading_line_length || lineLength); const codeLineLength = Number(params.config.code_block_line_length || lineLength);
const codeLineLength =
Number(params.config.code_block_line_length || lineLength);
const strict = !!params.config.strict; const strict = !!params.config.strict;
const stern = !!params.config.stern; const stern = !!params.config.stern;
const longLineRePostfix =
(strict || stern) ? longLineRePostfixStrict : longLineRePostfixRelaxed;
const longLineRe =
new RegExp(longLineRePrefix + lineLength + longLineRePostfix);
const longHeadingLineRe =
new RegExp(longLineRePrefix + headingLineLength + longLineRePostfix);
const longCodeLineRe =
new RegExp(longLineRePrefix + codeLineLength + longLineRePostfix);
const codeBlocks = params.config.code_blocks; const codeBlocks = params.config.code_blocks;
const includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks; const includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
const tables = params.config.tables; const tables = params.config.tables;
@ -75,28 +63,25 @@ export default {
const isHeading = headingLineNumbers.has(lineNumber); const isHeading = headingLineNumbers.has(lineNumber);
const inCode = codeBlockLineNumbers.has(lineNumber); const inCode = codeBlockLineNumbers.has(lineNumber);
const inTable = tableLineNumbers.has(lineNumber); const inTable = tableLineNumbers.has(lineNumber);
const length = inCode ? const maxLength = inCode ? codeLineLength : (isHeading ? headingLineLength : lineLength);
codeLineLength : const text = (strict || stern) ? line : line.replace(/\S*$/u, "");
(isHeading ? headingLineLength : lineLength); if ((maxLength > 0) &&
const lengthRe = inCode ? (includeCodeBlocks || !inCode) &&
longCodeLineRe :
(isHeading ? longHeadingLineRe : longLineRe);
if ((includeCodeBlocks || !inCode) &&
(includeTables || !inTable) && (includeTables || !inTable) &&
(includeHeadings || !isHeading) && (includeHeadings || !isHeading) &&
!definitionLineIndices.has(lineIndex) && !definitionLineIndices.has(lineIndex) &&
(strict || (strict ||
(!(stern && sternModeRe.test(line)) && (!(stern && notWrappableRe.test(line)) &&
!linkOnlyLineNumbers.has(lineNumber))) && !linkOnlyLineNumbers.has(lineNumber))) &&
lengthRe.test(line)) { (text.length > maxLength)) {
addErrorDetailIf( addErrorDetailIf(
onError, onError,
lineNumber, lineNumber,
length, maxLength,
line.length, line.length,
undefined, undefined,
undefined, undefined,
[ length + 1, line.length - length ] [ maxLength + 1, line.length - maxLength ]
); );
} }
} }