2021-10-24 06:54:58 +02:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
import { addError } from "../helpers/helpers.cjs";
|
|
|
|
import { filterByPredicate, getDescendantsByType } from "../helpers/micromark-helpers.cjs";
|
2021-10-24 06:54:58 +02:00
|
|
|
|
2024-02-19 15:41:10 -08:00
|
|
|
const intrawordRe = /^\w$/;
|
2023-05-25 02:38:18 +00:00
|
|
|
|
2024-10-06 20:59:09 -07:00
|
|
|
/**
|
|
|
|
* Return the string representation of a emphasis or strong markup character.
|
|
|
|
*
|
|
|
|
* @param {string} markup Emphasis or strong string.
|
|
|
|
* @returns {"asterisk" | "underscore"} String representation.
|
|
|
|
*/
|
|
|
|
function emphasisOrStrongStyleFor(markup) {
|
|
|
|
switch (markup[0]) {
|
|
|
|
case "*":
|
|
|
|
return "asterisk";
|
|
|
|
default:
|
|
|
|
return "underscore";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-02-29 23:05:27 -08:00
|
|
|
/**
|
2024-12-03 19:58:28 -08:00
|
|
|
* @param {import("markdownlint").RuleParams} params Rule parameters.
|
|
|
|
* @param {import("markdownlint").RuleOnError} onError Error-reporting callback.
|
2024-11-28 20:36:44 -08:00
|
|
|
* @param {import("micromark-util-types").TokenType} type Token type.
|
|
|
|
* @param {import("micromark-util-types").TokenType} typeSequence Token sequence type.
|
2024-02-29 23:05:27 -08:00
|
|
|
* @param {"*" | "**"} asterisk Asterisk kind.
|
|
|
|
* @param {"_" | "__"} underline Underline kind.
|
|
|
|
* @param {"asterisk" | "consistent" | "underscore"} style Style string.
|
|
|
|
*/
|
2023-05-25 02:50:56 +00:00
|
|
|
const impl =
|
2024-02-29 23:05:27 -08:00
|
|
|
(params, onError, type, typeSequence, asterisk, underline, style = "consistent") => {
|
2024-06-21 21:03:30 -07:00
|
|
|
const { lines, parsers } = params;
|
2023-09-02 12:07:14 -07:00
|
|
|
const emphasisTokens = filterByPredicate(
|
2024-06-21 21:03:30 -07:00
|
|
|
parsers.micromark.tokens,
|
2023-09-02 12:07:14 -07:00
|
|
|
(token) => token.type === type,
|
|
|
|
(token) => ((token.type === "htmlFlow") ? [] : token.children)
|
|
|
|
);
|
2023-05-25 03:47:34 +00:00
|
|
|
for (const token of emphasisTokens) {
|
2024-09-16 20:50:54 -07:00
|
|
|
const sequences = getDescendantsByType(token, [ typeSequence ]);
|
|
|
|
const startSequence = sequences[0];
|
|
|
|
const endSequence = sequences[sequences.length - 1];
|
2023-05-25 03:47:34 +00:00
|
|
|
if (startSequence && endSequence) {
|
|
|
|
const markupStyle = emphasisOrStrongStyleFor(startSequence.text);
|
|
|
|
if (style === "consistent") {
|
|
|
|
style = markupStyle;
|
|
|
|
}
|
|
|
|
if (style !== markupStyle) {
|
2023-05-25 02:38:18 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2023-05-25 02:50:56 +00:00
|
|
|
}
|
2021-11-28 23:18:57 -08:00
|
|
|
}
|
|
|
|
}
|
2023-05-25 03:47:34 +00:00
|
|
|
}
|
2023-05-25 02:50:56 +00:00
|
|
|
};
|
2022-05-03 21:59:49 -07:00
|
|
|
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @type {import("markdownlint").Rule[]} */
|
2024-11-28 20:36:44 -08:00
|
|
|
export default [
|
2022-05-03 21:59:49 -07:00
|
|
|
{
|
|
|
|
"names": [ "MD049", "emphasis-style" ],
|
2023-10-25 20:05:19 -07:00
|
|
|
"description": "Emphasis style",
|
2022-05-03 21:59:49 -07:00
|
|
|
"tags": [ "emphasis" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "micromark",
|
2022-05-03 21:59:49 -07:00
|
|
|
"function": function MD049(params, onError) {
|
2023-05-25 02:50:56 +00:00
|
|
|
return impl(
|
|
|
|
params,
|
|
|
|
onError,
|
2023-05-25 03:47:34 +00:00
|
|
|
"emphasis",
|
2024-02-29 23:05:27 -08:00
|
|
|
"emphasisSequence",
|
2023-05-25 02:50:56 +00:00
|
|
|
"*",
|
|
|
|
"_",
|
|
|
|
params.config.style || undefined
|
|
|
|
);
|
2022-05-03 21:59:49 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"names": [ "MD050", "strong-style" ],
|
2023-10-25 20:05:19 -07:00
|
|
|
"description": "Strong style",
|
2022-05-03 21:59:49 -07:00
|
|
|
"tags": [ "emphasis" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "micromark",
|
2022-05-03 21:59:49 -07:00
|
|
|
"function": function MD050(params, onError) {
|
2023-05-25 02:50:56 +00:00
|
|
|
return impl(
|
|
|
|
params,
|
|
|
|
onError,
|
|
|
|
"strong",
|
2024-02-29 23:05:27 -08:00
|
|
|
"strongSequence",
|
2023-05-25 02:50:56 +00:00
|
|
|
"**",
|
|
|
|
"__",
|
|
|
|
params.config.style || undefined
|
|
|
|
);
|
2022-05-03 21:59:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|