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
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