2017-04-18 03:28:35 +00:00
|
|
|
"use strict";
|
|
|
|
|
2018-04-26 16:03:33 +00:00
|
|
|
const linkify = require("linkify-it")()
|
|
|
|
.tlds(require("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",
|
2017-04-18 03:28:35 +00:00
|
|
|
"smb", "file",
|
|
|
|
"irc", "ircs",
|
|
|
|
"svn", "git",
|
|
|
|
"steam", "mumble", "ts3server",
|
|
|
|
"svn+ssh", "ssh",
|
|
|
|
];
|
|
|
|
|
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
|
|
|
|
2018-04-26 16:03:33 +00:00
|
|
|
function findLinks(text) {
|
|
|
|
const matches = linkify.match(text);
|
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
|
|
|
|
2018-04-26 16:03:33 +00:00
|
|
|
return matches.map((url) => ({
|
|
|
|
start: url.index,
|
|
|
|
end: url.lastIndex,
|
|
|
|
link: url.url,
|
|
|
|
}));
|
2017-04-18 03:28:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = findLinks;
|