diff --git a/package.json b/package.json index b4ff610d..c0aae49d 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-homepage-locales.mjs && node scripts/patch-federation-unlisted-guards.mjs && node scripts/patch-endpoint-micropub-where-note-visibility.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-homepage-locales.mjs && node scripts/patch-federation-unlisted-guards.mjs && node scripts/patch-endpoint-micropub-where-note-visibility.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-homepage-locales.mjs && node scripts/patch-federation-unlisted-guards.mjs && node scripts/patch-endpoint-micropub-where-note-visibility.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-homepage-locales.mjs && node scripts/patch-federation-unlisted-guards.mjs && node scripts/patch-endpoint-micropub-where-note-visibility.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-preset-eleventy-ai-frontmatter.mjs b/scripts/patch-preset-eleventy-ai-frontmatter.mjs new file mode 100644 index 00000000..a3f2b443 --- /dev/null +++ b/scripts/patch-preset-eleventy-ai-frontmatter.mjs @@ -0,0 +1,127 @@ +import { access, readFile, writeFile } from "node:fs/promises"; + +const candidates = [ + "node_modules/@rmdes/indiekit-preset-eleventy/lib/post-template.js", + "node_modules/@indiekit/preset-eleventy/lib/post-template.js", + "node_modules/@indiekit/indiekit/node_modules/@rmdes/indiekit-preset-eleventy/lib/post-template.js", + "node_modules/@indiekit/indiekit/node_modules/@indiekit/preset-eleventy/lib/post-template.js", +]; + +const patchMarker = "Normalize AI disclosure metadata and default to no AI usage."; + +const oldBlock = [ + " // Convert url to Eleventy permalink so generated URL matches Indiekit's stored URL", + " // Add trailing slash to generate /path/index.html instead of /path.html", + " if (properties.url) {", + " const url = properties.url;", + " properties.permalink = url.endsWith(\"/\") ? url : `${url}/`;", + " }", + " delete properties.url;", + "", + " const frontMatter = YAML.stringify(properties, { lineWidth: 0 });", + " return `---\\n${frontMatter}---\\n`;", + "};", +].join("\n"); + +const newBlock = [ + " // Convert url to Eleventy permalink so generated URL matches Indiekit's stored URL", + " // Add trailing slash to generate /path/index.html instead of /path.html", + " if (properties.url) {", + " const url = properties.url;", + " properties.permalink = url.endsWith(\"/\") ? url : `${url}/`;", + " }", + " delete properties.url;", + "", + " // Normalize AI disclosure metadata and default to no AI usage.", + " const aiSource =", + " properties.ai && typeof properties.ai === \"object\" && !Array.isArray(properties.ai)", + " ? properties.ai", + " : {};", + "", + " const aiTextLevel = String(", + " aiSource.textLevel ?? aiSource.aiTextLevel ?? properties.aiTextLevel ?? \"0\",", + " );", + "", + " const aiCodeLevel = String(", + " aiSource.codeLevel ?? aiSource.aiCodeLevel ?? properties.aiCodeLevel ?? \"0\",", + " );", + "", + " const aiTools = aiSource.aiTools ?? aiSource.tools ?? properties.aiTools;", + "", + " const aiDescription =", + " aiSource.aiDescription ?? aiSource.description ?? properties.aiDescription;", + "", + " delete properties.ai;", + " delete properties.aiTextLevel;", + " delete properties.aiCodeLevel;", + " delete properties.aiTools;", + " delete properties.aiDescription;", + "", + " const frontMatter = YAML.stringify(properties, { lineWidth: 0 });", + "", + " let aiFrontMatter = `ai:\\n textLevel: \\\"${aiTextLevel}\\\"\\n codeLevel: \\\"${aiCodeLevel}\\\"\\n # aiTools: \\\"Claude, ChatGPT, Copilot\\\"\\n # aiDescription: \\\"Optional disclosure about how AI was used\\\"\\n`;", + "", + " if (aiTools !== undefined && aiTools !== null && aiTools !== \"\") {", + " aiFrontMatter = aiFrontMatter.replace(", + " ' # aiTools: \\\"Claude, ChatGPT, Copilot\\\"\\n',", + " ` aiTools: ${JSON.stringify(String(aiTools))}\\n`,", + " );", + " }", + "", + " if (aiDescription !== undefined && aiDescription !== null && aiDescription !== \"\") {", + " aiFrontMatter = aiFrontMatter.replace(", + " ' # aiDescription: \\\"Optional disclosure about how AI was used\\\"\\n',", + " ` aiDescription: ${JSON.stringify(String(aiDescription))}\\n`,", + " );", + " }", + "", + " return `---\\n${frontMatter}${aiFrontMatter}---\\n`;", + "};", +].join("\n"); + +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"); + + if (source.includes(patchMarker)) { + continue; + } + + if (!source.includes(oldBlock)) { + console.warn( + `[postinstall] Skipping preset-eleventy AI frontmatter patch for ${filePath}: upstream format changed`, + ); + continue; + } + + const updated = source.replace(oldBlock, newBlock); + await writeFile(filePath, updated, "utf8"); + patched += 1; +} + +if (checked === 0) { + console.log("[postinstall] No preset-eleventy post-template files found"); +} else if (patched === 0) { + console.log("[postinstall] preset-eleventy AI frontmatter patch already applied"); +} else { + console.log( + `[postinstall] Patched preset-eleventy AI frontmatter in ${patched} file(s)`, + ); +}