Reimplement MD037/no-space-in-emphasis using micromark tokens, report start/end separately for smaller edit spans, remove markdown-it-texmath (fixes #533, fixes #597).

This commit is contained in:
David Anson 2023-07-24 21:36:55 -07:00
parent e86fb7699d
commit 73b9704159
20 changed files with 1784 additions and 2502 deletions

View file

@ -2,184 +2,97 @@
"use strict";
const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine,
withinAnyRange } = require("../helpers");
const { htmlElementRanges, lineMetadata } = require("./cache");
const { addError } = require("../helpers");
const emphasisRe = /(^|[^\\]|\\\\)(?:(\*{1,3})|(_{1,3}))/g;
const embeddedUnderscoreRe = /([A-Za-z\d])(_([A-Za-z\d]))+/g;
const asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/;
const leftSpaceRe = /^\s+/;
const rightSpaceRe = /\s+$/;
const tablePipeRe = /\|/;
const allUnderscoresRe = /_/g;
const emphasisStartTextRe = /^(\S{1,3})(\s+)\S/;
const emphasisEndTextRe = /\S(\s+)(\S{1,3})$/;
module.exports = {
"names": [ "MD037", "no-space-in-emphasis" ],
"description": "Spaces inside emphasis markers",
"tags": [ "whitespace", "emphasis" ],
"function": function MD037(params, onError) {
const exclusions = htmlElementRanges();
// eslint-disable-next-line init-declarations
let effectiveEmphasisLength, emphasisIndex, emphasisKind, emphasisLength,
pendingError = null;
// eslint-disable-next-line jsdoc/require-jsdoc
function resetRunTracking() {
emphasisIndex = -1;
emphasisLength = 0;
emphasisKind = "";
effectiveEmphasisLength = 0;
pendingError = null;
// Initialize variables
const { lines, parsers } = params;
const emphasisTokensByMarker = new Map();
for (const marker of [ "_", "__", "___", "*", "**", "***" ]) {
emphasisTokensByMarker.set(marker, []);
}
// eslint-disable-next-line jsdoc/require-jsdoc
function handleRunEnd(
line, lineIndex, contextLength, match, matchIndex, inTable
) {
// Close current run
let content = line.substring(emphasisIndex, matchIndex);
if (!emphasisLength) {
content = content.trimStart();
const pending = [ ...parsers.micromark.tokens ];
let token = null;
while ((token = pending.shift())) {
// Use reparsed children of htmlFlow tokens
if (token.type === "htmlFlow") {
pending.unshift(...token.htmlFlowChildren);
continue;
}
if (!match) {
content = content.trimEnd();
pending.push(...token.children);
// Build lists of bare tokens for each emphasis marker type
for (const emphasisTokens of emphasisTokensByMarker.values()) {
emphasisTokens.length = 0;
}
const leftSpace = leftSpaceRe.test(content);
const rightSpace = rightSpaceRe.test(content);
if (
(leftSpace || rightSpace) &&
(!inTable || !tablePipeRe.test(content))
) {
// Report the violation
const contextStart = emphasisIndex - emphasisLength;
const contextEnd = matchIndex + contextLength;
const column = contextStart + 1;
const length = contextEnd - contextStart;
if (!withinAnyRange(exclusions, lineIndex, column, length)) {
const context = line.substring(contextStart, contextEnd);
const leftMarker = line.substring(contextStart, emphasisIndex);
const rightMarker = match ? (match[2] || match[3]) : "";
const fixedText = `${leftMarker}${content.trim()}${rightMarker}`;
return [
onError,
lineIndex + 1,
context,
leftSpace,
rightSpace,
[ column, length ],
{
"editColumn": column,
"deleteCount": length,
"insertText": fixedText
}
];
}
}
return null;
}
// Initialize
const ignoreMarkersByLine = emphasisMarkersInContent(params);
resetRunTracking();
forEachLine(
lineMetadata(),
(line, lineIndex, inCode, onFence, inTable, inItem, onBreak, inMath) => {
const onItemStart = (inItem === 1);
if (
inCode ||
onFence ||
inTable ||
onBreak ||
onItemStart ||
isBlankLine(line)
) {
// Emphasis resets when leaving a block
resetRunTracking();
}
if (
inCode ||
onFence ||
onBreak ||
inMath
) {
// Emphasis has no meaning here
return;
}
let patchedLine = line.replace(
embeddedUnderscoreRe,
(match) => match.replace(allUnderscoresRe, " ")
);
if (onItemStart) {
// Trim overlapping '*' list item marker
patchedLine = patchedLine.replace(asteriskListItemMarkerRe, "$1 $2");
}
let match = null;
// Match all emphasis-looking runs in the line...
while ((match = emphasisRe.exec(patchedLine))) {
const ignoreMarkersForLine = ignoreMarkersByLine[lineIndex];
const matchIndex = match.index + match[1].length;
if (ignoreMarkersForLine.includes(matchIndex)) {
// Ignore emphasis markers inside code spans and links
continue;
for (const child of token.children) {
const { text, type } = child;
if ((type === "data") && (text.length <= 3)) {
const emphasisTokens = emphasisTokensByMarker.get(text);
if (emphasisTokens) {
emphasisTokens.push(child);
}
const matchLength = match[0].length - match[1].length;
const matchKind = (match[2] || match[3])[0];
if (emphasisIndex === -1) {
// New run
emphasisIndex = matchIndex + matchLength;
emphasisLength = matchLength;
emphasisKind = matchKind;
effectiveEmphasisLength = matchLength;
} else if (matchKind === emphasisKind) {
// Matching emphasis markers
if (matchLength === effectiveEmphasisLength) {
// Ending an existing run, report any pending error
if (pendingError) {
// @ts-ignore
addErrorContext(...pendingError);
pendingError = null;
}
const error = handleRunEnd(
line,
lineIndex,
effectiveEmphasisLength,
match,
matchIndex,
inTable
}
}
// Process bare tokens for each emphasis marker type
for (const emphasisTokens of emphasisTokensByMarker.values()) {
for (let i = 0; i + 1 < emphasisTokens.length; i += 2) {
// Process start token of start/end pair
const startToken = emphasisTokens[i];
const startText =
lines[startToken.startLine - 1].slice(startToken.startColumn - 1);
const startMatch = startText.match(emphasisStartTextRe);
if (startMatch) {
const [ startContext, startMarker, startSpaces ] = startMatch;
if ((startMarker === startToken.text) && (startSpaces.length > 0)) {
addError(
onError,
startToken.startLine,
undefined,
startContext,
[ startToken.startColumn, startContext.length ],
{
"editColumn": startToken.endColumn,
"deleteCount": startSpaces.length
}
);
if (error) {
// @ts-ignore
addErrorContext(...error);
}
// Reset
resetRunTracking();
} else if (matchLength === 3) {
// Swap internal run length (1->2 or 2->1)
effectiveEmphasisLength = matchLength - effectiveEmphasisLength;
} else if (effectiveEmphasisLength === 3) {
// Downgrade internal run (3->1 or 3->2)
effectiveEmphasisLength -= matchLength;
} else {
// Upgrade to internal run (1->3 or 2->3)
effectiveEmphasisLength += matchLength;
}
// Back up one character so RegExp has a chance to match the
// next marker (ex: "**star**_underscore_")
if (emphasisRe.lastIndex > 1) {
emphasisRe.lastIndex--;
}
// Process end token of start/end pair
const endToken = emphasisTokens[i + 1];
const endText =
lines[endToken.startLine - 1].slice(0, endToken.endColumn - 1);
const endMatch = endText.match(emphasisEndTextRe);
if (endMatch) {
const [ endContext, endSpace, endMarker ] = endMatch;
if ((endMarker === endToken.text) && (endSpace.length > 0)) {
addError(
onError,
endToken.startLine,
undefined,
endContext,
[ endToken.endColumn - endContext.length, endContext.length ],
{
"editColumn": endToken.startColumn - endSpace.length,
"deleteCount": endSpace.length
}
);
}
} else if (emphasisRe.lastIndex > 1) {
// Back up one character so RegExp has a chance to match the
// mis-matched marker (ex: "*text_*")
emphasisRe.lastIndex--;
}
}
if (emphasisIndex !== -1) {
pendingError = pendingError ||
handleRunEnd(line, lineIndex, 0, null, line.length, inTable);
// Adjust for pending run on new line
emphasisIndex = 0;
emphasisLength = 0;
}
}
);
}
}
};