mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 14:00:13 +01:00
Some checks are pending
Checkers / linkcheck (push) Waiting to run
Checkers / spellcheck (push) Waiting to run
CI / build (20, macos-latest) (push) Waiting to run
CI / build (20, ubuntu-latest) (push) Waiting to run
CI / build (20, windows-latest) (push) Waiting to run
CI / build (22, macos-latest) (push) Waiting to run
CI / build (22, ubuntu-latest) (push) Waiting to run
CI / build (22, windows-latest) (push) Waiting to run
CI / build (24, macos-latest) (push) Waiting to run
CI / build (24, ubuntu-latest) (push) Waiting to run
CI / build (24, windows-latest) (push) Waiting to run
CI / pnpm (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
TestRepos / build (latest, ubuntu-latest) (push) Waiting to run
UpdateTestRepos / update (push) Waiting to run
54 lines
1.9 KiB
JavaScript
54 lines
1.9 KiB
JavaScript
// @ts-check
|
|
|
|
import { addErrorContext, allPunctuation } from "../helpers/helpers.cjs";
|
|
import { getDescendantsByType } from "../helpers/micromark-helpers.cjs";
|
|
import { filterByTypesCached } from "./cache.mjs";
|
|
|
|
/** @typedef {import("markdownlint").MicromarkToken} MicromarkToken */
|
|
/** @typedef {import("markdownlint").MicromarkTokenType} MicromarkTokenType */
|
|
|
|
/** @type {MicromarkTokenType[][]} */
|
|
const emphasisTypes = [
|
|
[ "emphasis", "emphasisText" ],
|
|
[ "strong", "strongText" ]
|
|
];
|
|
|
|
const isParagraphChildMeaningful = (/** @type {MicromarkToken} */ token) => !(
|
|
(token.type === "htmlText") ||
|
|
((token.type === "data") && (token.text.trim().length === 0))
|
|
);
|
|
|
|
/** @type {import("markdownlint").Rule} */
|
|
export default {
|
|
"names": [ "MD036", "no-emphasis-as-heading" ],
|
|
"description": "Emphasis used instead of a heading",
|
|
"tags": [ "headings", "emphasis" ],
|
|
"parser": "micromark",
|
|
"function": function MD036(params, onError) {
|
|
let punctuation = params.config.punctuation;
|
|
punctuation = String((punctuation === undefined) ? allPunctuation : punctuation);
|
|
const punctuationRe = new RegExp("[" + punctuation + "]$");
|
|
const paragraphTokens =
|
|
filterByTypesCached([ "paragraph" ], true)
|
|
.filter((token) =>
|
|
(token.parent?.type === "content") &&
|
|
(
|
|
!token.parent?.parent ||
|
|
((token.parent?.parent?.type === "htmlFlow") && !token.parent?.parent?.parent)
|
|
) &&
|
|
(token.children.filter(isParagraphChildMeaningful).length === 1)
|
|
);
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|