2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2023-01-21 15:41:03 -08:00
|
|
|
const { addError } = require("../helpers");
|
|
|
|
|
const { filterByTypes, parse } = require("../helpers/micromark.cjs");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2023-01-21 15:41:03 -08:00
|
|
|
// eslint-disable-next-line regexp/optimal-quantifier-concatenation
|
|
|
|
|
const htmlTextRe = /^<([^!/\s>]+)[^\r\n>]*>?/;
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
"names": [ "MD033", "no-inline-html" ],
|
|
|
|
|
"description": "Inline HTML",
|
|
|
|
|
"tags": [ "html" ],
|
|
|
|
|
"function": function MD033(params, onError) {
|
2020-01-25 18:40:39 -08:00
|
|
|
let allowedElements = params.config.allowed_elements;
|
|
|
|
|
allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
|
|
|
|
|
allowedElements = allowedElements.map((element) => element.toLowerCase());
|
2023-01-21 15:41:03 -08:00
|
|
|
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 ]
|
|
|
|
|
);
|
2019-06-06 22:02:10 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2023-01-21 15:41:03 -08:00
|
|
|
} 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 ]
|
|
|
|
|
);
|
2019-04-29 22:09:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-21 15:41:03 -08:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
|
};
|