Update MD049/emphasis-style and MD050/strong-style to not report intraword asterisks/underscores as violations because exchanging either alters meaning (fixes #789).

This commit is contained in:
David Anson 2023-05-25 02:38:18 +00:00
parent 7005a8a438
commit 0006636f75
8 changed files with 77 additions and 22 deletions

View file

@ -5,10 +5,13 @@
const { addError, emphasisOrStrongStyleFor } = require("../helpers");
const { filterByTypes, tokenIfType } = require("../helpers/micromark.cjs");
const intrawordRe = /\w/;
const impl =
(params, onError, type, asterisk, underline, style = "consistent") => {
const { lines, parsers } = params;
const emphasisTokens =
filterByTypes(params.parsers.micromark.tokens, [ type ]);
filterByTypes(parsers.micromark.tokens, [ type ]);
for (const token of emphasisTokens) {
const { children } = token;
const childType = `${type}Sequence`;
@ -20,19 +23,29 @@ const impl =
style = markupStyle;
}
if (style !== markupStyle) {
for (const sequence of [ startSequence, endSequence ]) {
addError(
onError,
sequence.startLine,
`Expected: ${style}; Actual: ${markupStyle}`,
undefined,
[ sequence.startColumn, sequence.text.length ],
{
"editColumn": sequence.startColumn,
"deleteCount": sequence.text.length,
"insertText": (style === "asterisk") ? asterisk : underline
}
);
const underscoreIntraword = (style === "underscore") && (
intrawordRe.test(
lines[startSequence.startLine - 1][startSequence.startColumn - 2]
) ||
intrawordRe.test(
lines[endSequence.endLine - 1][endSequence.endColumn - 1]
)
);
if (!underscoreIntraword) {
for (const sequence of [ startSequence, endSequence ]) {
addError(
onError,
sequence.startLine,
`Expected: ${style}; Actual: ${markupStyle}`,
undefined,
[ sequence.startColumn, sequence.text.length ],
{
"editColumn": sequence.startColumn,
"deleteCount": sequence.text.length,
"insertText": (style === "asterisk") ? asterisk : underline
}
);
}
}
}
}