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

36 lines
580 B
JavaScript
Raw Normal View History

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