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

@ -3158,11 +3158,10 @@ module.exports = {
"use strict";
// @ts-check
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addError = _a.addError, forEachLine = _a.forEachLine, unescapeMarkdown = _a.unescapeMarkdown;
var lineMetadata = __webpack_require__(/*! ./cache */ "../lib/cache.js").lineMetadata;
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addError = _a.addError, forEachLine = _a.forEachLine, overlapsAnyRange = _a.overlapsAnyRange, unescapeMarkdown = _a.unescapeMarkdown;
var _b = __webpack_require__(/*! ./cache */ "../lib/cache.js"), inlineCodeSpanRanges = _b.inlineCodeSpanRanges, lineMetadata = _b.lineMetadata;
var htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^>]*)?)\/?>/g;
var linkDestinationRe = /]\(\s*$/;
var inlineCodeRe = /^[^`]*(`+[^`]+`+[^`]+)*`+[^`]*$/;
// See https://spec.commonmark.org/0.29/#autolinks
var emailAddressRe =
// eslint-disable-next-line max-len
@ -3175,6 +3174,7 @@ module.exports = {
var allowedElements = params.config.allowed_elements;
allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
allowedElements = allowedElements.map(function (element) { return element.toLowerCase(); });
var exclusions = inlineCodeSpanRanges();
forEachLine(lineMetadata(), function (line, lineIndex, inCode) {
var match = null;
// eslint-disable-next-line no-unmodified-loop-condition
@ -3182,12 +3182,12 @@ module.exports = {
var tag = match[0], content = match[1], element = match[2];
if (!allowedElements.includes(element.toLowerCase()) &&
!tag.endsWith("\\>") &&
!emailAddressRe.test(content)) {
!emailAddressRe.test(content) &&
!overlapsAnyRange(exclusions, lineIndex, match.index, match[0].length)) {
var prefix = line.substring(0, match.index);
if (!linkDestinationRe.test(prefix) && !inlineCodeRe.test(prefix)) {
if (!linkDestinationRe.test(prefix)) {
var 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]);
}
}

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 ]);
}

View file

@ -22,6 +22,63 @@ Text \` text `<code>` text \` text `<code>` text
Text \`\` text `<code>` text
Text `<code>` text \` text `<code>` text
## Elements in multiple line code spans
Text `code
<element/>`
`code
<element/>`
`code
<element/>` text
Text `code
code
<element/>
<element/>`
``code ``` ```` `
<code>code
</code>``
Text `code
</element>
code` text
Text `code code
code <element>` text
Text `code <element>
code code` text
Text `code code
code <element> code
code code` text
Text ````code code
code <element> code
code code```` text
Text `code code
code <element>` text
text `code code
code code` text
Text `code code
code code` text
text `code code
code <element>` text
Text `code code
code <element>` text
text `code code
code <element>` text
Text `code code
code` text <element> text `code {MD033}
code code` text
## Slash in element name
Text **\<base directory>\another\directory\\<slash/directory>** text