mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-23 09:20:12 +01:00
Convert markdownlint library to an ECMAScript module, replace markdownlint-micromark with micromark, stop publishing (large) markdownlint-browser.js, see https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c for guidance.
This commit is contained in:
parent
191226f070
commit
1e71f6f44e
140 changed files with 1087 additions and 10428 deletions
54
lib/md011.mjs
Normal file
54
lib/md011.mjs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
// @ts-check
|
||||
|
||||
import { addError, hasOverlap } from "../helpers/helpers.cjs";
|
||||
import { addRangeToSet } from "../helpers/micromark-helpers.cjs";
|
||||
import { filterByTypesCached } from "./cache.mjs";
|
||||
|
||||
const reversedLinkRe = /(^|[^\\])\(([^()]+)\)\[([^\]^][^\]]*)\](?!\()/g;
|
||||
|
||||
/** @type {import("./markdownlint.mjs").Rule} */
|
||||
export default {
|
||||
"names": [ "MD011", "no-reversed-links" ],
|
||||
"description": "Reversed link syntax",
|
||||
"tags": [ "links" ],
|
||||
"parser": "micromark",
|
||||
"function": function MD011(params, onError) {
|
||||
const codeBlockLineNumbers = new Set();
|
||||
for (const codeBlock of filterByTypesCached([ "codeFenced", "codeIndented" ])) {
|
||||
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine);
|
||||
}
|
||||
const codeTexts = filterByTypesCached([ "codeText" ]);
|
||||
for (const [ lineIndex, line ] of params.lines.entries()) {
|
||||
const lineNumber = lineIndex + 1;
|
||||
if (!codeBlockLineNumbers.has(lineNumber)) {
|
||||
let match = null;
|
||||
while ((match = reversedLinkRe.exec(line)) !== null) {
|
||||
const [ reversedLink, preChar, linkText, linkDestination ] = match;
|
||||
if (
|
||||
!linkText.endsWith("\\") &&
|
||||
!linkDestination.endsWith("\\")
|
||||
) {
|
||||
const column = match.index + preChar.length + 1;
|
||||
const length = match[0].length - preChar.length;
|
||||
/** @type {import("../helpers/helpers.cjs").FileRange} */
|
||||
const range = { "startLine": lineNumber, "startColumn": column, "endLine": lineNumber, "endColumn": column + length - 1 };
|
||||
if (!codeTexts.some((codeText) => hasOverlap(codeText, range))) {
|
||||
addError(
|
||||
onError,
|
||||
lineNumber,
|
||||
reversedLink.slice(preChar.length),
|
||||
undefined,
|
||||
[ column, length ],
|
||||
{
|
||||
"editColumn": column,
|
||||
"deleteCount": length,
|
||||
"insertText": `[${linkText}](${linkDestination})`
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue