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
|
|
|
|
2018-04-27 22:05:34 -07:00
|
|
|
const spaceInLinkRe = /\[(?:\s+(?:[^\]]*?)\s*|(?:[^\]]*?)\s+)](?=\(\S*\))/;
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD039", "no-space-in-links" ],
|
|
|
|
"description": "Spaces inside link text",
|
|
|
|
"tags": [ "whitespace", "links" ],
|
|
|
|
"function": function MD039(params, onError) {
|
2019-09-06 22:35:33 -07:00
|
|
|
filterTokens(params, "inline", (token) => {
|
|
|
|
const { line, lineNumber, children } = token;
|
2018-04-27 22:05:34 -07:00
|
|
|
let inLink = false;
|
|
|
|
let linkText = "";
|
2019-09-06 22:35:33 -07:00
|
|
|
children.forEach((child) => {
|
2018-01-21 21:44:25 -08:00
|
|
|
if (child.type === "link_open") {
|
|
|
|
inLink = true;
|
|
|
|
linkText = "";
|
|
|
|
} else if (child.type === "link_close") {
|
|
|
|
inLink = false;
|
2019-04-17 14:54:27 -07:00
|
|
|
const left = linkText.trimLeft().length !== linkText.length;
|
|
|
|
const right = linkText.trimRight().length !== linkText.length;
|
2018-01-21 21:44:25 -08:00
|
|
|
if (left || right) {
|
2019-09-06 22:35:33 -07:00
|
|
|
const match = line.match(spaceInLinkRe);
|
|
|
|
const column = match.index + 1;
|
|
|
|
const length = match[0].length;
|
|
|
|
addErrorContext(
|
|
|
|
onError,
|
|
|
|
lineNumber,
|
|
|
|
`[${linkText}]`,
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
[ column, length ],
|
|
|
|
{
|
|
|
|
"editColumn": column + 1,
|
|
|
|
"deleteCount": length - 2,
|
|
|
|
"insertText": linkText.trim()
|
|
|
|
}
|
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
} else if (inLink) {
|
|
|
|
linkText += child.content;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|