2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2023-10-18 23:20:19 -07:00
|
|
|
const { addError, nextLinesRe } = require("../helpers");
|
2023-09-02 12:07:14 -07:00
|
|
|
const { filterByTypes, getHtmlTagInfo } =
|
2023-08-04 20:53:38 -07:00
|
|
|
require("../helpers/micromark.cjs");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("./markdownlint").Rule */
|
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());
|
2024-02-28 21:01:23 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("../helpers/micromark.cjs").Token[] */
|
|
|
|
const micromarkTokens =
|
|
|
|
// @ts-ignore
|
|
|
|
params.parsers.micromark.tokens;
|
|
|
|
for (const token of filterByTypes(micromarkTokens, [ "htmlText" ])) {
|
2023-08-04 20:53:38 -07:00
|
|
|
const htmlTagInfo = getHtmlTagInfo(token);
|
|
|
|
if (
|
|
|
|
htmlTagInfo &&
|
|
|
|
!htmlTagInfo.close &&
|
|
|
|
!allowedElements.includes(htmlTagInfo.name.toLowerCase())
|
|
|
|
) {
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|