Allow targetUrls without protocol

This commit is contained in:
Jia Hao 2016-01-19 21:32:55 +08:00
parent 364993bfcd
commit 256064213b
1 changed files with 15 additions and 3 deletions

View File

@ -1,5 +1,6 @@
import os from 'os';
import path from 'path';
import url from 'url';
import request from 'request';
import cheerio from 'cheerio';
@ -24,9 +25,7 @@ function optionsFactory(name,
userAgent,
callback) {
if (!validator.isURL(targetUrl, {require_protocol: true})) {
throw `Your Url ${targetUrl} is invalid!, did you remember to include 'http://'?`;
}
targetUrl = normalizeUrl(targetUrl);
if (!width) {
width = 1280;
@ -109,4 +108,17 @@ function getTitle(url, callback) {
});
}
function normalizeUrl(testUrl) {
// add protocol if protocol not found
let normalized = testUrl;
const parsed = url.parse(normalized);
if (!parsed.protocol) {
normalized = 'http://' + normalized;
}
if (!validator.isURL(normalized, {require_protocol: true})) {
throw `Your Url: "${normalized}" is invalid!`;
}
return normalized;
}
export default optionsFactory;