-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (88 loc) · 2.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
var util = require('util');
var url = require('url');
var http = require('http');
var https = require('https');
var httpModules = {
'http:': http,
'https:': https
};
/**
* Output log only if environment variable DEBUG is enabled.
*/
function debug() {
if (process.env.DEBUG) {
// Because arguments is an array-like object, it must be called with Function.prototype.apply().
console.log('DEBUG: %s', util.format.apply(util, arguments));
}
}
/**
* Send a request for a given url and return response body.
*
* @param {String} urlStr
* @param {Function} callback
*/
function getBody(urlStr, callback) {
var urlObj = url.parse(urlStr);
var protocol = urlObj.protocol;
var httpModule = httpModules[protocol];
if (!httpModule) {
return callback(new Error('Invalid protocol: ' + protocol));
}
httpModule.get(urlStr, function(res) {
debug('status: ' + res.statusCode);
debug('headers: ' + JSON.stringify(res.headers));
var body = [];
debug('reading response body');
res.setEncoding('utf8');
res.on('data', function (chunk) {
body.push(chunk);
});
res.on('end', function() {
debug('end event');
// If separator is an empty string, all elements are joined without any characters in between them.
return callback(null, body.join(''));
});
}).on('error', function(err) {
return callback(new Error('Failed to retrieve data for given URL: %s\nReason: %s', urlStr, err.message));
});
}
/**
* Find a title from HTML.
*
* @param {String} html
* @return {String}
*/
function findTitle(html) {
// The flag 'i' represents case-insensitive search.
var re = new RegExp('<title>([^]*?)</title>', 'i');
var matches = html.match(re);
if (!matches) {
return null;
}
return matches[1];
}
/**
* Generate a markdown link from link text and url.
*
* @param {String} linkText
* @param {String} url
* @return {String}
*/
function createMarkdownLink(linkText, url) {
return util.format('[%s](%s)', linkText, url);
}
module.exports = function(urlStr, callback) {
getBody(urlStr, function(err, body) {
if (err) {
return callback(err);
}
debug(body);
var title = findTitle(body);
if (!title) {
return callback(new Error('Title not found'));
}
var link = createMarkdownLink(title, urlStr);
return callback(null, link);
});
}