Created
April 17, 2016 18:48
-
-
Save Chunlin-Li/a8550800ee1c2f37f1bd5bf019bc8c1f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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