2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-05-04 20:14:59 -07:00
|
|
|
const { addErrorContext, bareUrlRe, filterTokens } = require("../helpers");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD034", "no-bare-urls" ],
|
|
|
|
"description": "Bare URL used",
|
|
|
|
"tags": [ "links", "url" ],
|
|
|
|
"function": function MD034(params, onError) {
|
2019-05-04 20:14:59 -07:00
|
|
|
filterTokens(params, "inline", (token) => {
|
2018-04-27 22:05:34 -07:00
|
|
|
let inLink = false;
|
2019-05-04 20:14:59 -07:00
|
|
|
token.children.forEach((child) => {
|
|
|
|
const { content, line, lineNumber, type } = child;
|
2018-04-27 22:05:34 -07:00
|
|
|
let match = null;
|
2019-05-04 20:14:59 -07:00
|
|
|
if (type === "link_open") {
|
2018-01-21 21:44:25 -08:00
|
|
|
inLink = true;
|
2019-05-04 20:14:59 -07:00
|
|
|
} else if (type === "link_close") {
|
2018-01-21 21:44:25 -08:00
|
|
|
inLink = false;
|
2019-09-22 21:31:02 -07:00
|
|
|
} else if ((type === "text") && !inLink) {
|
|
|
|
while ((match = bareUrlRe.exec(content)) !== null) {
|
|
|
|
const [ bareUrl ] = match;
|
|
|
|
const index = line.indexOf(content);
|
|
|
|
const range = (index === -1) ? null : [
|
|
|
|
line.indexOf(content) + match.index + 1,
|
|
|
|
bareUrl.length
|
|
|
|
];
|
|
|
|
const fixInfo = range ? {
|
|
|
|
"editColumn": range[0],
|
|
|
|
"deleteCount": range[1],
|
|
|
|
"insertText": `<${bareUrl}>`
|
|
|
|
} : null;
|
|
|
|
addErrorContext(
|
|
|
|
onError,
|
|
|
|
lineNumber,
|
|
|
|
bareUrl,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
range,
|
|
|
|
fixInfo
|
|
|
|
);
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|