-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportBlogImages.ts
126 lines (99 loc) Β· 3.56 KB
/
importBlogImages.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import Bun from "bun";
import { mkdir, exists } from "node:fs/promises";
import { resolve } from "node:path";
console.log();
console.log("Hello, World!");
console.log();
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
class Log {
sleepCount = 25;
lineLength = 0;
async clearLine() {
const sleepDuration = this.sleepCount;
process.stdout.cursorTo(0);
process.stdout.write(" ".repeat(this.lineLength + 20));
process.stdout.cursorTo(0);
await sleep(sleepDuration);
}
async msg(msg: string) {
if (this.lineLength > 0) {
await this.clearLine();
}
process.stdout.write(msg);
this.lineLength = msg.length;
await sleep(this.sleepCount);
}
async next() {
process.stdout.write("\n");
await sleep(this.sleepCount);
this.lineLength = 0;
}
}
const downloadPath = "./src/assets/blog";
const downloadLocation = resolve(downloadPath);
console.log({ downloadLocation });
if (!(await exists(downloadLocation))) {
await mkdir(downloadLocation);
}
const log = new Log();
const glob = new Bun.Glob("**/*.md");
const contentPath = "./src/content";
for await (const filePath of glob.scan(contentPath)) {
const fullFilePath = [contentPath, filePath].join("/");
console.log(`π Checking ${fullFilePath}`);
const record = Bun.file(fullFilePath);
await log.msg(`π Reading File`);
const contents = await record.text();
await log.msg(`π Looking for Images`);
const images = Array.from(contents.matchAll(/!\[[^\]]*\]\((http.*?)\)/g)).map(
(match) => match[1]
);
const coverImage = contents.match(/image:\s{1,}([^\s]+)/)?.[1];
if (coverImage) {
images.push(coverImage);
}
const urlsToDownload = images.filter((v) => String(v ?? "").length > 0);
// .filter((v) => v.includes("blog.christophervachon.com"));
await log.msg(
`π Found ${urlsToDownload.length} Image${urlsToDownload.length !== 1 ? "s" : ""}`
);
await log.next();
for (const image of urlsToDownload) {
let fileUlrName = (image.split("/").pop() ?? "").split("?")[0];
if (String(fileUlrName).length === 0) {
continue;
} else if (!new RegExp("\\.\\w{1,}$").test(fileUlrName)) {
if (image.includes("fm=jpg") || image.includes("fm=jpeg")) {
fileUlrName += ".jpg";
} else if (image.includes("fm=png")) {
fileUlrName += ".png";
} else if (image.includes("fm=webp")) {
fileUlrName += ".webp";
} else {
fileUlrName += ".jpg";
}
}
await log.msg(`π Checking For ${fileUlrName}`);
const writePath = resolve(downloadLocation, `./${fileUlrName}`);
if (!(await Bun.file(writePath).exists())) {
await log.msg(`π Downloading ${image}`);
const result = await fetch(image);
await Bun.write(writePath, result);
await log.msg(`β
Downloaded ${fileUlrName}`);
}
await log.msg(`βοΈ Updating Content File with New Path`);
let fileContents = await Bun.file(fullFilePath).text();
fileContents = fileContents.replace(image, `./../../assets/blog/${fileUlrName}`);
await Bun.write(fullFilePath, fileContents);
await log.msg(`β
Migrated ${image}`);
await log.next();
console.log();
}
// console.log({ images });
console.log();
}
console.log();
console.log("Work Complete");
console.log();