|
| 1 | +import { useState } from 'react'; |
| 2 | +import { Ghost, Download, FileText, FileCode } from 'lucide-react'; |
| 3 | +import { Dropzone } from '@/components/ui/Dropzone'; |
| 4 | +import { Button } from '@/components/ui/Button'; |
| 5 | +import { Alert } from '@/components/ui/Alert'; |
| 6 | +import { downloadService } from '@/services/download'; |
| 7 | +import type { Lang } from '@/i18n/config'; |
| 8 | +import { |
| 9 | + parseGhostExport, selectPosts, toMarkdown, toHtmlPage, |
| 10 | + type NormalizedPost, |
| 11 | +} from '@/tools/dev/ghost-export.lib'; |
| 12 | + |
| 13 | +type Format = 'md' | 'html' | 'both'; |
| 14 | + |
| 15 | +const input = 'w-full border-2 border-border bg-muted px-3 py-2 text-sm outline-none focus:shadow-brutal-sm'; |
| 16 | + |
| 17 | +const TR: Record<Lang, { |
| 18 | + intro: string; how: string; drop: string; dropSub: string; |
| 19 | + loaded: (n: number, d: number, p: number) => string; |
| 20 | + optionsH: string; drafts: string; pages: string; formatH: string; |
| 21 | + md: string; mdNote: string; html: string; htmlNote: string; both: string; |
| 22 | + imageNote: string; generate: string; working: string; another: string; |
| 23 | + errRead: string; errGen: string; |
| 24 | +}> = { |
| 25 | + en: { |
| 26 | + intro: 'Turn a Ghost export into a folder of Markdown and/or standalone HTML files — one per post, with YAML frontmatter and your tags. Everything runs in your browser; nothing is uploaded.', |
| 27 | + how: 'In Ghost admin: Settings → Migration → Export, then drop the downloaded JSON here.', |
| 28 | + drop: 'Drop your Ghost export (.json)', dropSub: 'Processed on your device — no upload.', |
| 29 | + loaded: (n, d, p) => `${n} posts found — ${d} draft${d === 1 ? '' : 's'}, ${p} page${p === 1 ? '' : 's'}.`, |
| 30 | + optionsH: 'What to include', drafts: 'Include drafts (→ drafts/ folder)', pages: 'Include pages (→ pages/ folder)', |
| 31 | + formatH: 'Output format', |
| 32 | + md: 'Markdown', mdNote: '.md + YAML frontmatter (for Astro/Hugo/Jekyll…)', |
| 33 | + html: 'HTML', htmlNote: 'standalone .html pages (ready to host on R2/static)', |
| 34 | + both: 'Both', |
| 35 | + imageNote: 'Note: Ghost exports don’t include image files, only their URLs — so image links stay pointing at your Ghost/CDN. Save the images separately if you’re shutting the site down.', |
| 36 | + generate: 'Convert & download ZIP', working: 'Converting…', another: 'Choose another file', |
| 37 | + errRead: 'Could not read this file.', errGen: 'Could not convert the export.', |
| 38 | + }, |
| 39 | + id: { |
| 40 | + intro: 'Ubah ekspor Ghost menjadi folder berisi berkas Markdown dan/atau HTML mandiri — satu per pos, dengan frontmatter YAML dan tag Anda. Semuanya berjalan di browser Anda; tidak ada yang diunggah.', |
| 41 | + how: 'Di admin Ghost: Settings → Migration → Export, lalu letakkan berkas JSON yang terunduh di sini.', |
| 42 | + drop: 'Letakkan ekspor Ghost Anda (.json)', dropSub: 'Diproses di perangkat Anda — tanpa unggahan.', |
| 43 | + loaded: (n, d, p) => `${n} pos ditemukan — ${d} draf, ${p} halaman.`, |
| 44 | + optionsH: 'Yang disertakan', drafts: 'Sertakan draf (→ folder drafts/)', pages: 'Sertakan halaman (→ folder pages/)', |
| 45 | + formatH: 'Format keluaran', |
| 46 | + md: 'Markdown', mdNote: '.md + frontmatter YAML (untuk Astro/Hugo/Jekyll…)', |
| 47 | + html: 'HTML', htmlNote: 'halaman .html mandiri (siap dihosting di R2/statis)', |
| 48 | + both: 'Keduanya', |
| 49 | + imageNote: 'Catatan: ekspor Ghost tidak menyertakan berkas gambar, hanya URL-nya — jadi tautan gambar tetap mengarah ke Ghost/CDN Anda. Simpan gambar secara terpisah jika Anda menutup situs.', |
| 50 | + generate: 'Konversi & unduh ZIP', working: 'Mengonversi…', another: 'Pilih berkas lain', |
| 51 | + errRead: 'Tidak dapat membaca berkas ini.', errGen: 'Tidak dapat mengonversi ekspor.', |
| 52 | + }, |
| 53 | +}; |
| 54 | + |
| 55 | +export default function GhostBackup({ lang = 'en' }: { lang?: Lang }) { |
| 56 | + const t = TR[lang] ?? TR.en; |
| 57 | + const [posts, setPosts] = useState<NormalizedPost[] | null>(null); |
| 58 | + const [includeDrafts, setIncludeDrafts] = useState(true); |
| 59 | + const [includePages, setIncludePages] = useState(true); |
| 60 | + const [format, setFormat] = useState<Format>('md'); |
| 61 | + const [busy, setBusy] = useState(false); |
| 62 | + const [error, setError] = useState(''); |
| 63 | + |
| 64 | + const onDrop = async (files: File[]) => { |
| 65 | + setError(''); |
| 66 | + const f = files[0]; |
| 67 | + if (!f) return; |
| 68 | + try { |
| 69 | + setPosts(parseGhostExport(await f.text())); |
| 70 | + } catch (e) { |
| 71 | + setPosts(null); |
| 72 | + setError(e instanceof Error ? e.message : t.errRead); |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + const generate = async () => { |
| 77 | + if (!posts) return; |
| 78 | + setError(''); |
| 79 | + setBusy(true); |
| 80 | + try { |
| 81 | + const selected = selectPosts(posts, { includeDrafts, includePages }); |
| 82 | + const files: Record<string, Uint8Array> = {}; |
| 83 | + const enc = new TextEncoder(); |
| 84 | + |
| 85 | + if (format === 'md' || format === 'both') { |
| 86 | + const [{ default: Turndown }, gfm] = await Promise.all([ |
| 87 | + import('turndown'), |
| 88 | + import('@joplin/turndown-plugin-gfm'), |
| 89 | + ]); |
| 90 | + const td = new Turndown({ headingStyle: 'atx', codeBlockStyle: 'fenced', bulletListMarker: '-', emDelimiter: '*' }); |
| 91 | + td.use((gfm as { gfm: (s: unknown) => void }).gfm); |
| 92 | + const htmlToMd = (html: string) => td.turndown(html); |
| 93 | + for (const p of selected) { |
| 94 | + const { path, content } = toMarkdown(p, htmlToMd); |
| 95 | + files[path] = enc.encode(content); |
| 96 | + } |
| 97 | + } |
| 98 | + if (format === 'html' || format === 'both') { |
| 99 | + for (const p of selected) { |
| 100 | + const { path, content } = toHtmlPage(p); |
| 101 | + files[path] = enc.encode(content); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + const { zipSync } = await import('fflate'); |
| 106 | + const zipped = zipSync(files, { level: 6 }); |
| 107 | + downloadService.download(new Blob([zipped], { type: 'application/zip' }), 'ghost-backup.zip'); |
| 108 | + } catch (e) { |
| 109 | + setError(e instanceof Error ? e.message : t.errGen); |
| 110 | + } finally { |
| 111 | + setBusy(false); |
| 112 | + } |
| 113 | + }; |
| 114 | + |
| 115 | + const counts = posts |
| 116 | + ? { total: posts.length, drafts: posts.filter(p => p.status !== 'published').length, pages: posts.filter(p => p.type === 'page').length } |
| 117 | + : null; |
| 118 | + |
| 119 | + return ( |
| 120 | + <div className="space-y-4"> |
| 121 | + <p className="text-sm text-muted-foreground">{t.intro}</p> |
| 122 | + |
| 123 | + {!posts && ( |
| 124 | + <> |
| 125 | + <Dropzone onDrop={onDrop} accept=".json,application/json" multiple={false}> |
| 126 | + <div className="space-y-1"> |
| 127 | + <p className="flex items-center justify-center gap-2 text-lg font-bold"><Ghost className="h-5 w-5" /> {t.drop}</p> |
| 128 | + <p className="text-sm text-muted-foreground">{t.dropSub}</p> |
| 129 | + </div> |
| 130 | + </Dropzone> |
| 131 | + <p className="text-xs text-muted-foreground">{t.how}</p> |
| 132 | + </> |
| 133 | + )} |
| 134 | + |
| 135 | + {posts && counts && ( |
| 136 | + <div className="space-y-4"> |
| 137 | + <Alert variant="success">{t.loaded(counts.total, counts.drafts, counts.pages)}</Alert> |
| 138 | + |
| 139 | + <div className="space-y-2 border-2 border-border p-3"> |
| 140 | + <p className="text-sm font-bold uppercase tracking-wide text-muted-foreground">{t.optionsH}</p> |
| 141 | + <label className="flex items-center gap-2 text-sm"><input type="checkbox" checked={includeDrafts} onChange={e => setIncludeDrafts(e.target.checked)} /> {t.drafts}</label> |
| 142 | + <label className="flex items-center gap-2 text-sm"><input type="checkbox" checked={includePages} onChange={e => setIncludePages(e.target.checked)} /> {t.pages}</label> |
| 143 | + </div> |
| 144 | + |
| 145 | + <div className="space-y-2 border-2 border-border p-3"> |
| 146 | + <p className="text-sm font-bold uppercase tracking-wide text-muted-foreground">{t.formatH}</p> |
| 147 | + <div className="grid gap-2 sm:grid-cols-3"> |
| 148 | + {([['md', FileText, t.md, t.mdNote], ['html', FileCode, t.html, t.htmlNote], ['both', Download, t.both, '']] as const).map(([val, Icon, label, note]) => ( |
| 149 | + <button key={val} type="button" onClick={() => setFormat(val)} aria-pressed={format === val} |
| 150 | + className={`flex flex-col gap-1 border-2 border-border p-2 text-left text-sm press-brutal ${format === val ? 'bg-accent text-accent-foreground' : 'bg-muted'}`}> |
| 151 | + <span className="flex items-center gap-1.5 font-bold"><Icon className="h-4 w-4" /> {label}</span> |
| 152 | + {note && <span className={format === val ? 'text-accent-foreground/80' : 'text-muted-foreground'}>{note}</span>} |
| 153 | + </button> |
| 154 | + ))} |
| 155 | + </div> |
| 156 | + </div> |
| 157 | + |
| 158 | + <p className="border-2 border-border bg-muted px-3 py-2 text-xs text-muted-foreground">{t.imageNote}</p> |
| 159 | + |
| 160 | + {error && <Alert variant="error">{error}</Alert>} |
| 161 | + <div className="flex flex-wrap gap-2"> |
| 162 | + <Button onClick={generate} disabled={busy}><Download className="h-4 w-4" /> {busy ? t.working : t.generate}</Button> |
| 163 | + <Button variant="secondary" onClick={() => { setPosts(null); setError(''); }}>{t.another}</Button> |
| 164 | + </div> |
| 165 | + </div> |
| 166 | + )} |
| 167 | + |
| 168 | + {!posts && error && <Alert variant="error">{error}</Alert>} |
| 169 | + </div> |
| 170 | + ); |
| 171 | +} |
0 commit comments