mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 14:30:12 +01:00
Update MD033/no-inline-html to handle HTML elements in multi-line code spans (fixes #436).
This commit is contained in:
parent
ab9e5875a2
commit
f7dfd59a5e
3 changed files with 76 additions and 15 deletions
|
|
@ -3158,11 +3158,10 @@ module.exports = {
|
||||||
"use strict";
|
"use strict";
|
||||||
// @ts-check
|
// @ts-check
|
||||||
|
|
||||||
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addError = _a.addError, forEachLine = _a.forEachLine, unescapeMarkdown = _a.unescapeMarkdown;
|
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addError = _a.addError, forEachLine = _a.forEachLine, overlapsAnyRange = _a.overlapsAnyRange, unescapeMarkdown = _a.unescapeMarkdown;
|
||||||
var lineMetadata = __webpack_require__(/*! ./cache */ "../lib/cache.js").lineMetadata;
|
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 htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^>]*)?)\/?>/g;
|
||||||
var linkDestinationRe = /]\(\s*$/;
|
var linkDestinationRe = /]\(\s*$/;
|
||||||
var inlineCodeRe = /^[^`]*(`+[^`]+`+[^`]+)*`+[^`]*$/;
|
|
||||||
// See https://spec.commonmark.org/0.29/#autolinks
|
// See https://spec.commonmark.org/0.29/#autolinks
|
||||||
var emailAddressRe =
|
var emailAddressRe =
|
||||||
// eslint-disable-next-line max-len
|
// eslint-disable-next-line max-len
|
||||||
|
|
@ -3175,6 +3174,7 @@ module.exports = {
|
||||||
var allowedElements = params.config.allowed_elements;
|
var allowedElements = params.config.allowed_elements;
|
||||||
allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
|
allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
|
||||||
allowedElements = allowedElements.map(function (element) { return element.toLowerCase(); });
|
allowedElements = allowedElements.map(function (element) { return element.toLowerCase(); });
|
||||||
|
var exclusions = inlineCodeSpanRanges();
|
||||||
forEachLine(lineMetadata(), function (line, lineIndex, inCode) {
|
forEachLine(lineMetadata(), function (line, lineIndex, inCode) {
|
||||||
var match = null;
|
var match = null;
|
||||||
// eslint-disable-next-line no-unmodified-loop-condition
|
// eslint-disable-next-line no-unmodified-loop-condition
|
||||||
|
|
@ -3182,12 +3182,12 @@ module.exports = {
|
||||||
var tag = match[0], content = match[1], element = match[2];
|
var tag = match[0], content = match[1], element = match[2];
|
||||||
if (!allowedElements.includes(element.toLowerCase()) &&
|
if (!allowedElements.includes(element.toLowerCase()) &&
|
||||||
!tag.endsWith("\\>") &&
|
!tag.endsWith("\\>") &&
|
||||||
!emailAddressRe.test(content)) {
|
!emailAddressRe.test(content) &&
|
||||||
|
!overlapsAnyRange(exclusions, lineIndex, match.index, match[0].length)) {
|
||||||
var prefix = line.substring(0, match.index);
|
var prefix = line.substring(0, match.index);
|
||||||
if (!linkDestinationRe.test(prefix) && !inlineCodeRe.test(prefix)) {
|
if (!linkDestinationRe.test(prefix)) {
|
||||||
var unescaped = unescapeMarkdown(prefix + "<", "_");
|
var unescaped = unescapeMarkdown(prefix + "<", "_");
|
||||||
if (!unescaped.endsWith("_") &&
|
if (!unescaped.endsWith("_")) {
|
||||||
((unescaped + "`").match(/`/g).length % 2)) {
|
|
||||||
addError(onError, lineIndex + 1, "Element: " + element, null, [match.index + 1, tag.length]);
|
addError(onError, lineIndex + 1, "Element: " + element, null, [match.index + 1, tag.length]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
20
lib/md033.js
20
lib/md033.js
|
|
@ -2,12 +2,13 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addError, forEachLine, unescapeMarkdown } = require("../helpers");
|
const {
|
||||||
const { lineMetadata } = require("./cache");
|
addError, forEachLine, overlapsAnyRange, unescapeMarkdown
|
||||||
|
} = require("../helpers");
|
||||||
|
const { inlineCodeSpanRanges, lineMetadata } = require("./cache");
|
||||||
|
|
||||||
const htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^>]*)?)\/?>/g;
|
const htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^>]*)?)\/?>/g;
|
||||||
const linkDestinationRe = /]\(\s*$/;
|
const linkDestinationRe = /]\(\s*$/;
|
||||||
const inlineCodeRe = /^[^`]*(`+[^`]+`+[^`]+)*`+[^`]*$/;
|
|
||||||
// See https://spec.commonmark.org/0.29/#autolinks
|
// See https://spec.commonmark.org/0.29/#autolinks
|
||||||
const emailAddressRe =
|
const emailAddressRe =
|
||||||
// eslint-disable-next-line max-len
|
// eslint-disable-next-line max-len
|
||||||
|
|
@ -21,19 +22,22 @@ module.exports = {
|
||||||
let allowedElements = params.config.allowed_elements;
|
let allowedElements = params.config.allowed_elements;
|
||||||
allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
|
allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
|
||||||
allowedElements = allowedElements.map((element) => element.toLowerCase());
|
allowedElements = allowedElements.map((element) => element.toLowerCase());
|
||||||
|
const exclusions = inlineCodeSpanRanges();
|
||||||
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
|
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
|
||||||
let match = null;
|
let match = null;
|
||||||
// eslint-disable-next-line no-unmodified-loop-condition
|
// eslint-disable-next-line no-unmodified-loop-condition
|
||||||
while (!inCode && ((match = htmlElementRe.exec(line)) !== null)) {
|
while (!inCode && ((match = htmlElementRe.exec(line)) !== null)) {
|
||||||
const [ tag, content, element ] = match;
|
const [ tag, content, element ] = match;
|
||||||
if (!allowedElements.includes(element.toLowerCase()) &&
|
if (
|
||||||
|
!allowedElements.includes(element.toLowerCase()) &&
|
||||||
!tag.endsWith("\\>") &&
|
!tag.endsWith("\\>") &&
|
||||||
!emailAddressRe.test(content)) {
|
!emailAddressRe.test(content) &&
|
||||||
|
!overlapsAnyRange(exclusions, lineIndex, match.index, match[0].length)
|
||||||
|
) {
|
||||||
const prefix = line.substring(0, match.index);
|
const prefix = line.substring(0, match.index);
|
||||||
if (!linkDestinationRe.test(prefix) && !inlineCodeRe.test(prefix)) {
|
if (!linkDestinationRe.test(prefix)) {
|
||||||
const unescaped = unescapeMarkdown(prefix + "<", "_");
|
const unescaped = unescapeMarkdown(prefix + "<", "_");
|
||||||
if (!unescaped.endsWith("_") &&
|
if (!unescaped.endsWith("_")) {
|
||||||
((unescaped + "`").match(/`/g).length % 2)) {
|
|
||||||
addError(onError, lineIndex + 1, "Element: " + element,
|
addError(onError, lineIndex + 1, "Element: " + element,
|
||||||
null, [ match.index + 1, tag.length ]);
|
null, [ match.index + 1, tag.length ]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,63 @@ Text \` text `<code>` text \` text `<code>` text
|
||||||
Text \`\` text `<code>` text
|
Text \`\` text `<code>` text
|
||||||
Text `<code>` 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
|
## Slash in element name
|
||||||
|
|
||||||
Text **\<base directory>\another\directory\\<slash/directory>** text
|
Text **\<base directory>\another\directory\\<slash/directory>** text
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue