thelounge/client/js/libs/handlebars/ircmessageparser/findLinks.js

54 lines
969 B
JavaScript
Raw Normal View History

"use strict";
const LinkifyIt = require("linkify-it");
LinkifyIt.prototype.normalize = function normalize(match) {
if (!match.schema) {
match.schema = "https:";
match.url = "https://" + match.url;
}
if (match.schema === "//") {
match.schema = "https:";
match.url = "https:" + match.url;
}
if (match.schema === "mailto:" && !/^mailto:/i.test(match.url)) {
match.url = "mailto:" + match.url;
}
};
const linkify = LinkifyIt()
2018-04-26 18:03:33 +02:00
.tlds(require("tlds"))
.tlds("onion", true);
2018-04-26 18:03:33 +02:00
// Known schemes to detect in text
const commonSchemes = [
2018-04-26 18:03:33 +02:00
"sftp",
"smb", "file",
"irc", "ircs",
"svn", "git",
"steam", "mumble", "ts3server",
"svn+ssh", "ssh",
];
2018-04-26 18:03:33 +02:00
for (const schema of commonSchemes) {
linkify.add(schema + ":", "http:");
}
2018-04-26 18:03:33 +02:00
function findLinks(text) {
const matches = linkify.match(text);
2018-04-26 18:03:33 +02:00
if (!matches) {
return [];
2017-10-05 22:44:20 +02:00
}
return matches.map((url) => ({
start: url.index,
end: url.lastIndex,
link: url.url,
}));
}
module.exports = findLinks;