2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
import { addErrorContext, allPunctuation } from "../helpers/helpers.cjs";
|
|
|
|
import { getDescendantsByType } from "../helpers/micromark-helpers.cjs";
|
|
|
|
import { filterByTypesCached } from "./cache.mjs";
|
2024-03-19 21:20:44 -07:00
|
|
|
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @typedef {import("markdownlint").MicromarkTokenType} TokenType */
|
2024-09-18 21:02:59 -07:00
|
|
|
/** @type {TokenType[][]} */
|
|
|
|
const emphasisTypes = [
|
|
|
|
[ "emphasis", "emphasisText" ],
|
|
|
|
[ "strong", "strongText" ]
|
|
|
|
];
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2025-03-30 17:30:12 -07:00
|
|
|
const isParagraphChildMeaningful = (token) => !(
|
|
|
|
(token.type === "htmlText") ||
|
|
|
|
((token.type === "data") && (token.text.trim().length === 0))
|
|
|
|
);
|
|
|
|
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @type {import("markdownlint").Rule} */
|
2024-11-28 20:36:44 -08:00
|
|
|
export default {
|
2023-11-09 20:05:30 -08:00
|
|
|
"names": [ "MD036", "no-emphasis-as-heading" ],
|
2018-03-19 23:39:42 +01:00
|
|
|
"description": "Emphasis used instead of a heading",
|
2023-11-09 20:05:30 -08:00
|
|
|
"tags": [ "headings", "emphasis" ],
|
2024-03-19 21:20:44 -07:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD036(params, onError) {
|
2020-01-25 18:40:39 -08:00
|
|
|
let punctuation = params.config.punctuation;
|
2024-03-19 21:20:44 -07:00
|
|
|
punctuation = String((punctuation === undefined) ? allPunctuation : punctuation);
|
|
|
|
const punctuationRe = new RegExp("[" + punctuation + "]$");
|
|
|
|
const paragraphTokens =
|
2025-03-30 17:30:12 -07:00
|
|
|
filterByTypesCached([ "paragraph" ], true)
|
2024-09-01 16:16:05 -07:00
|
|
|
.filter((token) =>
|
2025-03-30 17:30:12 -07:00
|
|
|
(token.parent?.type === "content") &&
|
|
|
|
(
|
|
|
|
!token.parent?.parent ||
|
|
|
|
((token.parent?.parent?.type === "htmlFlow") && !token.parent?.parent?.parent)
|
|
|
|
) &&
|
|
|
|
(token.children.filter(isParagraphChildMeaningful).length === 1)
|
2024-03-19 21:20:44 -07:00
|
|
|
);
|
2024-09-18 21:02:59 -07:00
|
|
|
for (const emphasisType of emphasisTypes) {
|
|
|
|
const textTokens = getDescendantsByType(paragraphTokens, emphasisType);
|
|
|
|
for (const textToken of textTokens) {
|
|
|
|
if (
|
|
|
|
(textToken.children.length === 1) &&
|
|
|
|
(textToken.children[0].type === "data") &&
|
|
|
|
!punctuationRe.test(textToken.text)
|
|
|
|
) {
|
|
|
|
addErrorContext(onError, textToken.startLine, textToken.text);
|
2024-03-19 21:20:44 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|