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
|
|
|
|
2025-10-11 16:36:47 -07:00
|
|
|
/** @typedef {import("micromark-extension-gfm-table")} */
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line jsdoc/reject-any-type
|
|
|
|
|
const toLowerCaseStringArray = (/** @type {any} */ arr) => Array.isArray(arr) ? arr.map((elm) => String(elm).toLowerCase()) : [];
|
|
|
|
|
|
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) {
|
2025-10-11 16:36:47 -07:00
|
|
|
const allowedElements = toLowerCaseStringArray(params.config.allowed_elements);
|
|
|
|
|
// If not defined, use allowedElements for backward compatibility
|
|
|
|
|
const tableAllowedElements = toLowerCaseStringArray(params.config.table_allowed_elements || params.config.allowed_elements);
|
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-10-11 16:36:47 -07:00
|
|
|
if (htmlTagInfo && !htmlTagInfo.close) {
|
|
|
|
|
const elementName = htmlTagInfo?.name.toLowerCase();
|
|
|
|
|
const inTable = !!getParentOfType(token, [ "table" ]);
|
|
|
|
|
if (
|
|
|
|
|
(inTable || !allowedElements.includes(elementName)) &&
|
|
|
|
|
(!inTable || !tableAllowedElements.includes(elementName))
|
|
|
|
|
) {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
};
|