2022-06-19 00:25:21 +00:00
|
|
|
import LinkifyIt, {Match} from "linkify-it";
|
|
|
|
import {Part} from "./merge";
|
2017-04-18 03:28:35 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
export type LinkPart = Part & {
|
|
|
|
link: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type OurMatch = Match & {
|
|
|
|
noschema?: boolean;
|
|
|
|
};
|
2018-05-23 13:50:59 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
LinkifyIt.prototype.normalize = function normalize(match: OurMatch) {
|
2018-05-23 13:50:59 +00:00
|
|
|
if (!match.schema) {
|
2018-06-09 06:07:37 +00:00
|
|
|
match.schema = "http:";
|
|
|
|
match.url = "http://" + match.url;
|
2020-08-04 17:15:50 +00:00
|
|
|
match.noschema = true;
|
2018-05-23 13:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (match.schema === "//") {
|
2018-06-09 06:07:37 +00:00
|
|
|
match.schema = "http:";
|
|
|
|
match.url = "http:" + match.url;
|
2020-08-04 17:15:50 +00:00
|
|
|
match.noschema = true;
|
2018-05-23 13:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (match.schema === "mailto:" && !/^mailto:/i.test(match.url)) {
|
|
|
|
match.url = "mailto:" + match.url;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
import tlds from "tlds";
|
|
|
|
const linkify = LinkifyIt().tlds(tlds).tlds("onion", true);
|
2017-04-18 03:28:35 +00:00
|
|
|
|
2018-04-26 16:03:33 +00:00
|
|
|
// Known schemes to detect in text
|
2017-04-18 03:28:35 +00:00
|
|
|
const commonSchemes = [
|
2018-04-26 16:03:33 +00:00
|
|
|
"sftp",
|
2019-07-17 09:33:59 +00:00
|
|
|
"smb",
|
|
|
|
"file",
|
|
|
|
"irc",
|
|
|
|
"ircs",
|
|
|
|
"svn",
|
|
|
|
"git",
|
|
|
|
"steam",
|
|
|
|
"mumble",
|
|
|
|
"ts3server",
|
|
|
|
"svn+ssh",
|
|
|
|
"ssh",
|
2021-02-01 15:43:06 +00:00
|
|
|
"gopher",
|
|
|
|
"gemini",
|
2017-04-18 03:28:35 +00:00
|
|
|
];
|
|
|
|
|
2018-04-26 16:03:33 +00:00
|
|
|
for (const schema of commonSchemes) {
|
|
|
|
linkify.add(schema + ":", "http:");
|
|
|
|
}
|
2017-04-18 03:28:35 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function findLinks(text: string) {
|
|
|
|
const matches = linkify.match(text) as OurMatch[];
|
2017-04-18 03:28:35 +00:00
|
|
|
|
2018-04-26 16:03:33 +00:00
|
|
|
if (!matches) {
|
|
|
|
return [];
|
2017-10-05 20:44:20 +00:00
|
|
|
}
|
2017-04-18 03:28:35 +00:00
|
|
|
|
2020-08-04 17:15:50 +00:00
|
|
|
return matches.map(returnUrl);
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function findLinksWithSchema(text: string) {
|
|
|
|
const matches = linkify.match(text) as OurMatch[];
|
2020-08-04 17:15:50 +00:00
|
|
|
|
|
|
|
if (!matches) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches.filter((url) => !url.noschema).map(returnUrl);
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function returnUrl(url: OurMatch): LinkPart {
|
2020-08-04 17:15:50 +00:00
|
|
|
return {
|
2018-05-23 13:50:59 +00:00
|
|
|
start: url.index,
|
|
|
|
end: url.lastIndex,
|
|
|
|
link: url.url,
|
2020-08-04 17:15:50 +00:00
|
|
|
};
|
2017-04-18 03:28:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
export {findLinks, findLinksWithSchema};
|