Skip to content

Instantly share code, notes, and snippets.

@Chunlin-Li
Created April 17, 2016 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chunlin-Li/a8550800ee1c2f37f1bd5bf019bc8c1f to your computer and use it in GitHub Desktop.
Save Chunlin-Li/a8550800ee1c2f37f1bd5bf019bc8c1f to your computer and use it in GitHub Desktop.
'use strict';
const http = require('http');
const originalString = 'Today we see a good accelerator: http://alchemistaccelerator.com, you can found some thing in the wikipedia, https://en.wikipedia.org/wiki/The_Alchemist_Accelerator. or google it by : https://www.google.com.hk/search?es_sm=91&q=alchemist+accelerator&oq=alchemist+a&gs_l=serp.3.2.0l10.233056.233264.0.235425.2.2.0.0.0.0.134.267.0j2.2.0....0...1c.1j4.64.serp..0.2.265.J6dlgO9eRVI';
function fn(inputString, callback) {
// not a strict url match pattern
var regex = /https?:\/\/[\d\w\.\/_\-\?&=+#@]+\/?/g;
const newContent = [];
let temp = null;
let urlCount = 0;
let prevIndex = 0;
while (temp = regex.exec(inputString)) {
newContent.push(inputString.slice(prevIndex, temp.index));
newContent.push(temp[0]);
prevIndex = regex.lastIndex;
urlCount ++;
getContent(newContent, newContent.length - 1, err => {
// just ignore the failed urls.
if (err) console.error('fetch url content failed ', err, err.stack);
if (-- urlCount === 0) {
callback && callback(newContent.join(''));
}
});
}
newContent.push(inputString.slice(prevIndex));
}
function getContent(list, index, callback) {
let data = [];
require(list[index].split(':')[0]).get(list[index], resp => {
resp.on('data', chunk => data.push(chunk));
resp.on('end', () => {
data = Buffer.concat(data);
list[index] = data.toString();
callback && callback();
})
}).on('error', err => {
callback && callback(err);
})
}
/* test */
fn(originalString, res => {
console.log(res);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment