2022-01-26 00:21:08 +01:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2022-12-16 13:50:38 -08:00
|
|
|
const { addError, addErrorDetailIf, escapeForRegExp, filterTokens,
|
|
|
|
forEachInlineChild, forEachHeading, htmlElementRe } = require("../helpers");
|
2022-04-10 05:37:57 +00:00
|
|
|
|
|
|
|
// Regular expression for identifying HTML anchor names
|
2022-07-28 00:42:05 -04:00
|
|
|
const idRe = /\sid\s*=\s*['"]?([^'"\s>]+)/iu;
|
|
|
|
const nameRe = /\sname\s*=\s*['"]?([^'"\s>]+)/iu;
|
2023-07-08 22:14:00 -07:00
|
|
|
const anchorRe = /\{(#[a-z\d]+(?:[-_][a-z\d]+)*)\}/gu;
|
2022-01-26 00:21:08 +01:00
|
|
|
|
|
|
|
/**
|
2022-04-10 05:37:57 +00:00
|
|
|
* Converts a Markdown heading into an HTML fragment according to the rules
|
|
|
|
* used by GitHub.
|
2022-01-26 00:21:08 +01:00
|
|
|
*
|
2022-04-10 05:37:57 +00:00
|
|
|
* @param {Object} inline Inline token for heading.
|
|
|
|
* @returns {string} Fragment string for heading.
|
2022-01-26 00:21:08 +01:00
|
|
|
*/
|
2022-04-10 05:37:57 +00:00
|
|
|
function convertHeadingToHTMLFragment(inline) {
|
2022-07-19 06:29:52 +00:00
|
|
|
const inlineText = inline.children
|
|
|
|
.filter((token) => token.type !== "html_inline")
|
|
|
|
.map((token) => token.content)
|
|
|
|
.join("");
|
2022-04-18 20:59:01 -07:00
|
|
|
return "#" + encodeURIComponent(
|
|
|
|
inlineText
|
|
|
|
.toLowerCase()
|
|
|
|
// RegExp source with Ruby's \p{Word} expanded into its General Categories
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
// https://github.com/gjtorikian/html-pipeline/blob/main/lib/html/pipeline/toc_filter.rb
|
|
|
|
// https://ruby-doc.org/core-3.0.2/Regexp.html
|
|
|
|
.replace(
|
|
|
|
/[^\p{Letter}\p{Mark}\p{Number}\p{Connector_Punctuation}\- ]/gu,
|
|
|
|
""
|
|
|
|
)
|
|
|
|
.replace(/ /gu, "-")
|
|
|
|
);
|
2022-01-26 00:21:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2022-04-10 05:37:57 +00:00
|
|
|
"names": [ "MD051", "link-fragments" ],
|
2022-01-26 00:21:08 +01:00
|
|
|
"description": "Link fragments should be valid",
|
|
|
|
"tags": [ "links" ],
|
|
|
|
"function": function MD051(params, onError) {
|
2022-04-21 21:02:46 -07:00
|
|
|
const fragments = new Map();
|
2022-04-20 21:27:04 -07:00
|
|
|
// Process headings
|
2022-04-10 05:37:57 +00:00
|
|
|
forEachHeading(params, (heading, content, inline) => {
|
2022-04-21 21:02:46 -07:00
|
|
|
const fragment = convertHeadingToHTMLFragment(inline);
|
|
|
|
const count = fragments.get(fragment) || 0;
|
|
|
|
if (count) {
|
|
|
|
fragments.set(`${fragment}-${count}`, 0);
|
|
|
|
}
|
|
|
|
fragments.set(fragment, count + 1);
|
2023-07-08 22:14:00 -07:00
|
|
|
let match = null;
|
|
|
|
while ((match = anchorRe.exec(content)) !== null) {
|
|
|
|
const [ , anchor ] = match;
|
|
|
|
if (!fragments.has(anchor)) {
|
|
|
|
fragments.set(anchor, 1);
|
|
|
|
}
|
|
|
|
}
|
2022-04-10 05:37:57 +00:00
|
|
|
});
|
2022-04-20 21:27:04 -07:00
|
|
|
// Process HTML anchors
|
|
|
|
const processHtmlToken = (token) => {
|
2022-04-10 05:37:57 +00:00
|
|
|
let match = null;
|
2022-04-20 21:27:04 -07:00
|
|
|
while ((match = htmlElementRe.exec(token.content)) !== null) {
|
2022-04-10 05:37:57 +00:00
|
|
|
const [ tag, , element ] = match;
|
2022-07-28 00:42:05 -04:00
|
|
|
const anchorMatch = idRe.exec(tag) ||
|
|
|
|
(element.toLowerCase() === "a" && nameRe.exec(tag));
|
|
|
|
if (anchorMatch) {
|
|
|
|
fragments.set(`#${anchorMatch[1]}`, 0);
|
2022-04-10 05:37:57 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-20 21:27:04 -07:00
|
|
|
};
|
|
|
|
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;
|
2022-12-16 13:50:38 -08:00
|
|
|
let fixInfo = null;
|
2022-04-20 21:27:04 -07:00
|
|
|
const match = line.match(
|
|
|
|
new RegExp(`\\[.*?\\]\\(${escapeForRegExp(context)}\\)`)
|
|
|
|
);
|
|
|
|
if (match) {
|
2022-12-16 13:50:38 -08:00
|
|
|
[ context ] = match;
|
|
|
|
const index = match.index;
|
|
|
|
const length = context.length;
|
|
|
|
range = [ index + 1, length ];
|
|
|
|
fixInfo = {
|
|
|
|
"editColumn": index + (length - id.length),
|
|
|
|
"deleteCount": id.length,
|
|
|
|
"insertText": null
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const idLower = id.toLowerCase();
|
|
|
|
const mixedCaseKey = [ ...fragments.keys() ]
|
|
|
|
.find((key) => idLower === key.toLowerCase());
|
|
|
|
if (mixedCaseKey) {
|
|
|
|
(fixInfo || {}).insertText = mixedCaseKey;
|
|
|
|
addErrorDetailIf(
|
|
|
|
onError,
|
|
|
|
lineNumber,
|
|
|
|
mixedCaseKey,
|
|
|
|
id,
|
|
|
|
undefined,
|
|
|
|
context,
|
|
|
|
range,
|
|
|
|
fixInfo
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
addError(
|
|
|
|
onError,
|
|
|
|
lineNumber,
|
|
|
|
undefined,
|
|
|
|
context,
|
|
|
|
// @ts-ignore
|
|
|
|
range
|
|
|
|
);
|
2022-01-26 00:21:08 +01:00
|
|
|
}
|
2022-04-10 05:37:57 +00:00
|
|
|
}
|
2022-01-26 00:21:08 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|