Something went wrong. Try again.
a tool for shared writing and social publishing
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109import { v7 } from "uuid";import { gateContent, type ImportImage } from "src/import/content";import { buildPage, coverImage, publicationPageFacts, wrapInLeaflet, type ImportedLeaflet, type ResolveImage,} from "src/import/leaflet";import type { LeafletFact } from "src/utils/insertLeaflet";import { ghostHtmlToBlocks } from "./ghostToBlocks";import { ghostExcerpt, resolveGhostUrl, type GhostPost,} from "./parseGhostExport";
// The entities and facts of one page's worth of content: the page entity, its// blocks, and (for a Ghost page) the nav facts that make it a publication// page at /slug.export type GhostPage = { pageId: string; route: string; entities: string[]; facts: LeafletFact[]; coverImage: ImportImage | null; imageCount: number;};
export type GhostLeaflet = ImportedLeaflet & { ghostId: string; slug: string };
// Build a Ghost post or page as a single linear-document page. `resolveImage`// decides where each image's bytes live (Ghost's own URL for a preview, a// Leaflet upload for a real import).//// Posts Ghost restricted to members or paid tiers are placed entirely behind// a members-only delimiter, unless a paywall card already marks where the// public preview ends; their feature image is returned as the cover. Pages// have neither: publication pages can't be gated, and a page's feature image// becomes its first block.export async function ghostPostToPage( post: GhostPost, siteUrl: string, resolveImage: ResolveImage,): Promise<GhostPage> { const pageId = v7(); const isPage = post.type === "page"; const content = ghostHtmlToBlocks(post.html, { siteUrl, parent: pageId }); if (!isPage && post.visibility !== "public") gateContent(content, pageId);
const featureImage = coverImage( post.featureImage && resolveGhostUrl(post.featureImage, siteUrl), ); if (featureImage && isPage) { content.blocks.unshift({ entityID: featureImage.entityID, parent: pageId, type: "image", facts: [ { entity: featureImage.entityID, attribute: "block/type", data: { type: "block-type-union", value: "image" }, }, ], }); content.images.unshift(featureImage); } const page = await buildPage( pageId, content, isPage ? null : featureImage, resolveImage, ); const facts = page.facts; const route = `/${post.slug}`; if (isPage) facts.push(...publicationPageFacts(pageId, route, post.title));
return { pageId, route, entities: page.entities, facts, coverImage: page.cover, imageCount: page.imageCount, };}
// Wrap a page in a complete standalone leaflet: a root whose only page is the// content, with a post's feature image as the cover. This is the shape a post// is imported in, and the shape both posts and pages are previewed in.export async function ghostPostToLeaflet( post: GhostPost, siteUrl: string, resolveImage: ResolveImage,): Promise<GhostLeaflet> { const page = await ghostPostToPage(post, siteUrl, resolveImage); return { ...wrapInLeaflet({ ...page, cover: page.coverImage }), ghostId: post.id, slug: post.slug, title: post.title, description: ghostExcerpt(post), tags: post.tags, publishedAt: post.publishedAt ?? post.createdAt, };}