mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Reimplement MD040/fenced-code-language using micromark tokens.
This commit is contained in:
parent
f5b5773b50
commit
f26df4743c
5 changed files with 42 additions and 74 deletions
39
lib/md040.js
39
lib/md040.js
|
|
@ -2,7 +2,9 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addError, addErrorContext, filterTokens } = require("../helpers");
|
||||
const { addError, addErrorContext } = require("../helpers");
|
||||
const { filterByTypes, getTokenTextByType, tokenIfType } =
|
||||
require("../helpers/micromark.cjs");
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD040", "fenced-code-language" ],
|
||||
|
|
@ -12,26 +14,21 @@ module.exports = {
|
|||
let allowed = params.config.allowed_languages;
|
||||
allowed = Array.isArray(allowed) ? allowed : [];
|
||||
const languageOnly = !!params.config.language_only;
|
||||
|
||||
filterTokens(params, "fence", function forToken(token) {
|
||||
const lang = token.info.trim().split(/\s+/u).shift();
|
||||
if (lang === "") {
|
||||
addErrorContext(onError, token.lineNumber, token.line);
|
||||
} else if ((allowed.length > 0) && !allowed.includes(lang)) {
|
||||
addError(
|
||||
onError,
|
||||
token.lineNumber,
|
||||
`"${lang}" is not allowed`
|
||||
);
|
||||
const fencedCodes = filterByTypes(params.parsers.micromark.tokens, [ "codeFenced" ]);
|
||||
for (const fencedCode of fencedCodes) {
|
||||
const openingFence = tokenIfType(fencedCode.children[0], "codeFencedFence");
|
||||
if (openingFence) {
|
||||
const { children, startLine, text } = openingFence;
|
||||
const info = getTokenTextByType(children, "codeFencedFenceInfo")
|
||||
if (!info) {
|
||||
addErrorContext(onError, startLine, text);
|
||||
} else if ((allowed.length > 0) && !allowed.includes(info)) {
|
||||
addError(onError, startLine, `"${info}" is not allowed`);
|
||||
}
|
||||
if (languageOnly && getTokenTextByType(children, "codeFencedFenceMeta")) {
|
||||
addError(onError, startLine, `Info string contains more than language: "${text}"`);
|
||||
}
|
||||
}
|
||||
|
||||
if (languageOnly && (token.info !== lang)) {
|
||||
addError(
|
||||
onError,
|
||||
token.lineNumber,
|
||||
`Info string contains more than language: "${token.info}"`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue