Files
indiekit-server/scripts/patch-endpoint-posts-content-field-object.mjs
Sven b28a2c1a4e
Deploy Indiekit Server / deploy (push) Successful in 1m19s
fix: re-add content-field [object Object] patch (confirmed not localStorage)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-27 14:26:37 +02:00

46 lines
1.8 KiB
JavaScript

/**
* Patch: prevent [object Object] in content textarea.
*
* content-field.njk falls back to `fieldData("content").value` when
* `properties.content.text` is null. fieldData returns the raw content
* property which is an object `{ html: null, text: null }`. Nunjucks
* coerces that to "[object Object]" in the textarea value.
*
* Fix: extract .text from the fieldData value when it's an object,
* but preserve a plain string value for validation-error re-renders.
*/
import { access, readFile, writeFile } from "node:fs/promises";
const candidates = [
"node_modules/@rmdes/indiekit-endpoint-posts/includes/post-types/content-field.njk",
"node_modules/@indiekit/endpoint-posts/includes/post-types/content-field.njk",
];
const marker = "[patch] content-field-object";
const oldSnippet = `{{ textarea({
name: "content",
value: properties.content.text or fieldData("content").value,`;
const newSnippet = `{#- ${marker} -#}
{%- set _contentFv = fieldData("content").value -%}
{{ textarea({
name: "content",
value: properties.content.text or _contentFv.text or (_contentFv if _contentFv is string else ""),`;
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-field-object already applied: ${filePath}`); continue; }
if (!source.includes(oldSnippet)) { console.log(`[postinstall] content-field-object: snippet not found in ${filePath}`); continue; }
await writeFile(filePath, source.replace(oldSnippet, newSnippet), "utf8");
patched++;
}
console.log(patched > 0
? `[postinstall] Patched content-field-object in ${patched} file(s)`
: "[postinstall] content-field-object: nothing to patch");