Update MD037/no-space-in-emphasis to handle more scenarios (refs #286).

This commit is contained in:
David Anson 2020-05-10 17:06:07 -07:00
parent 37f1d6b64b
commit f607a49a5b
3 changed files with 101 additions and 28 deletions

View file

@ -6,8 +6,8 @@ const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine } =
require("../helpers");
const { lineMetadata } = require("./cache");
const emphasisRe = /(^|[^\\])(?:(\*\*?\*?)|(__?_?))/g;
const asteriskListItemMarkerRe = /^(\s*)\*(\s+)/;
const emphasisRe = /(^|[^\\]|\\\\)(?:(\*\*?\*?)|(__?_?))/g;
const asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/;
const leftSpaceRe = /^\s+/;
const rightSpaceRe = /\s+$/;
@ -101,31 +101,38 @@ module.exports = {
emphasisLength = matchLength;
emphasisKind = matchKind;
effectiveEmphasisLength = matchLength;
} else if (
(matchLength === effectiveEmphasisLength) &&
(matchKind === emphasisKind)
) {
// Ending an existing run, report any pending error
if (pendingError) {
addErrorContext(...pendingError);
pendingError = null;
} else if (matchKind === emphasisKind) {
// Matching emphasis markers
if (matchLength === effectiveEmphasisLength) {
// Ending an existing run, report any pending error
if (pendingError) {
addErrorContext(...pendingError);
pendingError = null;
}
const error = handleRunEnd(
line, lineIndex, effectiveEmphasisLength, match, matchIndex);
if (error) {
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;
}
const error = handleRunEnd(
line, lineIndex, effectiveEmphasisLength, match, matchIndex);
if (error) {
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;
// Back up one character so RegExp has a chance to match the
// next marker (ex: "**star**_underscore_")
emphasisRe.lastIndex--;
} else {
// Upgrade to internal run (1->3 or 2->3)
effectiveEmphasisLength += matchLength;
// Back up one character so RegExp has a chance to match the
// mis-matched marker (ex: "*text_*")
emphasisRe.lastIndex--;
}
}
if (emphasisIndex !== -1) {