-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud.js
92 lines (75 loc) · 2.09 KB
/
cloud.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 云服务器使用云服务器图床
const fs = require("fs");
const path = require('path');
// 涉及的文件夹路径
const dirs_use = ["./_posts", "./_includes/about", "./_includes/project",
"./_includes/topical", "./_includes/workflow"];
// 文件夹读取函数-core
function dirRead(direction) {
return new Promise((resolve, reject) => {
fs.readdir(direction, (err, files) => {
if (err) {
reject(console.error('读取目录时发生错误:', err));
};
resolve(files);
})
})
}
// 文件夹读取函数-full
async function searchFile(dirs = dirs_use) {
let target = [];
for (let i = 0; i < dirs.length; i++) {
let files = await dirRead(dirs[i]);
files.forEach(file => {
if (path.extname(file) === '.md') {
target.push(dirs[i]+"/"+file)
}
})
}
target = JSON.stringify(target);
return target;
}
// searchFile(dirs = dirs_use).then(data => {
// console.log(data);
// })
// ----------------------------------------
// 读取函数
function Read(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf-8', (err, data) => {
if (err) {
reject(console.log(err))
}
resolve(data)
})
})
}
// 处理文件
async function dealmd(path) {
let content = await Read(path);
const pattern = /https:\/\/img.caozihang.com\/img\//g;
content = content.replace(pattern, 'http://cnimg.caozihang.com/');
fs.writeFile(path, content, (err) => {
if (err) {
console.log(err);
throw err;
}
})
}
// dealmd("./_posts/2023-02-15-week.md").then(() => {
// console.log("处理完成");
// })
// ----------------------------------------
// 链接函数
function agency(data) {
data = JSON.parse(data);
const Promises = data.map(async md => {
await dealmd(md);
});
Promise.all(Promises).then(() => {
console.log("处理完成");
});
}
searchFile(dirs_use).then(data => {
agency(data);
})