fix(ingestion): file deletion of old articles now syncs correctly
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import http from "node:http";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { exec } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const MIME_TYPES = {
|
||||
@@ -205,12 +206,43 @@ export function createServer(customSiteDir, customPostsDir, customAssetsDir) {
|
||||
}
|
||||
|
||||
const cleanSlug = querySlug.toLowerCase().replace(/[^a-z0-9_-]/g, "-").replace(/^-+|-+$/g, "");
|
||||
const targetFilePath = path.join(postsDir, `${cleanSlug}.md`);
|
||||
if (!cleanSlug) {
|
||||
res.writeHead(400, { "Content-Type": "application/json" });
|
||||
res.end(JSON.stringify({ error: "Invalid slug parameter" }));
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. Remove source markdown post
|
||||
const targetFilePath = path.join(postsDir, `${cleanSlug}.md`);
|
||||
if (fs.existsSync(targetFilePath)) {
|
||||
fs.unlinkSync(targetFilePath);
|
||||
}
|
||||
|
||||
// 2. Remove compiled static site directory and file
|
||||
const compiledDir = path.join(siteDir, "posts", cleanSlug);
|
||||
if (fs.existsSync(compiledDir)) {
|
||||
fs.rmSync(compiledDir, { recursive: true, force: true });
|
||||
}
|
||||
const compiledHtml = path.join(siteDir, "posts", `${cleanSlug}.html`);
|
||||
if (fs.existsSync(compiledHtml)) {
|
||||
fs.unlinkSync(compiledHtml);
|
||||
}
|
||||
|
||||
// 3. Rebuild static collections (index.html, dispatches, tags) if in full Eleventy environment
|
||||
try {
|
||||
if (fs.existsSync(path.join(process.cwd(), ".eleventy.js"))) {
|
||||
exec("npx @11ty/eleventy", (err) => {
|
||||
if (err) {
|
||||
console.error("[Ingestion API] Error rebuilding site after delete:", err);
|
||||
} else {
|
||||
console.log(`[Ingestion API] Site rebuild complete after unpublishing '${cleanSlug}'.`);
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (rebuildErr) {
|
||||
console.error("[Ingestion API] Failed to trigger Eleventy rebuild:", rebuildErr);
|
||||
}
|
||||
|
||||
res.writeHead(200, { "Content-Type": "application/json" });
|
||||
res.end(JSON.stringify({ status: "deleted", slug: cleanSlug }));
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user