mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Refactor MD051/link-fragments slightly to reduce dependencies.
This commit is contained in:
parent
bd92ec3fea
commit
8c5f28c2f0
2 changed files with 47 additions and 56 deletions
|
|
@ -4395,8 +4395,7 @@ module.exports = {
|
|||
"use strict";
|
||||
// @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 _b = __webpack_require__(/*! ./cache */ "../lib/cache.js"), codeBlockAndSpanRanges = _b.codeBlockAndSpanRanges, lineMetadata = _b.lineMetadata;
|
||||
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;
|
||||
// Regular expression for identifying HTML anchor names
|
||||
var identifierRe = /(?:id|name)\s*=\s*['"]?([^'"\s>]+)/iu;
|
||||
/**
|
||||
|
|
@ -4423,42 +4422,39 @@ module.exports = {
|
|||
"tags": ["links"],
|
||||
"function": function MD051(params, onError) {
|
||||
var fragments = new Set();
|
||||
// Process headings
|
||||
forEachHeading(params, function (heading, content, inline) {
|
||||
fragments.add(convertHeadingToHTMLFragment(inline));
|
||||
});
|
||||
var exclusions = codeBlockAndSpanRanges();
|
||||
forEachLine(lineMetadata(), function (line, lineIndex, inCode) {
|
||||
// Process HTML anchors
|
||||
var processHtmlToken = function (token) {
|
||||
var match = null;
|
||||
// eslint-disable-next-line no-unmodified-loop-condition
|
||||
while (!inCode && ((match = htmlElementRe.exec(line)) !== null)) {
|
||||
while ((match = htmlElementRe.exec(token.content)) !== null) {
|
||||
var tag = match[0], element = match[2];
|
||||
if ((element.toLowerCase() === "a") &&
|
||||
!overlapsAnyRange(exclusions, lineIndex, match.index, match[0].length)) {
|
||||
if (element.toLowerCase() === "a") {
|
||||
var idMatch = identifierRe.exec(tag);
|
||||
if (idMatch) {
|
||||
fragments.add("#".concat(idMatch[1]));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
filterTokens(params, "inline", function (token) {
|
||||
for (var _i = 0, _a = token.children; _i < _a.length; _i++) {
|
||||
var child = _a[_i];
|
||||
var attrs = child.attrs, lineNumber = child.lineNumber, line = child.line, type = child.type;
|
||||
if (type === "link_open") {
|
||||
var href = attrs.find(function (attr) { return attr[0] === "href"; });
|
||||
var id = href && href[1];
|
||||
if (id && (id.length > 1) && (id[0] === "#") && !fragments.has(id)) {
|
||||
var context = id;
|
||||
var range = null;
|
||||
var match = line.match(new RegExp("\\[.*?\\]\\(".concat(escapeForRegExp(context), "\\)")));
|
||||
if (match) {
|
||||
context = match[0];
|
||||
range = [match.index + 1, match[0].length];
|
||||
}
|
||||
addError(onError, lineNumber, null, context, range);
|
||||
}
|
||||
};
|
||||
filterTokens(params, "html_block", processHtmlToken);
|
||||
forEachInlineChild(params, "html_inline", processHtmlToken);
|
||||
// Process link fragments
|
||||
forEachInlineChild(params, "link_open", function (token) {
|
||||
var attrs = token.attrs, lineNumber = token.lineNumber, line = token.line;
|
||||
var href = attrs.find(function (attr) { return attr[0] === "href"; });
|
||||
var id = href && href[1];
|
||||
if (id && (id.length > 1) && (id[0] === "#") && !fragments.has(id)) {
|
||||
var context = id;
|
||||
var range = null;
|
||||
var match = line.match(new RegExp("\\[.*?\\]\\(".concat(escapeForRegExp(context), "\\)")));
|
||||
if (match) {
|
||||
context = match[0];
|
||||
range = [match.index + 1, match[0].length];
|
||||
}
|
||||
addError(onError, lineNumber, null, context, range);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
55
lib/md051.js
55
lib/md051.js
|
|
@ -2,9 +2,8 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addError, escapeForRegExp, filterTokens, forEachLine, forEachHeading,
|
||||
htmlElementRe, overlapsAnyRange } = require("../helpers");
|
||||
const { codeBlockAndSpanRanges, lineMetadata } = require("./cache");
|
||||
const { addError, escapeForRegExp, filterTokens, forEachInlineChild,
|
||||
forEachHeading, htmlElementRe } = require("../helpers");
|
||||
|
||||
// Regular expression for identifying HTML anchor names
|
||||
const identifierRe = /(?:id|name)\s*=\s*['"]?([^'"\s>]+)/iu;
|
||||
|
|
@ -39,45 +38,41 @@ module.exports = {
|
|||
"tags": [ "links" ],
|
||||
"function": function MD051(params, onError) {
|
||||
const fragments = new Set();
|
||||
// Process headings
|
||||
forEachHeading(params, (heading, content, inline) => {
|
||||
fragments.add(convertHeadingToHTMLFragment(inline));
|
||||
});
|
||||
const exclusions = codeBlockAndSpanRanges();
|
||||
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
|
||||
// Process HTML anchors
|
||||
const processHtmlToken = (token) => {
|
||||
let match = null;
|
||||
// eslint-disable-next-line no-unmodified-loop-condition
|
||||
while (!inCode && ((match = htmlElementRe.exec(line)) !== null)) {
|
||||
while ((match = htmlElementRe.exec(token.content)) !== null) {
|
||||
const [ tag, , element ] = match;
|
||||
if (
|
||||
(element.toLowerCase() === "a") &&
|
||||
!overlapsAnyRange(exclusions, lineIndex, match.index, match[0].length)
|
||||
) {
|
||||
if (element.toLowerCase() === "a") {
|
||||
const idMatch = identifierRe.exec(tag);
|
||||
if (idMatch) {
|
||||
fragments.add(`#${idMatch[1]}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
filterTokens(params, "inline", (token) => {
|
||||
for (const child of token.children) {
|
||||
const { attrs, lineNumber, line, type } = child;
|
||||
if (type === "link_open") {
|
||||
const href = attrs.find((attr) => attr[0] === "href");
|
||||
const id = href && href[1];
|
||||
if (id && (id.length > 1) && (id[0] === "#") && !fragments.has(id)) {
|
||||
let context = id;
|
||||
let range = null;
|
||||
const match = line.match(
|
||||
new RegExp(`\\[.*?\\]\\(${escapeForRegExp(context)}\\)`)
|
||||
);
|
||||
if (match) {
|
||||
context = match[0];
|
||||
range = [ match.index + 1, match[0].length ];
|
||||
}
|
||||
addError(onError, lineNumber, null, context, range);
|
||||
}
|
||||
};
|
||||
filterTokens(params, "html_block", processHtmlToken);
|
||||
forEachInlineChild(params, "html_inline", processHtmlToken);
|
||||
// Process link fragments
|
||||
forEachInlineChild(params, "link_open", (token) => {
|
||||
const { attrs, lineNumber, line } = token;
|
||||
const href = attrs.find((attr) => attr[0] === "href");
|
||||
const id = href && href[1];
|
||||
if (id && (id.length > 1) && (id[0] === "#") && !fragments.has(id)) {
|
||||
let context = id;
|
||||
let range = null;
|
||||
const match = line.match(
|
||||
new RegExp(`\\[.*?\\]\\(${escapeForRegExp(context)}\\)`)
|
||||
);
|
||||
if (match) {
|
||||
context = match[0];
|
||||
range = [ match.index + 1, match[0].length ];
|
||||
}
|
||||
addError(onError, lineNumber, null, context, range);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue