Update MD040/fenced-code-language to add allowed_languages parameter (fixes #610).

This commit is contained in:
Sam Chen 2022-10-18 03:29:29 +08:00 committed by GitHub
parent c333976a44
commit 01ba757d3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 199 additions and 9 deletions

View file

@ -2,16 +2,30 @@
"use strict";
const { addErrorContext, filterTokens } = require("../helpers");
const { addError, addErrorContext, filterTokens } = require("../helpers");
module.exports = {
"names": [ "MD040", "fenced-code-language" ],
"description": "Fenced code blocks should have a language specified",
"tags": [ "code", "language" ],
"function": function MD040(params, onError) {
let allowed = params.config.allowed_languages;
allowed = Array.isArray(allowed) ?
allowed.map((lang) => lang.toLowerCase()) :
[];
filterTokens(params, "fence", function forToken(token) {
if (!token.info.trim()) {
const lang = token.info.trim();
if (lang === "") {
addErrorContext(onError, token.lineNumber, token.line);
} else if (
allowed.length > 0 &&
!allowed.includes(lang.toLowerCase())
) {
addError(
onError,
token.lineNumber,
`"${lang}" is not allowed`
);
}
});
}