MD033/no-inline-html: Add table_allowed_elements parameter.

This commit is contained in:
Alexandre Feblot 2025-05-27 01:32:15 +02:00 committed by David Anson
parent c6e2ee1488
commit 1ac0b22804
16 changed files with 432 additions and 8 deletions

View file

@ -605,6 +605,10 @@ export interface ConfigurationStrict {
* Allowed elements
*/
allowed_elements?: string[];
/**
* Allowed elements in tables
*/
table_allowed_elements?: string[];
};
/**
* MD033/no-inline-html : Inline HTML : https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md033.md
@ -616,6 +620,10 @@ export interface ConfigurationStrict {
* Allowed elements
*/
allowed_elements?: string[];
/**
* Allowed elements in tables
*/
table_allowed_elements?: string[];
};
/**
* MD034/no-bare-urls : Bare URL used : https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md034.md

View file

@ -1,7 +1,7 @@
// @ts-check
import { addError, nextLinesRe } from "../helpers/helpers.cjs";
import { getHtmlTagInfo } from "../helpers/micromark-helpers.cjs";
import { getHtmlTagInfo, getParentOfType } from "../helpers/micromark-helpers.cjs";
import { filterByTypesCached } from "./cache.mjs";
/** @type {import("markdownlint").Rule} */
@ -14,12 +14,20 @@ export default {
let allowedElements = params.config.allowed_elements;
allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
allowedElements = allowedElements.map((element) => element.toLowerCase());
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());
for (const token of filterByTypesCached([ "htmlText" ], true)) {
const htmlTagInfo = getHtmlTagInfo(token);
const elementName = htmlTagInfo?.name.toLowerCase();
const inTable = !!getParentOfType(token, [ "table" ]);
if (
htmlTagInfo &&
!htmlTagInfo.close &&
!allowedElements.includes(htmlTagInfo.name.toLowerCase())
!htmlTagInfo.close && !(
(!inTable && allowedElements.includes(elementName)) ||
(inTable && tableAllowedElements.includes(elementName))
)
) {
const range = [
token.startColumn,