hyper/lib/utils/url-command.js

27 lines
653 B
JavaScript
Raw Normal View History

2016-07-13 12:44:24 -08:00
import * as regex from './url-regex';
2016-07-14 15:40:15 -08:00
export const domainRegex = /\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b/;
export default function isUrlCommand (shell, data) {
const matcher = regex[shell];
if (undefined === matcher || !data) return null;
2016-07-14 15:40:15 -08:00
const match = data.match(matcher);
if (!match) return null;
const protocol = match[1];
const path = match[2];
2016-07-14 15:40:15 -08:00
if (path) {
if (protocol) {
return `${protocol}${path}`;
}
// extract the domain portion from the url
const domain = path.split('/')[0];
if (domainRegex.test(domain)) {
return `http://${path}`;
2016-07-13 12:44:24 -08:00
}
}
2016-07-14 15:40:15 -08:00
return null;
2016-07-13 12:44:24 -08:00
}