markdownlint/lib/md033.js

59 lines
1.8 KiB
JavaScript
Raw Normal View History

// @ts-check
"use strict";
const { addError } = require("../helpers");
const { filterByTypes, parse } = require("../helpers/micromark.cjs");
// eslint-disable-next-line regexp/optimal-quantifier-concatenation
const htmlTextRe = /^<([^!/\s>]+)[^\r\n>]*>?/;
module.exports = {
"names": [ "MD033", "no-inline-html" ],
"description": "Inline HTML",
"tags": [ "html" ],
"function": function MD033(params, onError) {
let allowedElements = params.config.allowed_elements;
allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
allowedElements = allowedElements.map((element) => element.toLowerCase());
const pending = [ [ 0, params.parsers.micromark ] ];
let current = null;
while ((current = pending.shift())) {
const [ offset, tokens ] = current;
for (const token of filterByTypes(tokens, "htmlFlow", "htmlText")) {
if (token.type === "htmlText") {
const match = htmlTextRe.exec(token.text);
if (match) {
const [ tag, element ] = match;
if (!allowedElements.includes(element.toLowerCase())) {
addError(
onError,
token.startLine + offset,
"Element: " + element,
undefined,
[ token.startColumn, tag.length ]
);
}
}
} else {
// token.type === "htmlFlow"
// Re-parse without "htmlFlow" to get only "htmlText" tokens
const options = {
"extensions": [
{
"disable": {
"null": [ "htmlFlow" ]
}
}
]
};
const flowTokens = parse(token.text, options);
pending.push(
[ token.startLine - 1, flowTokens ]
);
}
}
}
}
};