Something went wrong. Try again.
🌿 Collaborative wiki on ATProto forked from juprodh.me/lichen.wiki
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566import type { RevisionBlob } from "../atproto/pds.ts";
const BLOB_URL_RE = /!\[[^\]]*\]\(\/blob\/(did:[^/]+)\/([^)\s]+)\)/g;
interface BlobRef { did: string; cid: string;}
export function extractBlobRefs(content: string): BlobRef[] { const seen = new Set<string>(); const refs: BlobRef[] = [];
for (const match of content.matchAll(BLOB_URL_RE)) { const did = match[1]; const cid = match[2]; if (!did || !cid) continue; if (!seen.has(cid)) { seen.add(cid); refs.push({ did, cid }); } }
return refs;}
export interface BlobMeta { mimeType: string; size: number;}
export function parseBlobMetadata( raw: string | null,): Record<string, BlobMeta> { if (!raw) return {}; try { const parsed = JSON.parse(raw); if (typeof parsed === "object" && parsed !== null) { return parsed as Record<string, BlobMeta>; } } catch { // ignore malformed JSON } return {};}
export function buildBlobsForContent( content: string, blobMeta: Record<string, BlobMeta>,): RevisionBlob[] { const refs = extractBlobRefs(content); const blobs: RevisionBlob[] = []; for (const ref of refs) { const meta = blobMeta[ref.cid]; if (meta) { blobs.push({ $type: "blob", ref: { $link: ref.cid }, mimeType: meta.mimeType, size: meta.size, }); } } return blobs;}