mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 22:40:13 +01:00
Update MD044/proper-names to handle names with non-word-character boundaries better.
This commit is contained in:
parent
de56bc56ed
commit
e696960aab
7 changed files with 360 additions and 16 deletions
35
lib/md044.js
35
lib/md044.js
|
|
@ -5,6 +5,9 @@
|
|||
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, filterTokens,
|
||||
forEachInlineChild, newLineRe } = require("../helpers");
|
||||
|
||||
const startNonWordRe = /^\W/;
|
||||
const endNonWordRe = /\W$/;
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD044", "proper-names" ],
|
||||
"description": "Proper names should have the correct capitalization",
|
||||
|
|
@ -16,7 +19,10 @@ module.exports = {
|
|||
const includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
|
||||
names.forEach((name) => {
|
||||
const escapedName = escapeForRegExp(name);
|
||||
const namePattern = "\\S*\\b(" + escapedName + ")\\b\\S*";
|
||||
const startNamePattern = startNonWordRe.test(name) ? "" : "\\S*\\b";
|
||||
const endNamePattern = endNonWordRe.test(name) ? "" : "\\b\\S*";
|
||||
const namePattern =
|
||||
`(${startNamePattern})(${escapedName})(${endNamePattern})`;
|
||||
const anyNameRe = new RegExp(namePattern, "gi");
|
||||
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||
function forToken(token) {
|
||||
|
|
@ -25,34 +31,31 @@ module.exports = {
|
|||
.forEach((line, index) => {
|
||||
let match = null;
|
||||
while ((match = anyNameRe.exec(line)) !== null) {
|
||||
const fullMatch = match[0];
|
||||
const [ fullMatch, leftMatch, nameMatch, rightMatch ] = match;
|
||||
if (fullMatch.search(bareUrlRe) === -1) {
|
||||
const wordMatch = fullMatch
|
||||
.replace(/^\W*/, "").replace(/\W*$/, "");
|
||||
.replace(new RegExp(`^\\W{0,${leftMatch.length}}`), "")
|
||||
.replace(new RegExp(`\\W{0,${rightMatch.length}}$`), "");
|
||||
if (!names.includes(wordMatch)) {
|
||||
const lineNumber = token.lineNumber + index + fenceOffset;
|
||||
const fullLine = params.lines[lineNumber - 1];
|
||||
let matchIndex = match.index;
|
||||
const matchLength = wordMatch.length;
|
||||
const fullLineWord =
|
||||
fullLine.slice(matchIndex, matchIndex + matchLength);
|
||||
if (fullLineWord !== wordMatch) {
|
||||
// Attempt to fix bad offset due to inline content
|
||||
matchIndex = fullLine.indexOf(wordMatch);
|
||||
}
|
||||
const matchIndex = fullLine.indexOf(wordMatch);
|
||||
const range = (matchIndex === -1) ?
|
||||
null :
|
||||
[ matchIndex + 1, matchLength ];
|
||||
const fixInfo = (matchIndex === -1) ? null : {
|
||||
"editColumn": matchIndex + 1,
|
||||
"deleteCount": matchLength,
|
||||
"insertText": name
|
||||
};
|
||||
const fixInfo = (matchIndex === -1) ?
|
||||
null :
|
||||
{
|
||||
"editColumn": matchIndex + 1,
|
||||
"deleteCount": matchLength,
|
||||
"insertText": name
|
||||
};
|
||||
addErrorDetailIf(
|
||||
onError,
|
||||
lineNumber,
|
||||
name,
|
||||
match[1],
|
||||
nameMatch,
|
||||
null,
|
||||
null,
|
||||
range,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue