Skip to content

Commit a1f2ace

Browse files
committed
Harden build script and cache image metadata
1 parent 9f5570f commit a1f2ace

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

build.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import axios from "axios";
33

44
async function fetchCiTime(filePath) {
55
const url = `https://api.github.com/repos/tw93/weekly/commits?path=${filePath}&page=1&per_page=1`;
6-
const response = await axios.get(url);
7-
const ciTime = response.data[0].commit.committer.date.split("T")[0];
8-
return ciTime;
6+
try {
7+
const response = await axios.get(url);
8+
const commitDate = response.data?.[0]?.commit?.committer?.date;
9+
return commitDate ? commitDate.split("T")[0] : null;
10+
} catch (error) {
11+
return null;
12+
}
913
}
1014

1115
async function main() {
@@ -16,8 +20,11 @@ async function main() {
1620
const mdFiles = files
1721
.filter((file) => file.endsWith(".md"))
1822
.sort((a, b) => {
19-
const numA = parseInt(a.match(/(\d+)/)[0]);
20-
const numB = parseInt(b.match(/(\d+)/)[0]);
23+
const matchA = a.match(/(\d+)/);
24+
const matchB = b.match(/(\d+)/);
25+
if (!matchA || !matchB) return 0;
26+
const numA = parseInt(matchA[0], 10);
27+
const numB = parseInt(matchB[0], 10);
2128
return numB - numA;
2229
});
2330

@@ -36,7 +43,8 @@ async function main() {
3643
const title = `第 ${num} 期 - ${shortTitle}`;
3744

3845
// Read markdown file to extract cover image and description
39-
const mdContent = await fs.readFile(`./src/pages/posts/${name}`, "utf8");
46+
const fullPath = `./src/pages/posts/${name}`;
47+
const mdContent = await fs.readFile(fullPath, "utf8");
4048
const imgMatch = mdContent.match(/<img\s+src="([^"]+)"/);
4149
const pic = imgMatch ? imgMatch[1] : "";
4250

@@ -47,7 +55,9 @@ async function main() {
4755
readmeContent2 += `* [${title}](${url})\n`;
4856

4957
if (i < 5) {
50-
const modified = await fetchCiTime(`/src/pages/posts/${filePath}`);
58+
const modified =
59+
(await fetchCiTime(`/src/pages/posts/${filePath}`)) ||
60+
new Date((await fs.stat(fullPath)).mtime).toISOString().split("T")[0];
5161
recentContent += `* [${title}](${url}) - ${modified}\n`;
5262
}
5363
}

rehype-image.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,24 @@ import path from "path";
77
let imageMetadataCache = null;
88

99
function getImageMetadata() {
10+
if (process.env.NODE_ENV === "production" && imageMetadataCache) {
11+
return imageMetadataCache;
12+
}
1013
const METADATA_PATH = path.resolve("./src/data/image-metadata.json");
1114
if (!fs.existsSync(METADATA_PATH)) return {};
1215
try {
13-
return JSON.parse(fs.readFileSync(METADATA_PATH, "utf-8"));
16+
const data = JSON.parse(fs.readFileSync(METADATA_PATH, "utf-8"));
17+
if (process.env.NODE_ENV === "production") {
18+
imageMetadataCache = data;
19+
}
20+
return data;
1421
} catch (e) {
1522
console.error("Failed to parse image-metadata.json", e);
1623
return {};
1724
}
1825
}
1926

20-
const getHeroImage = (tree, homePage) => {
27+
const getHeroImage = (tree) => {
2128
let heroUrl = null;
2229
visit(tree, "raw", (node) => {
2330
if (heroUrl) return;

0 commit comments

Comments
 (0)