2022-01-26 00:21:08 +01:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2023-08-04 20:53:38 -07:00
|
|
|
const { addError, addErrorDetailIf } = require("../helpers");
|
2023-09-02 12:07:14 -07:00
|
|
|
const { filterByTypes, getHtmlTagInfo } = require("../helpers/micromark.cjs");
|
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
|
|
|
*
|
2023-08-04 20:53:38 -07:00
|
|
|
* @param {Object} headingText Heading text token.
|
2022-04-10 05:37:57 +00:00
|
|
|
* @returns {string} Fragment string for heading.
|
2022-01-26 00:21:08 +01:00
|
|
|
*/
|
2023-08-04 20:53:38 -07:00
|
|
|
function convertHeadingToHTMLFragment(headingText) {
|
|
|
|
const inlineText =
|
|
|
|
filterByTypes(headingText.children, [ "codeTextData", "data" ])
|
|
|
|
.map((token) => token.text)
|
|
|
|
.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) {
|
2023-08-04 20:53:38 -07:00
|
|
|
const { tokens } = params.parsers.micromark;
|
2022-04-21 21:02:46 -07:00
|
|
|
const fragments = new Map();
|
2023-08-04 20:53:38 -07:00
|
|
|
|
2022-04-20 21:27:04 -07:00
|
|
|
// Process headings
|
2023-08-04 20:53:38 -07:00
|
|
|
const headingTexts = filterByTypes(
|
|
|
|
tokens,
|
|
|
|
[ "atxHeadingText", "setextHeadingText" ]
|
|
|
|
);
|
|
|
|
for (const headingText of headingTexts) {
|
|
|
|
const fragment = convertHeadingToHTMLFragment(headingText);
|
2022-04-21 21:02:46 -07:00
|
|
|
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;
|
2023-08-04 20:53:38 -07:00
|
|
|
while ((match = anchorRe.exec(headingText.text)) !== null) {
|
2023-07-08 22:14:00 -07:00
|
|
|
const [ , anchor ] = match;
|
|
|
|
if (!fragments.has(anchor)) {
|
|
|
|
fragments.set(anchor, 1);
|
|
|
|
}
|
|
|
|
}
|
2023-08-04 20:53:38 -07:00
|
|
|
}
|
|
|
|
|
2022-04-20 21:27:04 -07:00
|
|
|
// Process HTML anchors
|
2023-09-02 12:07:14 -07:00
|
|
|
for (const token of filterByTypes(tokens, [ "htmlText" ])) {
|
2023-08-04 20:53:38 -07:00
|
|
|
const htmlTagInfo = getHtmlTagInfo(token);
|
|
|
|
if (htmlTagInfo && !htmlTagInfo.close) {
|
|
|
|
const anchorMatch = idRe.exec(token.text) ||
|
|
|
|
(htmlTagInfo.name.toLowerCase() === "a" && nameRe.exec(token.text));
|
2022-07-28 00:42:05 -04:00
|
|
|
if (anchorMatch) {
|
|
|
|
fragments.set(`#${anchorMatch[1]}`, 0);
|
2022-04-10 05:37:57 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-04 20:53:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process link and definition fragments
|
|
|
|
const parentChilds = [
|
|
|
|
[ "link", "resourceDestinationString" ],
|
|
|
|
[ "definition", "definitionDestinationString" ]
|
|
|
|
];
|
|
|
|
for (const [ parentType, definitionType ] of parentChilds) {
|
|
|
|
const links = filterByTypes(tokens, [ parentType ]);
|
|
|
|
for (const link of links) {
|
|
|
|
const definitions = filterByTypes(link.children, [ definitionType ]);
|
|
|
|
for (const definition of definitions) {
|
|
|
|
if (
|
|
|
|
(definition.text.length > 1) &&
|
|
|
|
definition.text.startsWith("#") &&
|
|
|
|
!fragments.has(definition.text)
|
|
|
|
) {
|
2023-08-04 21:23:43 -07:00
|
|
|
// eslint-disable-next-line no-undef-init
|
|
|
|
let context = undefined;
|
2023-08-04 20:53:38 -07:00
|
|
|
// eslint-disable-next-line no-undef-init
|
|
|
|
let range = undefined;
|
|
|
|
// eslint-disable-next-line no-undef-init
|
|
|
|
let fixInfo = undefined;
|
|
|
|
if (link.startLine === link.endLine) {
|
2023-08-04 21:23:43 -07:00
|
|
|
context = link.text;
|
2023-08-04 20:53:38 -07:00
|
|
|
range = [ link.startColumn, link.endColumn - link.startColumn ];
|
|
|
|
fixInfo = {
|
|
|
|
"editColumn": definition.startColumn,
|
|
|
|
"deleteCount": definition.endColumn - definition.startColumn
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const definitionTextLower = definition.text.toLowerCase();
|
|
|
|
const mixedCaseKey = [ ...fragments.keys() ]
|
|
|
|
.find((key) => definitionTextLower === key.toLowerCase());
|
|
|
|
if (mixedCaseKey) {
|
|
|
|
// @ts-ignore
|
|
|
|
(fixInfo || {}).insertText = mixedCaseKey;
|
|
|
|
addErrorDetailIf(
|
|
|
|
onError,
|
|
|
|
link.startLine,
|
|
|
|
mixedCaseKey,
|
|
|
|
definition.text,
|
|
|
|
undefined,
|
2023-08-04 21:23:43 -07:00
|
|
|
context,
|
2023-08-04 20:53:38 -07:00
|
|
|
range,
|
|
|
|
fixInfo
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
addError(
|
|
|
|
onError,
|
|
|
|
link.startLine,
|
|
|
|
undefined,
|
2023-08-04 21:23:43 -07:00
|
|
|
context,
|
2023-08-04 20:53:38 -07:00
|
|
|
range
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 00:21:08 +01:00
|
|
|
}
|
2022-04-10 05:37:57 +00:00
|
|
|
}
|
2023-08-04 20:53:38 -07:00
|
|
|
}
|
2022-01-26 00:21:08 +01:00
|
|
|
}
|
|
|
|
};
|