Update MD033/no-inline-html to handle HTML elements in multi-line code spans (fixes #436).

This commit is contained in:
Álvaro Mondéjar Rubio 2021-10-21 13:13:42 +02:00 committed by David Anson
parent ab9e5875a2
commit f7dfd59a5e
3 changed files with 76 additions and 15 deletions

View file

@ -2,12 +2,13 @@
"use strict";
const { addError, forEachLine, unescapeMarkdown } = require("../helpers");
const { lineMetadata } = require("./cache");
const {
addError, forEachLine, overlapsAnyRange, unescapeMarkdown
} = require("../helpers");
const { inlineCodeSpanRanges, lineMetadata } = require("./cache");
const htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^>]*)?)\/?>/g;
const linkDestinationRe = /]\(\s*$/;
const inlineCodeRe = /^[^`]*(`+[^`]+`+[^`]+)*`+[^`]*$/;
// See https://spec.commonmark.org/0.29/#autolinks
const emailAddressRe =
// eslint-disable-next-line max-len
@ -21,19 +22,22 @@ module.exports = {
let allowedElements = params.config.allowed_elements;
allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
allowedElements = allowedElements.map((element) => element.toLowerCase());
const exclusions = inlineCodeSpanRanges();
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
let match = null;
// eslint-disable-next-line no-unmodified-loop-condition
while (!inCode && ((match = htmlElementRe.exec(line)) !== null)) {
const [ tag, content, element ] = match;
if (!allowedElements.includes(element.toLowerCase()) &&
if (
!allowedElements.includes(element.toLowerCase()) &&
!tag.endsWith("\\>") &&
!emailAddressRe.test(content)) {
!emailAddressRe.test(content) &&
!overlapsAnyRange(exclusions, lineIndex, match.index, match[0].length)
) {
const prefix = line.substring(0, match.index);
if (!linkDestinationRe.test(prefix) && !inlineCodeRe.test(prefix)) {
if (!linkDestinationRe.test(prefix)) {
const unescaped = unescapeMarkdown(prefix + "<", "_");
if (!unescaped.endsWith("_") &&
((unescaped + "`").match(/`/g).length % 2)) {
if (!unescaped.endsWith("_")) {
addError(onError, lineIndex + 1, "Element: " + element,
null, [ match.index + 1, tag.length ]);
}