Something went wrong. Try again.
a tool for shared writing and social publishing
Something went wrong. Try again.
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import { AtUri } from "@atproto/syntax";import { PubLeafletPublication } from "lexicons/api";import { isProductionDomain } from "src/utils/isProductionDeployment";import { Json } from "supabase/database.types";import { normalizePublicationRecord, isLeafletPublication, type NormalizedDocument, type NormalizedPublication,} from "src/utils/normalizeRecords";
type PublicationInput = | { uri: string; record: Json | NormalizedPublication | null } | { uri: string; record: unknown };
/** * Gets the public URL for a publication. * Works with both pub.leaflet.publication and site.standard.publication records. */export function getPublicationURL(pub: PublicationInput): string { const normalized = normalizePublicationRecord(pub.record);
// If we have a normalized record with a URL (site.standard format), use it. // Only in production though — like base_path below, the record's URL points // at the live custom domain, and dev/preview should stay on their own host // (a custom-domain redirect would kick off the cross-site auth dance there). if (normalized?.url && isProductionDomain()) { return normalized.url; }
// Fall back to checking raw record for legacy base_path if ( isLeafletPublication(pub.record) && pub.record.base_path && isProductionDomain() ) { return `https://${pub.record.base_path}`; }
return getBasePublicationURL(pub);}
export function getBasePublicationURL(pub: PublicationInput): string { const normalized = normalizePublicationRecord(pub.record); const aturi = new AtUri(pub.uri);
//use rkey, fallback to name const name = aturi.rkey || normalized?.name; return `/lish/${aturi.host}/${encodeURIComponent(name || "")}`;}
/** * Gets the full URL for a document. * Always appends the document's path property. */export function getDocumentURL( doc: NormalizedDocument, docUri: string, publication?: PublicationInput | NormalizedPublication | null,): string { let path = doc.path || "/" + new AtUri(docUri).rkey; if (path[0] !== "/") path = "/" + path;
let base: string; if (!publication) { base = doc.site; } else if ( // Already-normalized publications: use URL directly (publication as NormalizedPublication).$type === "site.standard.publication" ) { base = (publication as NormalizedPublication).url || doc.site; } else { // Raw publication input: delegate to getPublicationURL for full resolution base = getPublicationURL(publication as PublicationInput); } return base.replace(/\/+$/, "") + path;}