mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20: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
20
lib/md033.js
20
lib/md033.js
|
|
@ -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 ]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue