mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Improve handling of nested tags and blocks by MD033/no-inline-html (fixes #179).
This commit is contained in:
parent
44fac78721
commit
4c7ffdd335
7 changed files with 372 additions and 18 deletions
38
lib/md033.js
38
lib/md033.js
|
|
@ -2,10 +2,12 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addError, filterTokens, forEachInlineChild, newLineRe,
|
||||
rangeFromRegExp } = require("../helpers");
|
||||
const { addError, bareUrlRe, forEachLine, unescapeMarkdown } =
|
||||
require("../helpers");
|
||||
const { lineMetadata } = require("./cache");
|
||||
|
||||
const htmlRe = /<[^>]*>/;
|
||||
const htmlElementRe = /<(\w+)(?:[^>]*)?>/g;
|
||||
const linkDestinationRe = /]\(\s*$/;
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD033", "no-inline-html" ],
|
||||
|
|
@ -14,21 +16,21 @@ module.exports = {
|
|||
"function": function MD033(params, onError) {
|
||||
const allowedElements = (params.config.allowed_elements || [])
|
||||
.map((element) => element.toLowerCase());
|
||||
function forToken(token) {
|
||||
token.content.split(newLineRe)
|
||||
.forEach((line, offset) => {
|
||||
const allowed = (line.match(/<[^/\s>!]*/g) || [])
|
||||
.filter((element) => element.length > 1)
|
||||
.map((element) => element.slice(1).toLowerCase())
|
||||
.filter((element) => !allowedElements.includes(element));
|
||||
if (allowed.length) {
|
||||
addError(onError, token.lineNumber + offset,
|
||||
"Element: " + allowed[0], null,
|
||||
rangeFromRegExp(token.line, htmlRe));
|
||||
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
|
||||
let match = null;
|
||||
// eslint-disable-next-line no-unmodified-loop-condition
|
||||
while (!inCode && (match = htmlElementRe.exec(line))) {
|
||||
const [ tag, element ] = match;
|
||||
if (!allowedElements.includes(element.toLowerCase()) &&
|
||||
!tag.endsWith("\\>") && !bareUrlRe.test(tag)) {
|
||||
const prefix = line.substring(0, match.index);
|
||||
if (!linkDestinationRe.test(prefix) &&
|
||||
!unescapeMarkdown(prefix + "<", "_").endsWith("_")) {
|
||||
addError(onError, lineIndex + 1, "Element: " + element,
|
||||
null, [ match.index + 1, tag.length ]);
|
||||
}
|
||||
});
|
||||
}
|
||||
filterTokens(params, "html_block", forToken);
|
||||
forEachInlineChild(params, "html_inline", forToken);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue