mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Update re-implementation of MD044/proper-names to exclude link destinations and references.
This commit is contained in:
parent
4db40256d9
commit
72543a82e7
4 changed files with 99 additions and 16 deletions
29
lib/md044.js
29
lib/md044.js
|
@ -2,8 +2,8 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, forEachLine, newLineRe,
|
||||
forEachInlineCodeSpan } = require("../helpers");
|
||||
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, forEachLine, linkRe,
|
||||
linkReferenceRe, newLineRe, forEachInlineCodeSpan } = require("../helpers");
|
||||
const { lineMetadata } = require("./cache");
|
||||
|
||||
module.exports = {
|
||||
|
@ -17,6 +17,24 @@ module.exports = {
|
|||
const codeBlocks = params.config.code_blocks;
|
||||
const includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
|
||||
const exclusions = [];
|
||||
forEachLine(lineMetadata(), (line, lineIndex) => {
|
||||
if (linkReferenceRe.test(line)) {
|
||||
exclusions.push([ lineIndex, 0, line.length ]);
|
||||
} else {
|
||||
let match = null;
|
||||
while ((match = bareUrlRe.exec(line)) !== null) {
|
||||
exclusions.push([ lineIndex, match.index, match[0].length ]);
|
||||
}
|
||||
while ((match = linkRe.exec(line)) !== null) {
|
||||
const [ , text, destination ] = match;
|
||||
if (destination) {
|
||||
exclusions.push(
|
||||
[ lineIndex, match.index + text.length, destination.length ]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
if (!includeCodeBlocks) {
|
||||
forEachInlineCodeSpan(
|
||||
params.lines.join("\n"),
|
||||
|
@ -34,8 +52,8 @@ module.exports = {
|
|||
}
|
||||
for (const name of names) {
|
||||
const escapedName = escapeForRegExp(name);
|
||||
const startNamePattern = /^\W/.test(name) ? "" : "[^\\s([\"]*\\b_*";
|
||||
const endNamePattern = /\W$/.test(name) ? "" : "_*\\b[^\\s)\\]\"]*";
|
||||
const startNamePattern = /^\W/.test(name) ? "" : "\\b_*";
|
||||
const endNamePattern = /\W$/.test(name) ? "" : "_*\\b";
|
||||
const namePattern =
|
||||
`(${startNamePattern})(${escapedName})${endNamePattern}`;
|
||||
const nameRe = new RegExp(namePattern, "gi");
|
||||
|
@ -43,11 +61,10 @@ module.exports = {
|
|||
if (includeCodeBlocks || (!inCode && !onFence)) {
|
||||
let match = null;
|
||||
while ((match = nameRe.exec(line)) !== null) {
|
||||
const [ fullMatch, leftMatch, nameMatch ] = match;
|
||||
const [ , leftMatch, nameMatch ] = match;
|
||||
const index = match.index + leftMatch.length;
|
||||
const length = nameMatch.length;
|
||||
if (
|
||||
(fullMatch.search(bareUrlRe) === -1) &&
|
||||
exclusions.every((span) => (
|
||||
(lineIndex !== span[0]) ||
|
||||
(index + length < span[1]) ||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue