Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions dist/public/syndication-export.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/public/syndication-export.js.map

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions src/public/syndication-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ export async function exportPostArtifact(client: PublicClient, postUrl: string):
} catch { /* Preserve any visible fallback text when metadata is malformed. */ }
if (label) node.replaceWith(`<span>${escapeHtml(label)}</span>`);
});
// Image galleries are divs whose photos live only in data-attrs, so the
// serializer below dropped them. Export each photo as its own image marker.
$(".image-gallery-embed").each((_index, element) => {
const node = $(element);
let gallery: { images?: unknown; caption?: unknown } | undefined;
try {
const attributes = JSON.parse(node.attr("data-attrs") ?? "") as { gallery?: { images?: unknown; caption?: unknown } };
gallery = attributes.gallery;
} catch { /* A malformed gallery has no safe portable representation. */ }
const urls = Array.isArray(gallery?.images)
? gallery.images.flatMap((image: unknown) => typeof image === "object" && image !== null && typeof (image as { src?: unknown }).src === "string" ? [(image as { src: string }).src] : [])
: [];
if (!urls.length) { node.remove(); return; }
const caption = typeof gallery?.caption === "string" ? gallery.caption.trim() : "";
const markers = urls.map((url) => { const marker = `<p>[[NORI_IMAGE:${images.length}]]</p>`; images.push({ url, caption: "" }); return marker; });
node.replaceWith(`${markers.join("")}${caption ? `<p><em>${escapeHtml(caption)}</em></p>` : ""}`);
});
$(".captioned-image-container").each((_index, element) => {
const url = $(element).find("img").first().attr("src") ?? "";
if (!url || url.includes("missing-image")) { $(element).remove(); return; }
Expand Down
21 changes: 21 additions & 0 deletions tests/syndication-export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ test("exports a public Substack post as a portable article bundle", async () =>
expect(bundle.html).not.toContain("button-wrapper");
});

test("exports every photo in an image gallery as an image marker", async () => {
const gallery = JSON.stringify({ gallery: { images: [{ type: "image/jpeg", src: "https://cdn.example.com/a.jpeg" }, { type: "image/jpeg", src: "https://cdn.example.com/b.jpeg" }], caption: "Event photos", staticGalleryImage: { src: "https://cdn.example.com/static.png" } }, isEditorNode: true }).replace(/"/g, "&quot;");
const server = await withHttpServer((_request, response) => {
response.setHeader("content-type", "application/json");
response.end(JSON.stringify({
title: "Gallery",
canonical_url: `${server.origin}/p/gallery`,
body_html: `<p>Before.</p><div class="image-gallery-embed" data-attrs="${gallery}"></div><p>After.</p>`,
}));
});
closers.push(server.close);
const { output } = await tempOutput("gallery.json");

const result = await runCli(["post", "export", "--url", `${server.origin}/p/gallery`, "--output", output]);

expect(result.code).toBe(0);
const bundle = JSON.parse(await readFile(output, "utf8"));
expect(bundle.images).toEqual([{ url: "https://cdn.example.com/a.jpeg", caption: "" }, { url: "https://cdn.example.com/b.jpeg", caption: "" }]);
expect(bundle.html).toBe("<p>Before.</p><p>[[NORI_IMAGE:0]]</p><p>[[NORI_IMAGE:1]]</p><p><em>Event photos</em></p><p>After.</p>");
});

test("exports only recent top-level Notes from the requested author", async () => {
const now = Date.now();
const note = (overrides: Record<string, unknown>) => ({
Expand Down
Loading