perf(pic): 使用 cnb.cool 作为图床

This commit is contained in:
Ad-closeNN
2026-04-25 19:48:47 +08:00
parent c57b68a9e5
commit 54b9e8a44f
2 changed files with 71 additions and 1 deletions
+1 -1
View File
@@ -6,7 +6,7 @@
"dev": "astro dev", "dev": "astro dev",
"start": "astro dev", "start": "astro dev",
"check": "astro check", "check": "astro check",
"build": "astro build --verbose && pagefind --site dist", "build": "astro build --verbose && node scripts/rewrite-built-image-links.js && pagefind --site dist",
"preview": "astro preview", "preview": "astro preview",
"astro": "astro", "astro": "astro",
"type-check": "tsc --noEmit --isolatedDeclarations", "type-check": "tsc --noEmit --isolatedDeclarations",
+70
View File
@@ -0,0 +1,70 @@
import fs from "node:fs/promises";
import path from "node:path";
const DIST_DIR = path.resolve("dist");
const REMOTE_RAW_ORIGIN = "https://cnb.cool/CLN-Grated/blog-fuwari/-/git/raw/main/public";
const TEXT_FILE_EXTENSIONS = new Set([".html", ".xml", ".json", ".js", ".css", ".txt"]);
const PIC_PATH_PATTERN = /(?:https:\/\/blog\.adclosenn\.top)?\/pic\/([^"'\s)<>]+)/g;
async function walk(dir) {
const entries = await fs.readdir(dir, { withFileTypes: true });
const files = await Promise.all(
entries.map(async (entry) => {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
return walk(fullPath);
}
return [fullPath];
}),
);
return files.flat();
}
async function rewriteFile(filePath) {
const ext = path.extname(filePath).toLowerCase();
if (!TEXT_FILE_EXTENSIONS.has(ext)) {
return 0;
}
const original = await fs.readFile(filePath, "utf8");
let replacements = 0;
const rewritten = original.replace(PIC_PATH_PATTERN, (_match, rest) => {
replacements += 1;
return `${REMOTE_RAW_ORIGIN}/pic/${rest}`;
});
if (rewritten === original) {
return 0;
}
await fs.writeFile(filePath, rewritten, "utf8");
return replacements;
}
async function main() {
try {
await fs.access(DIST_DIR);
} catch {
console.error(`[rewrite-built-image-links] dist directory not found: ${DIST_DIR}`);
process.exit(1);
}
const files = await walk(DIST_DIR);
let touchedFiles = 0;
let totalReplacements = 0;
for (const filePath of files) {
const replacements = await rewriteFile(filePath);
if (replacements > 0) {
touchedFiles += 1;
totalReplacements += replacements;
}
}
console.log(
`[rewrite-built-image-links] Replaced ${totalReplacements} link(s) in ${touchedFiles} file(s).`,
);
}
await main();