Convert markdownlint library to an ECMAScript module, replace markdownlint-micromark with micromark, stop publishing (large) markdownlint-browser.js, see https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c for guidance.

This commit is contained in:
David Anson 2024-11-28 20:36:44 -08:00
parent 191226f070
commit 1e71f6f44e
140 changed files with 1087 additions and 10428 deletions

32
lib/md040.mjs Normal file
View file

@ -0,0 +1,32 @@
// @ts-check
import { addError, addErrorContext } from "../helpers/helpers.cjs";
import { getDescendantsByType } from "../helpers/micromark-helpers.cjs";
import { filterByTypesCached } from "./cache.mjs";
/** @type {import("./markdownlint.mjs").Rule} */
export default {
"names": [ "MD040", "fenced-code-language" ],
"description": "Fenced code blocks should have a language specified",
"tags": [ "code", "language" ],
"parser": "micromark",
"function": function MD040(params, onError) {
let allowed = params.config.allowed_languages;
allowed = Array.isArray(allowed) ? allowed : [];
const languageOnly = !!params.config.language_only;
const fencedCodes = filterByTypesCached([ "codeFenced" ]);
for (const fencedCode of fencedCodes) {
const openingFence = getDescendantsByType(fencedCode, [ "codeFencedFence" ])[0];
const { startLine, text } = openingFence;
const info = getDescendantsByType(openingFence, [ "codeFencedFenceInfo" ])[0]?.text;
if (!info) {
addErrorContext(onError, startLine, text);
} else if ((allowed.length > 0) && !allowed.includes(info)) {
addError(onError, startLine, `"${info}" is not allowed`);
}
if (languageOnly && getDescendantsByType(openingFence, [ "codeFencedFenceMeta" ]).length > 0) {
addError(onError, startLine, `Info string contains more than language: "${text}"`);
}
}
}
};