Files
indiekit-server/scripts/patch-endpoint-posts-content-null-guard.mjs
Sven 2734434440
Deploy Indiekit Server / deploy (push) Successful in 1m28s
fix: re-add content.njk null guard (crashes on draft with no content)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-27 14:22:56 +02:00

43 lines
1.6 KiB
JavaScript

/**
* Patch: guard against null content.text in post view template.
*
* content.njk calls `property.text | markdown` but property.text can be null
* when a post was published with empty body (draft with no content). The
* markdown filter throws "Input data should be a String" on null input.
*
* Fix: coerce null/undefined to empty string via Nunjucks `or ''`.
*/
import { access, readFile, writeFile } from "node:fs/promises";
const candidates = [
"node_modules/@rmdes/indiekit-endpoint-posts/includes/post-types/content.njk",
"node_modules/@indiekit/endpoint-posts/includes/post-types/content.njk",
];
const marker = "[patch] content-null-guard";
const oldSnippet = `{{ prose({
html: property.text | markdown
}) if property }}`;
const newSnippet = `{# ${marker} #}
{{ prose({
html: (property.text or '') | markdown
}) if property }}`;
async function exists(p) {
try { await access(p); return true; } catch { return false; }
}
let patched = 0;
for (const filePath of candidates) {
if (!(await exists(filePath))) continue;
const source = await readFile(filePath, "utf8");
if (source.includes(marker)) { console.log(`[postinstall] content-null-guard already applied: ${filePath}`); continue; }
if (!source.includes(oldSnippet)) { console.log(`[postinstall] content-null-guard: snippet not found in ${filePath}`); continue; }
await writeFile(filePath, source.replace(oldSnippet, newSnippet), "utf8");
patched++;
}
console.log(patched > 0
? `[postinstall] Patched content-null-guard in ${patched} file(s)`
: "[postinstall] content-null-guard: nothing to patch");