2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2023-02-05 16:58:06 -08:00
|
|
|
const { addErrorContext } = require("../helpers");
|
2023-05-23 04:01:55 +00:00
|
|
|
const { filterByPredicate, getHtmlTagInfo, parse } =
|
2023-02-05 16:58:06 -08:00
|
|
|
require("../helpers/micromark.cjs");
|
2022-11-13 21:39:14 -08:00
|
|
|
|
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) {
|
2023-05-23 04:01:55 +00:00
|
|
|
const literalAutolinks = (tokens) => (
|
2023-02-05 16:58:06 -08:00
|
|
|
filterByPredicate(
|
2023-05-23 04:01:55 +00:00
|
|
|
tokens,
|
2023-02-05 16:58:06 -08:00
|
|
|
(token) => token.type === "literalAutolink",
|
2023-03-14 21:03:07 -07:00
|
|
|
(token) => {
|
|
|
|
const { children } = token;
|
2023-02-05 16:58:06 -08:00
|
|
|
const result = [];
|
2023-03-14 21:03:07 -07:00
|
|
|
for (let i = 0; i < children.length; i++) {
|
|
|
|
const openToken = children[i];
|
2023-02-05 16:58:06 -08:00
|
|
|
const openTagInfo = getHtmlTagInfo(openToken);
|
|
|
|
if (openTagInfo && !openTagInfo.close) {
|
|
|
|
let count = 1;
|
2023-03-14 21:03:07 -07:00
|
|
|
for (let j = i + 1; j < children.length; j++) {
|
|
|
|
const closeToken = children[j];
|
2023-02-05 16:58:06 -08:00
|
|
|
const closeTagInfo = getHtmlTagInfo(closeToken);
|
|
|
|
if (closeTagInfo && (openTagInfo.name === closeTagInfo.name)) {
|
|
|
|
if (closeTagInfo.close) {
|
|
|
|
count--;
|
|
|
|
if (count === 0) {
|
|
|
|
i = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.push(openToken);
|
|
|
|
}
|
2019-09-22 21:31:02 -07:00
|
|
|
}
|
2023-02-05 16:58:06 -08:00
|
|
|
return result;
|
2023-05-23 04:01:55 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
if (literalAutolinks(params.parsers.micromark.tokens).length > 0) {
|
|
|
|
// Re-parse with correct link/image reference definition handling
|
|
|
|
const document = params.lines.join("\n");
|
|
|
|
const tokens = parse(document, undefined, false);
|
|
|
|
for (const token of literalAutolinks(tokens)) {
|
|
|
|
const range = [
|
|
|
|
token.startColumn,
|
|
|
|
token.endColumn - token.startColumn
|
|
|
|
];
|
|
|
|
const fixInfo = {
|
|
|
|
"editColumn": range[0],
|
|
|
|
"deleteCount": range[1],
|
|
|
|
"insertText": `<${token.text}>`
|
|
|
|
};
|
|
|
|
addErrorContext(
|
|
|
|
onError,
|
|
|
|
token.startLine,
|
|
|
|
token.text,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
range,
|
|
|
|
fixInfo
|
|
|
|
);
|
|
|
|
}
|
2022-12-06 22:14:40 -08:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|