import type { Page } from '@/lib/schema/page' export type ExtrasOverrides = { robotsText?: string manifest?: Partial<{ name: string short_name: string start_url: string display: string theme_color: string }> sheetsMode?: 'auto' | 'include' | 'exclude' } function toBytes(s: string): Uint8Array { return new TextEncoder().encode(s) } function buildRobots(base?: string): string { if (base && base.trim().length > 0) { const s = base.trim() if (/^User-agent:/i.test(s)) return s const lines = s.split(/\n+/).map((l) => l.trim()).filter((l) => l.length > 0) if (lines.length === 0) return 'User-agent: *\n' const disallowLines = lines.map((l) => (l.startsWith('Disallow:') ? l : `Disallow: ${l}`)) return `User-agent: *\n${disallowLines.join('\n')}` } return 'User-agent: *\n' } function deriveManifest(page: Page) { const name = page.title || 'Landing' const short_name = name.length > 12 ? name.slice(0, 12) : name const start_url = '/' const display = 'standalone' const theme_color = page.theme?.primaryColor || '#0ea5e9' return { name, short_name, start_url, display, theme_color } } export function buildExtrasForPage(page: Page, overrides?: ExtrasOverrides): Record { const out: Record = {} // robots.txt const robotsText = buildRobots(overrides?.robotsText) out['robots.txt'] = toBytes(robotsText) // site.webmanifest (auto-enrich and safe-merge) const manifestBase = deriveManifest(page) const ov = overrides?.manifest || {} const name = ov.name && ov.name.trim().length > 0 ? ov.name : manifestBase.name const short_name = ov.short_name && ov.short_name.trim().length > 0 ? ov.short_name : (name.length > 12 ? name.slice(0, 12) : name) const start_url = ov.start_url && ov.start_url.trim().length > 0 ? ov.start_url : manifestBase.start_url const display = ov.display && ov.display.trim().length > 0 ? ov.display : manifestBase.display const hexOk = ov.theme_color ? /^#([0-9a-fA-F]{3}){1,2}$/.test(ov.theme_color) : false const theme_color = hexOk ? (ov.theme_color as string) : manifestBase.theme_color const manifestMerged = { name, short_name, start_url, display, theme_color } const manifestJson = JSON.stringify(manifestMerged, null, 2) out['site.webmanifest'] = toBytes(manifestJson) // Google Sheets 템플릿: 기본 auto. overrides.sheetsMode로 강제 포함/제외 가능 const mode = overrides?.sheetsMode ?? 'auto' const action = page.form?.actionUrl?.trim() const hasAction = !!(action && action.length > 0) const shouldInclude = mode === 'include' || (mode === 'auto' && !hasAction) if (shouldInclude) { const code = `/** * Google Apps Script Web App for form submissions * - Deploy as Web App and set URL as form action if needed. */ function doPost(e){ // TODO: handle submission to Google Sheets return ContentService.createTextOutput('OK') } ` const readme = `Google Sheets Apps Script Template\n\n1) Apps Script에서 새 프로젝트를 만들고 아래 Code.gs를 붙여넣으세요.\n2) 배포 > 웹 앱으로 배포 후 URL을 복사해 사용하세요.` out['sheets/Code.gs'] = toBytes(code) out['sheets/README.txt'] = toBytes(readme) } return out }