Refactor MD049/emphasis-style and MD050/strong-style slightly to simplify.

This commit is contained in:
David Anson 2023-05-25 02:50:56 +00:00
parent d5f108b35e
commit a4a38ec9d2
2 changed files with 64 additions and 52 deletions

View file

@ -5,51 +5,52 @@
const { addError, emphasisOrStrongStyleFor, forEachInlineChild,
getNextChildToken, getRangeAndFixInfoIfFound } = require("../helpers");
const impl = (params, onError, tagPrefix, asterisk, underline, style) => {
let lastLineNumber = -1;
const instances = new Map();
forEachInlineChild(params, `${tagPrefix}_open`, (token, parent) => {
const { lineNumber, markup } = token;
const markupStyle = emphasisOrStrongStyleFor(markup);
if (style === "consistent") {
style = markupStyle;
}
if (style !== markupStyle) {
let rangeAndFixInfo = {};
const contentToken = getNextChildToken(
parent, token, "text", `${tagPrefix}_close`
);
if (contentToken) {
const { content } = contentToken;
const actual = `${markup}${content}${markup}`;
const expectedMarkup =
(style === "asterisk") ? asterisk : underline;
const expected = `${expectedMarkup}${content}${expectedMarkup}`;
if (lastLineNumber !== lineNumber) {
lastLineNumber = lineNumber;
instances.clear();
const impl =
(params, onError, tagPrefix, asterisk, underline, style = "consistent") => {
let lastLineNumber = -1;
const instances = new Map();
forEachInlineChild(params, `${tagPrefix}_open`, (token, parent) => {
const { lineNumber, markup } = token;
const markupStyle = emphasisOrStrongStyleFor(markup);
if (style === "consistent") {
style = markupStyle;
}
if (style !== markupStyle) {
let rangeAndFixInfo = {};
const contentToken = getNextChildToken(
parent, token, "text", `${tagPrefix}_close`
);
if (contentToken) {
const { content } = contentToken;
const actual = `${markup}${content}${markup}`;
const expectedMarkup =
(style === "asterisk") ? asterisk : underline;
const expected = `${expectedMarkup}${content}${expectedMarkup}`;
if (lastLineNumber !== lineNumber) {
lastLineNumber = lineNumber;
instances.clear();
}
const instance = (instances.get(expected) || 0) + 1;
instances.set(expected, instance);
rangeAndFixInfo = getRangeAndFixInfoIfFound(
params.lines,
lineNumber - 1,
actual,
expected,
instance
);
}
const instance = (instances.get(expected) || 0) + 1;
instances.set(expected, instance);
rangeAndFixInfo = getRangeAndFixInfoIfFound(
params.lines,
lineNumber - 1,
actual,
expected,
instance
addError(
onError,
lineNumber,
`Expected: ${style}; Actual: ${markupStyle}`,
undefined,
rangeAndFixInfo.range,
rangeAndFixInfo.fixInfo
);
}
addError(
onError,
lineNumber,
`Expected: ${style}; Actual: ${markupStyle}`,
null,
rangeAndFixInfo.range,
rangeAndFixInfo.fixInfo
);
}
});
};
});
};
module.exports = [
{
@ -57,8 +58,14 @@ module.exports = [
"description": "Emphasis style should be consistent",
"tags": [ "emphasis" ],
"function": function MD049(params, onError) {
const style = String(params.config.style || "consistent");
return impl(params, onError, "em", "*", "_", style);
return impl(
params,
onError,
"em",
"*",
"_",
params.config.style || undefined
);
}
},
{
@ -66,8 +73,14 @@ module.exports = [
"description": "Strong style should be consistent",
"tags": [ "emphasis" ],
"function": function MD050(params, onError) {
const style = String(params.config.style || "consistent");
return impl(params, onError, "strong", "**", "__", style);
return impl(
params,
onError,
"strong",
"**",
"__",
params.config.style || undefined
);
}
}
];