-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgetImages.js
55 lines (49 loc) · 1.57 KB
/
getImages.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
const fs = require('fs-extra');
const Queue = require('./lib/Queue');
const getImg = require('./lib/getImg');
function self(items, imgPath, imgRelatePath = './imgs', jsonPath = '') {
return new Promise((resolve, reject) => {
if (!Array.isArray(items)) {
reject('items is not an Array');
}
let rs = [];
items.forEach(item => {
item.content.replace(/!\[\]\((.+?)\)/g, (input, $1) => {
rs.push($1);
});
});
const queue = new Queue(getImg, 2);
rs.forEach((url, i) => {
queue.add([url, imgPath]);
});
rs = {};
queue.run().then(
data => {
data.forEach(d => {
if (d && d.url && d.name) {
rs[d.url] = d.name;
}
});
items.forEach(item => {
let content = item.content.replace(/!\[\]\((.+?)\)/g, (input, $1) => {
if (rs[$1]) {
return `![](${imgRelatePath}/${rs[$1]})`;
}
return input;
});
// console.log(content);
item.content = content;
delete item.links;
});
if (jsonPath) {
fs.writeJSONSync(jsonPath, items);
}
resolve(items, rs.length);
},
e => {
reject(e);
}
);
});
}
module.exports = self;