Reimplement MD034/no-bare-urls using micromark tokens (fixes #707).

This commit is contained in:
David Anson 2023-02-05 16:58:06 -08:00
parent 64159fa456
commit b990b3ea77
22 changed files with 1495 additions and 947 deletions

View file

@ -3,10 +3,10 @@
"use strict";
const { addError } = require("../helpers");
const { filterByTypes, parse } = require("../helpers/micromark.cjs");
const { filterByTypes, getHtmlTagInfo, parse } =
require("../helpers/micromark.cjs");
// eslint-disable-next-line regexp/optimal-quantifier-concatenation
const htmlTextRe = /^<([^!/\s>]+)[^\r\n>]*>?/;
const nextLinesRe = /[\r\n][\s\S]*$/;
module.exports = {
"names": [ "MD033", "no-inline-html" ],
@ -20,20 +20,25 @@ module.exports = {
let current = null;
while ((current = pending.shift())) {
const [ offset, tokens ] = current;
for (const token of filterByTypes(tokens, "htmlFlow", "htmlText")) {
for (const token of filterByTypes(tokens, [ "htmlFlow", "htmlText" ])) {
if (token.type === "htmlText") {
const match = htmlTextRe.exec(token.text);
if (match) {
const [ tag, element ] = match;
if (!allowedElements.includes(element.toLowerCase())) {
addError(
onError,
token.startLine + offset,
"Element: " + element,
undefined,
[ token.startColumn, tag.length ]
);
}
const htmlTagInfo = getHtmlTagInfo(token);
if (
htmlTagInfo &&
!htmlTagInfo.close &&
!allowedElements.includes(htmlTagInfo.name.toLowerCase())
) {
const range = [
token.startColumn,
token.text.replace(nextLinesRe, "").length
];
addError(
onError,
token.startLine + offset,
"Element: " + htmlTagInfo.name,
undefined,
range
);
}
} else {
// token.type === "htmlFlow"