Reimplement MD051/link-fragments using micromark tokens, report reference link issues for definition and fix when possible, handle reporting multiple violations on the same line better.

This commit is contained in:
David Anson 2023-08-04 20:53:38 -07:00
parent dd73b0ad7f
commit ef1bd286a9
14 changed files with 458 additions and 346 deletions

View file

@ -3,7 +3,8 @@
"use strict";
const { addError } = require("../helpers");
const { filterByTypes, getHtmlTagInfo } = require("../helpers/micromark.cjs");
const { filterByHtmlTokens, getHtmlTagInfo } =
require("../helpers/micromark.cjs");
const nextLinesRe = /[\r\n][\s\S]*$/;
@ -15,34 +16,24 @@ module.exports = {
let allowedElements = params.config.allowed_elements;
allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
allowedElements = allowedElements.map((element) => element.toLowerCase());
const pending = [ params.parsers.micromark.tokens ];
let current = null;
while ((current = pending.shift())) {
const tokens = current;
for (const token of filterByTypes(tokens, [ "htmlFlow", "htmlText" ])) {
if (token.type === "htmlText") {
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,
"Element: " + htmlTagInfo.name,
undefined,
range
);
}
} else {
// token.type === "htmlFlow"
pending.push(token.htmlFlowChildren);
}
for (const token of filterByHtmlTokens(params.parsers.micromark.tokens)) {
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,
"Element: " + htmlTagInfo.name,
undefined,
range
);
}
}
}