2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2021-10-21 13:13:42 +02:00
|
|
|
const {
|
|
|
|
|
addError, forEachLine, overlapsAnyRange, unescapeMarkdown
|
|
|
|
|
} = require("../helpers");
|
2021-11-23 04:40:05 +00:00
|
|
|
const { codeBlockAndSpanRanges, lineMetadata } = require("./cache");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2019-07-26 22:34:32 -07:00
|
|
|
const htmlElementRe = /<(([A-Za-z][A-Za-z0-9-]*)(?:\s[^>]*)?)\/?>/g;
|
2019-04-29 22:09:03 -07:00
|
|
|
const linkDestinationRe = /]\(\s*$/;
|
2019-05-08 18:42:26 -07:00
|
|
|
// See https://spec.commonmark.org/0.29/#autolinks
|
|
|
|
|
const emailAddressRe =
|
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
|
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
"names": [ "MD033", "no-inline-html" ],
|
|
|
|
|
"description": "Inline HTML",
|
|
|
|
|
"tags": [ "html" ],
|
|
|
|
|
"function": function MD033(params, onError) {
|
2020-01-25 18:40:39 -08:00
|
|
|
let allowedElements = params.config.allowed_elements;
|
|
|
|
|
allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
|
|
|
|
|
allowedElements = allowedElements.map((element) => element.toLowerCase());
|
2021-11-23 04:40:05 +00:00
|
|
|
const exclusions = codeBlockAndSpanRanges();
|
2019-04-29 22:09:03 -07:00
|
|
|
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
|
|
|
|
|
let match = null;
|
|
|
|
|
// eslint-disable-next-line no-unmodified-loop-condition
|
2019-10-21 22:17:26 -07:00
|
|
|
while (!inCode && ((match = htmlElementRe.exec(line)) !== null)) {
|
2019-05-08 18:42:26 -07:00
|
|
|
const [ tag, content, element ] = match;
|
2021-10-21 13:13:42 +02:00
|
|
|
if (
|
|
|
|
|
!allowedElements.includes(element.toLowerCase()) &&
|
2019-10-21 22:17:26 -07:00
|
|
|
!tag.endsWith("\\>") &&
|
2021-10-21 13:13:42 +02:00
|
|
|
!emailAddressRe.test(content) &&
|
|
|
|
|
!overlapsAnyRange(exclusions, lineIndex, match.index, match[0].length)
|
|
|
|
|
) {
|
2019-04-29 22:09:03 -07:00
|
|
|
const prefix = line.substring(0, match.index);
|
2021-10-21 13:13:42 +02:00
|
|
|
if (!linkDestinationRe.test(prefix)) {
|
2019-06-06 22:02:10 -07:00
|
|
|
const unescaped = unescapeMarkdown(prefix + "<", "_");
|
2021-10-21 13:13:42 +02:00
|
|
|
if (!unescaped.endsWith("_")) {
|
2019-06-06 22:02:10 -07:00
|
|
|
addError(onError, lineIndex + 1, "Element: " + element,
|
|
|
|
|
null, [ match.index + 1, tag.length ]);
|
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2019-04-29 22:09:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
|
};
|