-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace.ts
60 lines (50 loc) · 1.5 KB
/
replace.ts
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
import { Dirent, readdirSync, readFileSync } from "fs";
import https from "https";
import { basename, resolve } from "path";
const sourceDirPath = resolve(__dirname, "./source/_posts");
const imageUrlReg = /\!\[.*\]\((http.+)\)/g;
async function main() {
const posts = readdirSync(sourceDirPath, { withFileTypes: true }).filter(
file => file.isFile()
);
for (const post of posts) {
await processPost(post);
}
}
async function processPost(postFile: Dirent) {
// console.log("Start process file: ", postFile.name);
let fileContent = readFileSync(resolve(sourceDirPath, postFile.name), {
encoding: "utf8"
});
let matched;
let fileIndex = 1;
while (true) {
matched = imageUrlReg.exec(fileContent);
if (!matched) break;
const url = matched[1];
console.log("Dowanload File: ", url);
const fileData = await download(url);
console.log("Download Complete: ", url);
const fileName = basename(postFile.name, ".md") + ".";
saveFile(fileData, resolve(sourceDirPath, fileName));
fileIndex++;
}
}
function download(url: string): Promise<Buffer> {
return new Promise((resolve, reject) => {
let chunks: any[] = [];
https.get(url, res => {
res.on("data", chunk => chunks.push(chunk));
res.once("error", () => {
reject(new Error("Download Error"));
});
res.once("end", () => {
resolve(Buffer.from(chunks));
});
});
});
}
function saveFile(data: Buffer, fileName: string) {
console.log(fileName);
}
main();