mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +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";
|
"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,29 +4422,28 @@ 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)) {
|
||||||
|
|
@ -4458,8 +4456,6 @@ module.exports = {
|
||||||
}
|
}
|
||||||
addError(onError, lineNumber, null, context, range);
|
addError(onError, lineNumber, null, context, range);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
31
lib/md051.js
31
lib/md051.js
|
|
@ -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,30 +38,28 @@ 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 { attrs, lineNumber, line } = token;
|
||||||
const href = attrs.find((attr) => attr[0] === "href");
|
const href = attrs.find((attr) => attr[0] === "href");
|
||||||
const id = href && href[1];
|
const id = href && href[1];
|
||||||
if (id && (id.length > 1) && (id[0] === "#") && !fragments.has(id)) {
|
if (id && (id.length > 1) && (id[0] === "#") && !fragments.has(id)) {
|
||||||
|
|
@ -77,8 +74,6 @@ module.exports = {
|
||||||
}
|
}
|
||||||
addError(onError, lineNumber, null, context, range);
|
addError(onError, lineNumber, null, context, range);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue