缩减体积
This commit is contained in:
1 parent
d19b09a985
commit
327cdeb818
151 files changed
+801
-123
No files matched your search
@@ -0,0 +1,56 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const CONTENT_DIR = path.join(__dirname, '../content/post');
|
||||
|
||||
async function processDirectory(dir) {
|
||||
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(dir, entry.name);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
await processDirectory(fullPath);
|
||||
} else if (entry.isFile()) {
|
||||
const ext = path.extname(entry.name).toLowerCase();
|
||||
if (['.png', '.jpg', '.jpeg'].includes(ext)) {
|
||||
const webpPath = path.join(dir, path.basename(entry.name, ext) + '.webp');
|
||||
if (fs.existsSync(webpPath)) {
|
||||
// Check if index.md exists and verify it doesn't explicitly reference the original file
|
||||
// Actually, if WebP exists, we assume we want to use it.
|
||||
// But checking index.md is safer.
|
||||
const mdPath = path.join(dir, 'index.md');
|
||||
if (fs.existsSync(mdPath)) {
|
||||
const content = fs.readFileSync(mdPath, 'utf8');
|
||||
if (content.includes(path.basename(webpPath))) {
|
||||
console.log(`Deleting redundant file: ${fullPath} (WebP exists and is referenced)`);
|
||||
try {
|
||||
fs.unlinkSync(fullPath);
|
||||
} catch (e) {
|
||||
console.error(`Failed to delete ${fullPath}: ${e.message}`);
|
||||
}
|
||||
} else {
|
||||
console.warn(`Skipping ${fullPath}: WebP exists but not referenced in index.md?`);
|
||||
}
|
||||
} else {
|
||||
// No index.md, but WebP exists. Likely orphaned.
|
||||
console.log(`Deleting redundant file (no index.md): ${fullPath}`);
|
||||
try {
|
||||
fs.unlinkSync(fullPath);
|
||||
} catch (e) {
|
||||
console.error(`Failed to delete ${fullPath}: ${e.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(async () => {
|
||||
console.log(`Starting cleanup in ${CONTENT_DIR}...`);
|
||||
if (fs.existsSync(CONTENT_DIR)) {
|
||||
await processDirectory(CONTENT_DIR);
|
||||
}
|
||||
console.log('Done.');
|
||||
})();
|
||||
Reference in new issue
Block a user