2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2023-05-30 20:14:02 -07:00
|
|
|
const { addErrorDetailIf, escapeForRegExp, newLineRe, withinAnyRange } =
|
2022-12-18 15:46:19 -08:00
|
|
|
require("../helpers");
|
2023-03-14 21:03:07 -07:00
|
|
|
const { filterByPredicate, filterByTypes, parse } =
|
|
|
|
require("../helpers/micromark.cjs");
|
|
|
|
|
|
|
|
const ignoredChildTypes = new Set(
|
|
|
|
[ "codeFencedFence", "definition", "reference", "resource" ]
|
|
|
|
);
|
2020-06-19 21:08:34 -07:00
|
|
|
|
2018-01-21 21:44:25 -08:00
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD044", "proper-names" ],
|
|
|
|
"description": "Proper names should have the correct capitalization",
|
|
|
|
"tags": [ "spelling" ],
|
|
|
|
"function": function MD044(params, onError) {
|
2020-01-25 18:40:39 -08:00
|
|
|
let names = params.config.names;
|
|
|
|
names = Array.isArray(names) ? names : [];
|
2021-06-12 17:10:59 -07:00
|
|
|
names.sort((a, b) => (b.length - a.length) || a.localeCompare(b));
|
2018-04-27 22:05:34 -07:00
|
|
|
const codeBlocks = params.config.code_blocks;
|
2022-04-25 21:50:33 -07:00
|
|
|
const includeCodeBlocks =
|
|
|
|
(codeBlocks === undefined) ? true : !!codeBlocks;
|
|
|
|
const htmlElements = params.config.html_elements;
|
|
|
|
const includeHtmlElements =
|
|
|
|
(htmlElements === undefined) ? true : !!htmlElements;
|
2023-03-14 21:03:07 -07:00
|
|
|
const scannedTypes = new Set([ "data", "htmlFlowData" ]);
|
|
|
|
if (includeCodeBlocks) {
|
|
|
|
scannedTypes.add("codeFlowValue");
|
|
|
|
scannedTypes.add("codeTextData");
|
2022-04-25 21:50:33 -07:00
|
|
|
}
|
2023-05-30 20:14:02 -07:00
|
|
|
const tokenAdjustments = new Map();
|
2023-03-14 21:03:07 -07:00
|
|
|
const contentTokens =
|
|
|
|
filterByPredicate(
|
|
|
|
params.parsers.micromark.tokens,
|
|
|
|
(token) => scannedTypes.has(token.type),
|
|
|
|
(token) => {
|
|
|
|
let { children } = token;
|
2023-05-30 20:14:02 -07:00
|
|
|
const { startLine, text } = token;
|
2023-03-14 21:03:07 -07:00
|
|
|
if (!includeHtmlElements && (token.type === "htmlFlow")) {
|
2023-05-30 20:14:02 -07:00
|
|
|
if (text.startsWith("<!--")) {
|
|
|
|
// Remove comment content
|
|
|
|
children = [];
|
|
|
|
} else {
|
|
|
|
// Re-parse to get htmlText elements for detailed tokenization
|
|
|
|
const htmlTextLines =
|
|
|
|
`<md044>\n${text}\n</md044>`.split(newLineRe);
|
|
|
|
children = parse(htmlTextLines.join(""));
|
|
|
|
const reTokens = [ ...children ];
|
|
|
|
for (const reToken of reTokens) {
|
|
|
|
tokenAdjustments.set(reToken, {
|
|
|
|
htmlTextLines,
|
|
|
|
startLine
|
|
|
|
});
|
|
|
|
reTokens.push(...reToken.children);
|
|
|
|
}
|
|
|
|
}
|
2023-03-14 21:03:07 -07:00
|
|
|
}
|
|
|
|
return children.filter((t) => !ignoredChildTypes.has(t.type));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const exclusions = [];
|
|
|
|
const autoLinked = new Set();
|
2021-06-12 17:10:59 -07:00
|
|
|
for (const name of names) {
|
2019-03-28 22:06:42 -07:00
|
|
|
const escapedName = escapeForRegExp(name);
|
2021-06-13 13:07:03 -07:00
|
|
|
const startNamePattern = /^\W/.test(name) ? "" : "\\b_*";
|
|
|
|
const endNamePattern = /\W$/.test(name) ? "" : "_*\\b";
|
2020-06-19 21:08:34 -07:00
|
|
|
const namePattern =
|
2021-06-12 17:10:59 -07:00
|
|
|
`(${startNamePattern})(${escapedName})${endNamePattern}`;
|
|
|
|
const nameRe = new RegExp(namePattern, "gi");
|
2023-03-14 21:03:07 -07:00
|
|
|
for (const token of contentTokens) {
|
|
|
|
let match = null;
|
|
|
|
while ((match = nameRe.exec(token.text)) !== null) {
|
|
|
|
const [ , leftMatch, nameMatch ] = match;
|
|
|
|
const index = token.startColumn - 1 + match.index + leftMatch.length;
|
|
|
|
const length = nameMatch.length;
|
|
|
|
const lineIndex = token.startLine - 1;
|
|
|
|
if (
|
|
|
|
!withinAnyRange(exclusions, lineIndex, index, length) &&
|
|
|
|
!names.includes(nameMatch)
|
|
|
|
) {
|
|
|
|
let urlRanges = [];
|
|
|
|
if (!autoLinked.has(token)) {
|
|
|
|
urlRanges = filterByTypes(
|
|
|
|
parse(token.text),
|
|
|
|
[ "literalAutolink" ]
|
|
|
|
).map(
|
|
|
|
(t) => [
|
|
|
|
lineIndex,
|
|
|
|
token.startColumn - 1 + t.startColumn - 1,
|
|
|
|
t.endColumn - t.startColumn
|
|
|
|
]
|
|
|
|
);
|
|
|
|
exclusions.push(...urlRanges);
|
|
|
|
autoLinked.add(token);
|
|
|
|
}
|
|
|
|
if (!withinAnyRange(urlRanges, lineIndex, index, length)) {
|
2023-05-30 20:14:02 -07:00
|
|
|
let lineNumber = token.startLine;
|
|
|
|
let column = index;
|
|
|
|
if (tokenAdjustments.has(token)) {
|
|
|
|
const { htmlTextLines, startLine } =
|
|
|
|
tokenAdjustments.get(token);
|
|
|
|
let lineDelta = 0;
|
|
|
|
while (htmlTextLines[lineDelta].length <= column) {
|
|
|
|
column -= htmlTextLines[lineDelta].length;
|
|
|
|
lineDelta++;
|
|
|
|
}
|
|
|
|
lineNumber = startLine + lineDelta - 1;
|
|
|
|
}
|
|
|
|
column++;
|
2021-06-12 17:10:59 -07:00
|
|
|
addErrorDetailIf(
|
|
|
|
onError,
|
2023-05-30 20:14:02 -07:00
|
|
|
lineNumber,
|
2021-06-12 17:10:59 -07:00
|
|
|
name,
|
|
|
|
nameMatch,
|
|
|
|
null,
|
|
|
|
null,
|
2023-03-14 21:03:07 -07:00
|
|
|
[ column, length ],
|
2021-06-12 17:10:59 -07:00
|
|
|
{
|
2023-03-14 21:03:07 -07:00
|
|
|
"editColumn": column,
|
2021-06-12 17:10:59 -07:00
|
|
|
"deleteCount": length,
|
|
|
|
"insertText": name
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2021-06-12 17:10:59 -07:00
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2021-06-12 17:10:59 -07:00
|
|
|
}
|
2023-03-14 21:03:07 -07:00
|
|
|
exclusions.push([ lineIndex, index, length ]);
|
2020-09-18 20:30:46 -07:00
|
|
|
}
|
2023-03-14 21:03:07 -07:00
|
|
|
}
|
2021-06-12 17:10:59 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|