From 4eb7c07eb386c5d934a7fb82409eb92f79efde21 Mon Sep 17 00:00:00 2001 From: svemagie <869694+svemagie@users.noreply.github.com> Date: Mon, 9 Mar 2026 21:23:50 +0100 Subject: [PATCH] Fix homepage SITE_SOCIAL identity defaults parsing --- package.json | 4 +- ...ch-endpoint-homepage-identity-defaults.mjs | 209 ++++++++++++++++++ start.example.sh | 1 + 3 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 scripts/patch-endpoint-homepage-identity-defaults.mjs diff --git a/package.json b/package.json index 9bb1c0f4..ba9e5db6 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "postinstall": "node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs && node scripts/patch-endpoint-media-sharp-runtime.mjs && node scripts/patch-frontend-sharp-runtime.mjs && node scripts/patch-endpoint-files-upload-route.mjs && node scripts/patch-endpoint-files-upload-locales.mjs && node scripts/patch-endpoint-activitypub-locales.mjs && node scripts/patch-endpoint-activitypub-docloader-loglevel.mjs && node scripts/patch-endpoint-activitypub-private-url-docloader.mjs && node scripts/patch-endpoint-activitypub-migrate-alias-clear.mjs && node scripts/patch-endpoint-homepage-locales.mjs && node scripts/patch-federation-unlisted-guards.mjs && node scripts/patch-endpoint-micropub-where-note-visibility.mjs && node scripts/patch-endpoint-posts-ai-fields.mjs && node scripts/patch-endpoint-posts-ai-cleanup.mjs && node scripts/patch-endpoint-podroll-opml-upload.mjs && node scripts/patch-preset-eleventy-ai-frontmatter.mjs && node scripts/patch-frontend-serviceworker-file.mjs && node scripts/patch-conversations-collection-guards.mjs && node scripts/patch-conversations-mastodon-disconnect.mjs && node scripts/patch-indiekit-routes-rate-limits.mjs && node scripts/patch-indiekit-error-production-stack.mjs && node scripts/patch-indieauth-devmode-guard.mjs && node scripts/patch-listening-endpoint-runtime-guards.mjs", - "serve": "export NODE_ENV=${NODE_ENV:-production} INDIEKIT_DEBUG=${INDIEKIT_DEBUG:-0} && node scripts/preflight-production-security.mjs && node scripts/preflight-mongo-connection.mjs && node scripts/preflight-activitypub-rsa-key.mjs && node scripts/preflight-activitypub-profile-urls.mjs && node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs && node scripts/patch-endpoint-media-sharp-runtime.mjs && node scripts/patch-frontend-sharp-runtime.mjs && node scripts/patch-endpoint-files-upload-route.mjs && node scripts/patch-endpoint-files-upload-locales.mjs && node scripts/patch-endpoint-activitypub-locales.mjs && node scripts/patch-endpoint-activitypub-docloader-loglevel.mjs && node scripts/patch-endpoint-activitypub-private-url-docloader.mjs && node scripts/patch-endpoint-activitypub-migrate-alias-clear.mjs && node scripts/patch-endpoint-homepage-locales.mjs && node scripts/patch-federation-unlisted-guards.mjs && node scripts/patch-endpoint-micropub-where-note-visibility.mjs && node scripts/patch-endpoint-posts-ai-fields.mjs && node scripts/patch-endpoint-posts-ai-cleanup.mjs && node scripts/patch-endpoint-podroll-opml-upload.mjs && node scripts/patch-preset-eleventy-ai-frontmatter.mjs && node scripts/patch-frontend-serviceworker-file.mjs && node scripts/patch-conversations-collection-guards.mjs && node scripts/patch-conversations-mastodon-disconnect.mjs && node scripts/patch-indiekit-routes-rate-limits.mjs && node scripts/patch-indiekit-error-production-stack.mjs && node scripts/patch-indieauth-devmode-guard.mjs && node scripts/patch-listening-endpoint-runtime-guards.mjs && node node_modules/@indiekit/indiekit/bin/cli.js serve --config indiekit.config.mjs", + "postinstall": "node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs && node scripts/patch-endpoint-media-sharp-runtime.mjs && node scripts/patch-frontend-sharp-runtime.mjs && node scripts/patch-endpoint-files-upload-route.mjs && node scripts/patch-endpoint-files-upload-locales.mjs && node scripts/patch-endpoint-activitypub-locales.mjs && node scripts/patch-endpoint-activitypub-docloader-loglevel.mjs && node scripts/patch-endpoint-activitypub-private-url-docloader.mjs && node scripts/patch-endpoint-activitypub-migrate-alias-clear.mjs && node scripts/patch-endpoint-homepage-locales.mjs && node scripts/patch-endpoint-homepage-identity-defaults.mjs && node scripts/patch-federation-unlisted-guards.mjs && node scripts/patch-endpoint-micropub-where-note-visibility.mjs && node scripts/patch-endpoint-posts-ai-fields.mjs && node scripts/patch-endpoint-posts-ai-cleanup.mjs && node scripts/patch-endpoint-podroll-opml-upload.mjs && node scripts/patch-preset-eleventy-ai-frontmatter.mjs && node scripts/patch-frontend-serviceworker-file.mjs && node scripts/patch-conversations-collection-guards.mjs && node scripts/patch-conversations-mastodon-disconnect.mjs && node scripts/patch-indiekit-routes-rate-limits.mjs && node scripts/patch-indiekit-error-production-stack.mjs && node scripts/patch-indieauth-devmode-guard.mjs && node scripts/patch-listening-endpoint-runtime-guards.mjs", + "serve": "export NODE_ENV=${NODE_ENV:-production} INDIEKIT_DEBUG=${INDIEKIT_DEBUG:-0} && node scripts/preflight-production-security.mjs && node scripts/preflight-mongo-connection.mjs && node scripts/preflight-activitypub-rsa-key.mjs && node scripts/preflight-activitypub-profile-urls.mjs && node scripts/patch-lightningcss.mjs && node scripts/patch-endpoint-media-scope.mjs && node scripts/patch-endpoint-media-sharp-runtime.mjs && node scripts/patch-frontend-sharp-runtime.mjs && node scripts/patch-endpoint-files-upload-route.mjs && node scripts/patch-endpoint-files-upload-locales.mjs && node scripts/patch-endpoint-activitypub-locales.mjs && node scripts/patch-endpoint-activitypub-docloader-loglevel.mjs && node scripts/patch-endpoint-activitypub-private-url-docloader.mjs && node scripts/patch-endpoint-activitypub-migrate-alias-clear.mjs && node scripts/patch-endpoint-homepage-locales.mjs && node scripts/patch-endpoint-homepage-identity-defaults.mjs && node scripts/patch-federation-unlisted-guards.mjs && node scripts/patch-endpoint-micropub-where-note-visibility.mjs && node scripts/patch-endpoint-posts-ai-fields.mjs && node scripts/patch-endpoint-posts-ai-cleanup.mjs && node scripts/patch-endpoint-podroll-opml-upload.mjs && node scripts/patch-preset-eleventy-ai-frontmatter.mjs && node scripts/patch-frontend-serviceworker-file.mjs && node scripts/patch-conversations-collection-guards.mjs && node scripts/patch-conversations-mastodon-disconnect.mjs && node scripts/patch-indiekit-routes-rate-limits.mjs && node scripts/patch-indiekit-error-production-stack.mjs && node scripts/patch-indieauth-devmode-guard.mjs && node scripts/patch-listening-endpoint-runtime-guards.mjs && node node_modules/@indiekit/indiekit/bin/cli.js serve --config indiekit.config.mjs", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], diff --git a/scripts/patch-endpoint-homepage-identity-defaults.mjs b/scripts/patch-endpoint-homepage-identity-defaults.mjs new file mode 100644 index 00000000..5250f645 --- /dev/null +++ b/scripts/patch-endpoint-homepage-identity-defaults.mjs @@ -0,0 +1,209 @@ +import { access, readFile, writeFile } from "node:fs/promises"; + +const candidates = [ + "node_modules/@rmdes/indiekit-endpoint-homepage/lib/controllers/dashboard.js", + "node_modules/@indiekit/indiekit/node_modules/@rmdes/indiekit-endpoint-homepage/lib/controllers/dashboard.js", +]; + +const marker = "function parseSiteSocialFromEnv(rawValue)"; + +const oldHelpersBlock = `function parseSocialLinks(body) { + const social = []; + if (!body.social) return social; + const entries = Array.isArray(body.social) ? body.social : Object.values(body.social); + for (const entry of entries) { + if (!entry || (!entry.name && !entry.url)) continue; + social.push({ + name: entry.name || "", + url: entry.url || "", + rel: entry.rel || "me", + icon: entry.icon || "", + }); + } + return social; +}`; + +const newHelpersBlock = `function parseSocialLinks(body) { + const social = []; + if (!body.social) return social; + const entries = Array.isArray(body.social) ? body.social : Object.values(body.social); + for (const entry of entries) { + if (!entry || (!entry.name && !entry.url)) continue; + social.push({ + name: entry.name || "", + url: entry.url || "", + rel: entry.rel || "me", + icon: entry.icon || "", + }); + } + return social; +} + +function normalizeSiteSocialEnvValue(rawValue) { + if (typeof rawValue !== "string") { + return ""; + } + + let value = rawValue.trim(); + + if (!value) { + return ""; + } + + while (/^SITE_SOCIAL\s*=/i.test(value)) { + value = value.replace(/^SITE_SOCIAL\s*=/i, "").trim(); + } + + if ( + (value.startsWith('"') && value.endsWith('"')) || + (value.startsWith("'") && value.endsWith("'")) + ) { + value = value.slice(1, -1).trim(); + } else { + value = value.replace(/^["']+/, "").replace(/["']+$/, "").trim(); + } + + return value; +} + +function parseSiteSocialFromEnv(rawValue) { + const normalized = normalizeSiteSocialEnvValue(rawValue); + + if (!normalized) { + return []; + } + + return normalized + .split(",") + .map((entry) => entry.trim()) + .filter(Boolean) + .map((entry) => { + const parts = entry.split("|").map((part) => part.trim()); + + if (parts.length < 2) { + return null; + } + + const name = parts[0] || ""; + const url = parts[1] || ""; + let rel = "me"; + let icon = ""; + + if (parts.length >= 4) { + rel = parts[2] || "me"; + icon = parts.slice(3).join("|") || ""; + } else if (parts.length >= 3) { + icon = parts[2] || ""; + } + + if (!name && !url) { + return null; + } + + return { + name, + url, + rel, + icon, + }; + }) + .filter(Boolean); +} + +function getIdentityDefaultsFromEnv() { + return { + name: (process.env.AUTHOR_NAME || "").trim(), + avatar: (process.env.AUTHOR_AVATAR || "").trim(), + title: "", + pronoun: "", + bio: (process.env.AUTHOR_BIO || "").trim(), + description: (process.env.SITE_DESCRIPTION || "").trim(), + locality: "", + country: "", + org: "", + url: (process.env.PUBLICATION_URL || process.env.SITE_URL || "").trim(), + email: "", + keyUrl: "", + categories: [], + social: parseSiteSocialFromEnv(process.env.SITE_SOCIAL), + }; +} + +function mergeIdentityDefaults(identity) { + const defaults = getIdentityDefaultsFromEnv(); + + if (!identity || typeof identity !== "object") { + return defaults; + } + + return { + ...defaults, + ...identity, + categories: Array.isArray(identity.categories) + ? identity.categories + : defaults.categories, + social: Array.isArray(identity.social) ? identity.social : defaults.social, + }; +}`; + +const oldIdentityLine = " const identity = config.identity || {};"; +const newIdentityLine = " const identity = mergeIdentityDefaults(config.identity);"; + +async function exists(filePath) { + try { + await access(filePath); + return true; + } catch { + return false; + } +} + +let checked = 0; +let patched = 0; + +for (const filePath of candidates) { + if (!(await exists(filePath))) { + continue; + } + + checked += 1; + + const source = await readFile(filePath, "utf8"); + let updated = source; + let changed = false; + + if (!updated.includes(marker)) { + if (!updated.includes(oldHelpersBlock)) { + continue; + } + + updated = updated.replace(oldHelpersBlock, newHelpersBlock); + changed = true; + } + + if (!updated.includes(newIdentityLine)) { + if (!updated.includes(oldIdentityLine)) { + continue; + } + + updated = updated.replace(oldIdentityLine, newIdentityLine); + changed = true; + } + + if (!changed) { + continue; + } + + await writeFile(filePath, updated, "utf8"); + patched += 1; +} + +if (checked === 0) { + console.log("[postinstall] No endpoint-homepage dashboard controllers found"); +} else if (patched === 0) { + console.log("[postinstall] endpoint-homepage identity defaults already patched"); +} else { + console.log( + `[postinstall] Patched endpoint-homepage identity defaults in ${patched} file(s)`, + ); +} diff --git a/start.example.sh b/start.example.sh index 75af3f08..b751cfd7 100644 --- a/start.example.sh +++ b/start.example.sh @@ -291,6 +291,7 @@ unset DEBUG "${NODE_BIN}" scripts/patch-endpoint-activitypub-private-url-docloader.mjs "${NODE_BIN}" scripts/patch-endpoint-activitypub-migrate-alias-clear.mjs "${NODE_BIN}" scripts/patch-endpoint-homepage-locales.mjs +"${NODE_BIN}" scripts/patch-endpoint-homepage-identity-defaults.mjs "${NODE_BIN}" scripts/patch-frontend-serviceworker-file.mjs "${NODE_BIN}" scripts/patch-conversations-collection-guards.mjs "${NODE_BIN}" scripts/patch-indiekit-routes-rate-limits.mjs