forked from Laboratoria/GDL004-md-links
/
links.js
29 lines (26 loc) 路 921 Bytes
/
links.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
const fs = require('fs');
module.exports.links = ( path ) => {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
reject(err);
return;
} else {
const exp = /\[((.*))\]\(((http|https|ftp|ftps).+?)\)/g;
const regExpObj = /\[(.*)\]\((.*)\)/;
let links = data.match(exp)
let arrayLinks = []
for (let i = 0; i < links.length; i++) {
const exec = regExpObj.exec(links[i])
let object = {
href: exec[2],
text: exec[1],
file: path
}
arrayLinks.push(object)
}
resolve(arrayLinks);
}
});
});
}