2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2022-10-18 03:29:29 +08:00
|
|
|
const { addError, addErrorContext, filterTokens } = require("../helpers");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD040", "fenced-code-language" ],
|
|
|
|
"description": "Fenced code blocks should have a language specified",
|
|
|
|
"tags": [ "code", "language" ],
|
|
|
|
"function": function MD040(params, onError) {
|
2022-10-18 03:29:29 +08:00
|
|
|
let allowed = params.config.allowed_languages;
|
2022-11-06 01:41:27 +01:00
|
|
|
allowed = Array.isArray(allowed) ? allowed : [];
|
2019-04-13 11:18:57 -07:00
|
|
|
filterTokens(params, "fence", function forToken(token) {
|
2022-11-06 01:41:27 +01:00
|
|
|
const lang = token.info.trim().split(/\s+/u).shift();
|
2022-10-18 03:29:29 +08:00
|
|
|
if (lang === "") {
|
2019-04-13 11:18:57 -07:00
|
|
|
addErrorContext(onError, token.lineNumber, token.line);
|
2022-11-06 01:41:27 +01:00
|
|
|
} else if ((allowed.length > 0) && !allowed.includes(lang)) {
|
2022-10-18 03:29:29 +08:00
|
|
|
addError(
|
|
|
|
onError,
|
|
|
|
token.lineNumber,
|
|
|
|
`"${lang}" is not allowed`
|
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|