2021-10-24 06:54:58 +02:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2023-05-25 03:47:34 +00:00
|
|
|
const { addError, emphasisOrStrongStyleFor } = require("../helpers");
|
2023-09-02 12:07:14 -07:00
|
|
|
const { filterByPredicate, tokenIfType } = require("../helpers/micromark.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-02-29 23:05:27 -08:00
|
|
|
/**
|
|
|
|
* @param {import("./markdownlint").RuleParams} params Rule parameters.
|
|
|
|
* @param {import("./markdownlint").RuleOnError} onError Error-reporting callback.
|
2024-03-04 21:38:18 -08:00
|
|
|
* @param {import("markdownlint-micromark").TokenType} type Token type.
|
|
|
|
* @param {import("markdownlint-micromark").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") => {
|
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("../helpers/micromark.cjs").Token[] */
|
|
|
|
const micromarkTokens =
|
|
|
|
// @ts-ignore
|
|
|
|
params.parsers.micromark.tokens;
|
|
|
|
const { lines } = params;
|
2023-09-02 12:07:14 -07:00
|
|
|
const emphasisTokens = filterByPredicate(
|
2024-02-29 23:05:27 -08:00
|
|
|
micromarkTokens,
|
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) {
|
|
|
|
const { children } = token;
|
2024-02-29 23:05:27 -08:00
|
|
|
const startSequence = tokenIfType(children[0], typeSequence);
|
|
|
|
const endSequence = tokenIfType(children[children.length - 1], typeSequence);
|
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-02-29 23:05:27 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("./markdownlint").Rule[] */
|
2022-05-03 21:59:49 -07:00
|
|
|
module.exports = [
|
|
|
|
{
|
|
|
|
"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
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|