// One-off export of nate.rip posts into base/: markdown + rendered HTML + local images. // Run from the site repo so its markdown pipeline resolves: // cd ~/code/iamnbutler.github.io && npx tsx ~/code/stylize/scripts/snapshot.ts import { createHash } from 'node:crypto'; import { existsSync, mkdirSync, writeFileSync } from 'node:fs'; import { join, resolve } from 'node:path'; import { renderMarkdown } from '../../iamnbutler.github.io/src/lib/markdown.ts'; const DID = 'did:plc:5dnwnjydruv7wmbi33xchkr6'; const PDS = 'https://morel.us-east.host.bsky.network'; const SITE = `at://${DID}/site.standard.publication/self`; const EXT: Record = { 'image/jpeg': 'jpg', 'image/png': 'png', 'image/gif': 'gif', 'image/webp': 'webp' }; const BASE = resolve(import.meta.dirname, '../base'); mkdirSync(join(BASE, 'media'), { recursive: true }); mkdirSync(join(BASE, 'posts'), { recursive: true }); async function download(url: string): Promise { const res = await fetch(url); if (!res.ok) { console.warn(`skip ${res.status} ${url}`); return null; } const ext = EXT[res.headers.get('content-type')?.split(';')[0] ?? ''] ?? 'bin'; const name = `${createHash('sha1').update(url).digest('hex').slice(0, 12)}.${ext}`; const file = join(BASE, 'media', name); if (!existsSync(file)) writeFileSync(file, Buffer.from(await res.arrayBuffer())); return `media/${name}`; } const slugify = (s: string) => s.toLowerCase().normalize('NFKD').replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, ''); const records: any[] = []; let cursor = ''; do { const res = await fetch(`${PDS}/xrpc/com.atproto.repo.listRecords?repo=${DID}&collection=site.standard.document&limit=100${cursor ? `&cursor=${cursor}` : ''}`); const data = await res.json(); records.push(...data.records); cursor = data.cursor ?? ''; } while (cursor); const posts = []; for (const { value: v } of records) { if (v.site !== SITE || (v.fragmentType ?? 'post') !== 'post') continue; let markdown: string = (v.content?.text ?? v.textContent ?? '').trim(); if (!markdown) continue; for (const [, url] of markdown.matchAll(/!\[[^\]]*\]\((https?:[^)\s]+)\)/g)) { const local = await download(url); if (local) markdown = markdown.split(url).join(local); } const slug = slugify(v.title); const date = v.publishedAt.slice(0, 10); writeFileSync(join(BASE, 'posts', `${slug}.md`), `---\ntitle: ${JSON.stringify(v.title)}\ndate: ${date}\n---\n\n${markdown}\n`); posts.push({ slug, title: v.title, date, html: await renderMarkdown(markdown) }); console.log(`${date} ${v.title}`); } posts.sort((a, b) => b.date.localeCompare(a.date)); writeFileSync(join(BASE, 'posts.json'), JSON.stringify(posts, null, 2)); console.log(`wrote ${posts.length} posts`);