2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
import { addError, nextLinesRe } from "../helpers/helpers.cjs";
|
2025-05-27 01:32:15 +02:00
|
|
|
import { getHtmlTagInfo, getParentOfType } from "../helpers/micromark-helpers.cjs";
|
2024-11-28 20:36:44 -08:00
|
|
|
import { filterByTypesCached } from "./cache.mjs";
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @type {import("markdownlint").Rule} */
|
2024-11-28 20:36:44 -08:00
|
|
|
export default {
|
2018-01-21 21:44:25 -08:00
|
|
|
"names": [ "MD033", "no-inline-html" ],
|
|
|
|
"description": "Inline HTML",
|
|
|
|
"tags": [ "html" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"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());
|
2025-05-27 01:32:15 +02:00
|
|
|
let tableAllowedElements = params.config.table_allowed_elements;
|
|
|
|
// if not defined, use allowedElements for backward compatibility
|
|
|
|
tableAllowedElements = Array.isArray(tableAllowedElements) ? tableAllowedElements : allowedElements;
|
|
|
|
tableAllowedElements = tableAllowedElements.map((element) => element.toLowerCase());
|
2024-08-24 22:05:16 -07:00
|
|
|
for (const token of filterByTypesCached([ "htmlText" ], true)) {
|
2023-08-04 20:53:38 -07:00
|
|
|
const htmlTagInfo = getHtmlTagInfo(token);
|
2025-05-27 01:32:15 +02:00
|
|
|
const elementName = htmlTagInfo?.name.toLowerCase();
|
|
|
|
const inTable = !!getParentOfType(token, [ "table" ]);
|
2023-08-04 20:53:38 -07:00
|
|
|
if (
|
|
|
|
htmlTagInfo &&
|
2025-05-27 01:32:15 +02:00
|
|
|
!htmlTagInfo.close && !(
|
|
|
|
(!inTable && allowedElements.includes(elementName)) ||
|
|
|
|
(inTable && tableAllowedElements.includes(elementName))
|
|
|
|
)
|
2023-08-04 20:53:38 -07:00
|
|
|
) {
|
|
|
|
const range = [
|
|
|
|
token.startColumn,
|
|
|
|
token.text.replace(nextLinesRe, "").length
|
|
|
|
];
|
|
|
|
addError(
|
|
|
|
onError,
|
|
|
|
token.startLine,
|
|
|
|
"Element: " + htmlTagInfo.name,
|
|
|
|
undefined,
|
|
|
|
range
|
|
|
|
);
|
2019-04-29 22:09:03 -07:00
|
|
|
}
|
2023-01-21 15:41:03 -08:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|