This commit is contained in:
David Anson 2025-11-27 19:45:22 -08:00
parent d8bf33dde6
commit 4541ee3dd2
17 changed files with 395 additions and 67 deletions

View file

@ -3,6 +3,7 @@
import { addErrorDetailIf } from "../helpers/helpers.cjs";
import { filterByTypesCached, getReferenceLinkImageData } from "./cache.mjs";
import { addRangeToSet, getDescendantsByType } from "../helpers/micromark-helpers.cjs";
import stringWidth from "string-width";
// Regular expression for a line that is not wrappable
const notWrappableRe = /^(?:[#>\s]*\s)?\S*$/;
@ -28,6 +29,7 @@ export default {
const includeTables = (tables === undefined) ? true : !!tables;
const headings = params.config.headings;
const includeHeadings = (headings === undefined) ? true : !!headings;
const wideCharacters = !!params.config.wide_characters;
const headingLineNumbers = new Set();
for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
addRangeToSet(headingLineNumbers, heading.startLine, heading.endLine);
@ -65,6 +67,7 @@ export default {
const inTable = tableLineNumbers.has(lineNumber);
const maxLength = inCode ? codeLineLength : (isHeading ? headingLineLength : lineLength);
const text = (strict || stern) ? line : line.replace(/\S*$/u, "");
const textLength = wideCharacters ? stringWidth(text) : text.length;
if ((maxLength > 0) &&
(includeCodeBlocks || !inCode) &&
(includeTables || !inTable) &&
@ -73,15 +76,15 @@ export default {
(strict ||
(!(stern && notWrappableRe.test(line)) &&
!linkOnlyLineNumbers.has(lineNumber))) &&
(text.length > maxLength)) {
(textLength > maxLength)) {
addErrorDetailIf(
onError,
lineNumber,
maxLength,
line.length,
wideCharacters ? stringWidth(line) : line.length,
undefined,
undefined,
[ maxLength + 1, line.length - maxLength ]
wideCharacters ? undefined : [ maxLength + 1, line.length - maxLength ]
);
}
}