2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-09-28 14:30:16 -07:00
|
|
|
const { addError, forEachInlineChild, unescapeMarkdown } =
|
|
|
|
require("../helpers");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2019-09-04 22:18:23 -07:00
|
|
|
const reversedLinkRe = /\(([^)]+)\)\[([^\]^][^\]]*)]/g;
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD011", "no-reversed-links" ],
|
|
|
|
"description": "Reversed link syntax",
|
|
|
|
"tags": [ "links" ],
|
|
|
|
"function": function MD011(params, onError) {
|
2019-09-04 22:18:23 -07:00
|
|
|
forEachInlineChild(params, "text", (token) => {
|
|
|
|
const { lineNumber, content } = token;
|
|
|
|
let match = null;
|
|
|
|
while ((match = reversedLinkRe.exec(content)) !== null) {
|
|
|
|
const [ reversedLink, linkText, linkDestination ] = match;
|
2019-09-26 22:29:01 -07:00
|
|
|
const line = params.lines[lineNumber - 1];
|
2019-09-28 14:30:16 -07:00
|
|
|
const column = unescapeMarkdown(line).indexOf(reversedLink) + 1;
|
2019-09-04 22:18:23 -07:00
|
|
|
const length = reversedLink.length;
|
2020-10-14 20:57:04 -07:00
|
|
|
const range = column ? [ column, length ] : null;
|
|
|
|
const fixInfo = column ?
|
|
|
|
{
|
|
|
|
"editColumn": column,
|
|
|
|
"deleteCount": length,
|
|
|
|
"insertText": `[${linkText}](${linkDestination})`
|
|
|
|
} :
|
|
|
|
null;
|
2019-09-04 22:18:23 -07:00
|
|
|
addError(
|
|
|
|
onError,
|
|
|
|
lineNumber,
|
|
|
|
reversedLink,
|
|
|
|
null,
|
2020-10-14 20:57:04 -07:00
|
|
|
range,
|
|
|
|
fixInfo
|
2019-09-04 22:18:23 -07:00
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|