thelounge/client/js/libs/handlebars/ircmessageparser/findLinks.js
Jérémie Astori 5b4c00d8ca Extract findLinks into its own file and add tests
Tests were taken from 5a249c30b1/test/findLinks.js.
The underlying code is different but the tests are the same.
2017-04-21 20:31:27 +03:00

54 lines
1.1 KiB
JavaScript

"use strict";
const URI = require("urijs");
const commonSchemes = [
"http", "https",
"ftp", "sftp",
"smb", "file",
"irc", "ircs",
"svn", "git",
"steam", "mumble", "ts3server",
"svn+ssh", "ssh",
];
function findLinks(text) {
let result = [];
let lastPosition = 0;
URI.withinString(text, function(url, start, end) {
// v-- fix: url was modified and does not match input string -> cant be mapped
if (text.indexOf(url, lastPosition) < 0) {
return;
}
// ^-- /fix: url was modified and does not match input string -> cant be mapped
// v-- fix: use prefered scheme
const parsed = URI(url);
const parsedScheme = parsed.scheme().toLowerCase();
const matchedScheme = commonSchemes.find(scheme => parsedScheme.endsWith(scheme));
if (matchedScheme) {
const prefix = parsedScheme.length - matchedScheme.length;
start += prefix;
url = url.slice(prefix);
}
// ^-- /fix: use prefered scheme
// URL matched, but does not start with a protocol, add it
if (!parsedScheme.length) {
url = "http://" + url;
}
result.push({
start: start,
end: end,
link: url
});
});
return result;
}
module.exports = findLinks;