2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-09-06 22:35:33 -07:00
|
|
|
const { addErrorContext, filterTokens } = require("../helpers");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2021-12-22 14:52:43 -08:00
|
|
|
const spaceInLinkRe =
|
2022-12-19 21:36:24 -08:00
|
|
|
/\[(?:\s[^\]]*|[^\]]*?\s)\](?=(\([^)]*\)|\[[^\]]*\]))/;
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2018-01-21 21:44:25 -08:00
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD039", "no-space-in-links" ],
|
|
|
|
"description": "Spaces inside link text",
|
|
|
|
"tags": [ "whitespace", "links" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "markdownit",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD039(params, onError) {
|
2019-09-06 22:35:33 -07:00
|
|
|
filterTokens(params, "inline", (token) => {
|
2019-09-24 23:00:30 -07:00
|
|
|
const { children } = token;
|
|
|
|
let { lineNumber } = token;
|
2018-04-27 22:05:34 -07:00
|
|
|
let inLink = false;
|
|
|
|
let linkText = "";
|
2019-09-21 21:05:58 -07:00
|
|
|
let lineIndex = 0;
|
2022-06-08 22:10:27 -07:00
|
|
|
for (const child of children) {
|
2022-05-06 21:42:31 -07:00
|
|
|
const { content, markup, type } = child;
|
2019-09-28 22:50:33 -07:00
|
|
|
if (type === "link_open") {
|
2018-01-21 21:44:25 -08:00
|
|
|
inLink = true;
|
|
|
|
linkText = "";
|
2019-09-28 22:50:33 -07:00
|
|
|
} else if (type === "link_close") {
|
2018-01-21 21:44:25 -08:00
|
|
|
inLink = false;
|
2021-02-06 19:55:22 -08:00
|
|
|
const left = linkText.trimStart().length !== linkText.length;
|
|
|
|
const right = linkText.trimEnd().length !== linkText.length;
|
2018-01-21 21:44:25 -08:00
|
|
|
if (left || right) {
|
2019-09-24 23:00:30 -07:00
|
|
|
const line = params.lines[lineNumber - 1];
|
2020-03-07 19:49:57 -08:00
|
|
|
let range = null;
|
|
|
|
let fixInfo = null;
|
2019-09-21 21:05:58 -07:00
|
|
|
const match = line.slice(lineIndex).match(spaceInLinkRe);
|
2020-03-07 19:49:57 -08:00
|
|
|
if (match) {
|
2024-02-27 20:42:09 -08:00
|
|
|
// @ts-ignore
|
2020-03-07 19:49:57 -08:00
|
|
|
const column = match.index + lineIndex + 1;
|
|
|
|
const length = match[0].length;
|
|
|
|
range = [ column, length ];
|
|
|
|
fixInfo = {
|
|
|
|
"editColumn": column + 1,
|
|
|
|
"deleteCount": length - 2,
|
|
|
|
"insertText": linkText.trim()
|
|
|
|
};
|
|
|
|
lineIndex = column + length - 1;
|
|
|
|
}
|
2019-09-06 22:35:33 -07:00
|
|
|
addErrorContext(
|
|
|
|
onError,
|
|
|
|
lineNumber,
|
|
|
|
`[${linkText}]`,
|
|
|
|
left,
|
|
|
|
right,
|
2020-03-07 19:49:57 -08:00
|
|
|
range,
|
|
|
|
fixInfo
|
2019-09-06 22:35:33 -07:00
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2019-09-28 22:50:33 -07:00
|
|
|
} else if ((type === "softbreak") || (type === "hardbreak")) {
|
2019-09-24 23:00:30 -07:00
|
|
|
lineNumber++;
|
|
|
|
lineIndex = 0;
|
2018-01-21 21:44:25 -08:00
|
|
|
} else if (inLink) {
|
2022-05-06 21:42:31 -07:00
|
|
|
linkText += type.endsWith("_inline") ?
|
|
|
|
`${markup}${content}${markup}` :
|
|
|
|
(content || markup);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|