Refactor MD051/link-fragments slightly to reduce dependencies.

This commit is contained in:
David Anson 2022-04-20 21:27:04 -07:00
parent bd92ec3fea
commit 8c5f28c2f0
2 changed files with 47 additions and 56 deletions

View file

@ -4395,8 +4395,7 @@ module.exports = {
"use strict"; "use strict";
// @ts-check // @ts-check
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addError = _a.addError, escapeForRegExp = _a.escapeForRegExp, filterTokens = _a.filterTokens, forEachLine = _a.forEachLine, forEachHeading = _a.forEachHeading, htmlElementRe = _a.htmlElementRe, overlapsAnyRange = _a.overlapsAnyRange; var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addError = _a.addError, escapeForRegExp = _a.escapeForRegExp, filterTokens = _a.filterTokens, forEachInlineChild = _a.forEachInlineChild, forEachHeading = _a.forEachHeading, htmlElementRe = _a.htmlElementRe;
var _b = __webpack_require__(/*! ./cache */ "../lib/cache.js"), codeBlockAndSpanRanges = _b.codeBlockAndSpanRanges, lineMetadata = _b.lineMetadata;
// Regular expression for identifying HTML anchor names // Regular expression for identifying HTML anchor names
var identifierRe = /(?:id|name)\s*=\s*['"]?([^'"\s>]+)/iu; var identifierRe = /(?:id|name)\s*=\s*['"]?([^'"\s>]+)/iu;
/** /**
@ -4423,42 +4422,39 @@ module.exports = {
"tags": ["links"], "tags": ["links"],
"function": function MD051(params, onError) { "function": function MD051(params, onError) {
var fragments = new Set(); var fragments = new Set();
// Process headings
forEachHeading(params, function (heading, content, inline) { forEachHeading(params, function (heading, content, inline) {
fragments.add(convertHeadingToHTMLFragment(inline)); fragments.add(convertHeadingToHTMLFragment(inline));
}); });
var exclusions = codeBlockAndSpanRanges(); // Process HTML anchors
forEachLine(lineMetadata(), function (line, lineIndex, inCode) { var processHtmlToken = function (token) {
var match = null; var match = null;
// eslint-disable-next-line no-unmodified-loop-condition while ((match = htmlElementRe.exec(token.content)) !== null) {
while (!inCode && ((match = htmlElementRe.exec(line)) !== null)) {
var tag = match[0], element = match[2]; var tag = match[0], element = match[2];
if ((element.toLowerCase() === "a") && if (element.toLowerCase() === "a") {
!overlapsAnyRange(exclusions, lineIndex, match.index, match[0].length)) {
var idMatch = identifierRe.exec(tag); var idMatch = identifierRe.exec(tag);
if (idMatch) { if (idMatch) {
fragments.add("#".concat(idMatch[1])); fragments.add("#".concat(idMatch[1]));
} }
} }
} }
}); };
filterTokens(params, "inline", function (token) { filterTokens(params, "html_block", processHtmlToken);
for (var _i = 0, _a = token.children; _i < _a.length; _i++) { forEachInlineChild(params, "html_inline", processHtmlToken);
var child = _a[_i]; // Process link fragments
var attrs = child.attrs, lineNumber = child.lineNumber, line = child.line, type = child.type; forEachInlineChild(params, "link_open", function (token) {
if (type === "link_open") { var attrs = token.attrs, lineNumber = token.lineNumber, line = token.line;
var href = attrs.find(function (attr) { return attr[0] === "href"; }); var href = attrs.find(function (attr) { return attr[0] === "href"; });
var id = href && href[1]; var id = href && href[1];
if (id && (id.length > 1) && (id[0] === "#") && !fragments.has(id)) { if (id && (id.length > 1) && (id[0] === "#") && !fragments.has(id)) {
var context = id; var context = id;
var range = null; var range = null;
var match = line.match(new RegExp("\\[.*?\\]\\(".concat(escapeForRegExp(context), "\\)"))); var match = line.match(new RegExp("\\[.*?\\]\\(".concat(escapeForRegExp(context), "\\)")));
if (match) { if (match) {
context = match[0]; context = match[0];
range = [match.index + 1, match[0].length]; range = [match.index + 1, match[0].length];
}
addError(onError, lineNumber, null, context, range);
}
} }
addError(onError, lineNumber, null, context, range);
} }
}); });
} }

View file

@ -2,9 +2,8 @@
"use strict"; "use strict";
const { addError, escapeForRegExp, filterTokens, forEachLine, forEachHeading, const { addError, escapeForRegExp, filterTokens, forEachInlineChild,
htmlElementRe, overlapsAnyRange } = require("../helpers"); forEachHeading, htmlElementRe } = require("../helpers");
const { codeBlockAndSpanRanges, lineMetadata } = require("./cache");
// Regular expression for identifying HTML anchor names // Regular expression for identifying HTML anchor names
const identifierRe = /(?:id|name)\s*=\s*['"]?([^'"\s>]+)/iu; const identifierRe = /(?:id|name)\s*=\s*['"]?([^'"\s>]+)/iu;
@ -39,45 +38,41 @@ module.exports = {
"tags": [ "links" ], "tags": [ "links" ],
"function": function MD051(params, onError) { "function": function MD051(params, onError) {
const fragments = new Set(); const fragments = new Set();
// Process headings
forEachHeading(params, (heading, content, inline) => { forEachHeading(params, (heading, content, inline) => {
fragments.add(convertHeadingToHTMLFragment(inline)); fragments.add(convertHeadingToHTMLFragment(inline));
}); });
const exclusions = codeBlockAndSpanRanges(); // Process HTML anchors
forEachLine(lineMetadata(), (line, lineIndex, inCode) => { const processHtmlToken = (token) => {
let match = null; let match = null;
// eslint-disable-next-line no-unmodified-loop-condition while ((match = htmlElementRe.exec(token.content)) !== null) {
while (!inCode && ((match = htmlElementRe.exec(line)) !== null)) {
const [ tag, , element ] = match; const [ tag, , element ] = match;
if ( if (element.toLowerCase() === "a") {
(element.toLowerCase() === "a") &&
!overlapsAnyRange(exclusions, lineIndex, match.index, match[0].length)
) {
const idMatch = identifierRe.exec(tag); const idMatch = identifierRe.exec(tag);
if (idMatch) { if (idMatch) {
fragments.add(`#${idMatch[1]}`); fragments.add(`#${idMatch[1]}`);
} }
} }
} }
}); };
filterTokens(params, "inline", (token) => { filterTokens(params, "html_block", processHtmlToken);
for (const child of token.children) { forEachInlineChild(params, "html_inline", processHtmlToken);
const { attrs, lineNumber, line, type } = child; // Process link fragments
if (type === "link_open") { forEachInlineChild(params, "link_open", (token) => {
const href = attrs.find((attr) => attr[0] === "href"); const { attrs, lineNumber, line } = token;
const id = href && href[1]; const href = attrs.find((attr) => attr[0] === "href");
if (id && (id.length > 1) && (id[0] === "#") && !fragments.has(id)) { const id = href && href[1];
let context = id; if (id && (id.length > 1) && (id[0] === "#") && !fragments.has(id)) {
let range = null; let context = id;
const match = line.match( let range = null;
new RegExp(`\\[.*?\\]\\(${escapeForRegExp(context)}\\)`) const match = line.match(
); new RegExp(`\\[.*?\\]\\(${escapeForRegExp(context)}\\)`)
if (match) { );
context = match[0]; if (match) {
range = [ match.index + 1, match[0].length ]; context = match[0];
} range = [ match.index + 1, match[0].length ];
addError(onError, lineNumber, null, context, range);
}
} }
addError(onError, lineNumber, null, context, range);
} }
}); });
} }