markdownlint/lib/md033.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

// @ts-check
"use strict";
const { addError, nextLinesRe } = require("../helpers");
const { filterByTypes, getHtmlTagInfo } =
require("../helpers/micromark.cjs");
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
module.exports = {
"names": [ "MD033", "no-inline-html" ],
"description": "Inline HTML",
"tags": [ "html" ],
"parser": "micromark",
"function": function MD033(params, onError) {
let allowedElements = params.config.allowed_elements;
allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
allowedElements = allowedElements.map((element) => element.toLowerCase());
// 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" ], true)) {
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
);
}
}
}
};