Something went wrong. Try again.
A CLI for publishing standard.site documents to ATProto sequoia.pub
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345import { describe, expect, it, spyOn } from "bun:test";import { log } from "@clack/prompts";import { collectPublishedPosts, Injected, injectLinkTags, matchPostsToHtmlFiles,} from "../src/commands/inject";
const atUri = "at://did:plc:abc123/app.bsky.feed.post/xyz";const publicationUri = "at://did:plc:def456/app.bsky.feed.generator/main";
describe("injectLinkTags", () => { describe("neither tag needs injection", () => { it("returns AlreadyPresent when both tags already exist", () => { const content = `<html><head> <link rel="site.standard.document" href="${atUri}"> <link rel="site.standard.publication" href="${publicationUri}"></head></html>`; const result = injectLinkTags( false, "test.html", content, atUri, publicationUri, ); expect(result).toBe(Injected.AlreadyPresent); }); });
describe("one tag needs injection", () => { it("injects only documentLinkTag when publicationLinkTag is already present", () => { const content = `<html><head> <link rel="site.standard.publication" href="${publicationUri}"></head></html>`; const result = injectLinkTags( false, "test.html", content, atUri, publicationUri, ); expect(typeof result).toBe("string"); expect(result as string).toContain( `<link rel="site.standard.document" href="${atUri}">`, ); expect( result as string, ).not.toContain(`<link rel="site.standard.publication" href="${publicationUri}"> <link rel="site.standard.publication"`); });
it("injects only publicationLinkTag when documentLinkTag is already present", () => { const content = `<html><head> <link rel="site.standard.document" href="${atUri}"></head></html>`; const result = injectLinkTags( false, "test.html", content, atUri, publicationUri, ); expect(typeof result).toBe("string"); expect(result as string).toContain( `<link rel="site.standard.publication" href="${publicationUri}">`, ); }); });
describe("both tags need injection", () => { it("injects both tags when neither is present", () => { const content = "<html><head>\n</head></html>"; const result = injectLinkTags( false, "test.html", content, atUri, publicationUri, ); expect(typeof result).toBe("string"); expect(result as string).toContain( `<link rel="site.standard.document" href="${atUri}">`, ); expect(result as string).toContain( `<link rel="site.standard.publication" href="${publicationUri}">`, ); });
it("injects tags before </head>", () => { const content = "<html><head>\n</head><body></body></html>"; const result = injectLinkTags( false, "test.html", content, atUri, publicationUri, ) as string; const headCloseIndex = result.indexOf("</head>"); expect(result.indexOf('rel="site.standard.document"')).toBeLessThan( headCloseIndex, ); expect(result.indexOf('rel="site.standard.publication"')).toBeLessThan( headCloseIndex, ); });
it("returns Skipped when no </head> is found", () => { const warnSpy = spyOn(log, "warn").mockImplementation(() => {}); const content = "<html><body>No head tag here</body></html>"; const result = injectLinkTags( false, "test.html", content, atUri, publicationUri, ); expect(result).toBe(Injected.Skipped); expect(warnSpy).toHaveBeenCalledWith( " No </head> found in test.html, skipping", ); warnSpy.mockRestore(); });
it("returns Faked and does not modify content during dry run", () => { const messageSpy = spyOn(log, "message").mockImplementation(() => {}); const content = "<html><head>\n</head></html>"; const result = injectLinkTags( true, "test.html", content, atUri, publicationUri, ); expect(result).toBe(Injected.Faked); expect(messageSpy).toHaveBeenCalledWith(" Would inject into: test.html"); expect(messageSpy).toHaveBeenCalledWith( ` <link rel="site.standard.document" href="${atUri}">`, ); expect(messageSpy).toHaveBeenCalledWith( ` <link rel="site.standard.publication" href="${publicationUri}">`, ); messageSpy.mockRestore(); }); });});
describe("matchPostsToHtmlFiles", () => { const otherAtUri = "at://did:plc:abc123/site.standard.document/other";
it("prefers the generated post path over a section index with the same slug", () => { // https://tangled.org/stevedylan.dev/sequoia/issues/62 // Zola: content/blog/2024-12-17-blog/index.md with slug = "blog" // builds to public/blog/blog/index.html, alongside the section index // public/blog/index.html const { matches } = matchPostsToHtmlFiles( ["index.html", "blog/index.html", "blog/blog/index.html"], [ { filePath: "content/blog/2024-12-17-blog/index.md", slug: "blog", atUri, }, ], { contentDir: "./content", pathPrefix: "/blog" }, );
expect(matches.get("blog/blog/index.html")).toBe(atUri); expect(matches.has("blog/index.html")).toBe(false); expect(matches.has("index.html")).toBe(false); });
it("matches the generated path when pathPrefix does not describe the output", () => { const { matches } = matchPostsToHtmlFiles( ["blog/index.html", "blog/blog/index.html"], [ { filePath: "content/blog/2024-12-17-blog/index.md", slug: "blog", atUri, }, ], { contentDir: "./content", pathPrefix: "/posts" }, );
expect(matches.get("blog/blog/index.html")).toBe(atUri); expect(matches.has("blog/index.html")).toBe(false); });
it("matches the configured pathPrefix path", () => { const { matches } = matchPostsToHtmlFiles( ["posts/my-post/index.html", "my-post/index.html"], [{ filePath: "content/my-post.md", slug: "my-post", atUri }], { contentDir: "./content", pathPrefix: "/posts" }, );
expect(matches.get("posts/my-post/index.html")).toBe(atUri); expect(matches.has("my-post/index.html")).toBe(false); });
it("matches flat HTML files", () => { const { matches } = matchPostsToHtmlFiles( ["posts/my-post.html"], [{ filePath: "content/my-post.md", slug: "my-post", atUri }], { contentDir: "./content", pathPrefix: "/posts" }, );
expect(matches.get("posts/my-post.html")).toBe(atUri); });
it("matches nested slugs", () => { const { matches } = matchPostsToHtmlFiles( ["other/my-other-post/index.html"], [ { filePath: "content/other/my-other-post.md", slug: "other/my-other-post", atUri, }, ], { contentDir: "./content", pathPrefix: "" }, );
expect(matches.get("other/my-other-post/index.html")).toBe(atUri); });
it("falls back to the slug when the output layout is unrelated to the source", () => { const { matches } = matchPostsToHtmlFiles( ["archive/my-post/index.html"], [{ filePath: "content/2024/my-post.md", slug: "archive/my-post", atUri }], { contentDir: "./content" }, );
expect(matches.get("archive/my-post/index.html")).toBe(atUri); });
it("falls back to the last slug segment", () => { const { matches } = matchPostsToHtmlFiles( ["writing/my-post/index.html"], [{ filePath: "content/blog/my-post.md", slug: "blog/my-post", atUri }], { contentDir: "./content", pathPrefix: "" }, );
expect(matches.get("writing/my-post/index.html")).toBe(atUri); });
it("skips files two posts match equally well", () => { const { matches, warnings } = matchPostsToHtmlFiles( ["writing/my-post/index.html"], [ { filePath: "content/blog/my-post.md", slug: "blog/my-post", atUri }, { filePath: "content/notes/my-post.md", slug: "notes/my-post", atUri: otherAtUri, }, ], { contentDir: "./content", pathPrefix: "" }, );
expect(matches.size).toBe(0); expect(warnings).toHaveLength(1); expect(warnings[0]).toContain("writing/my-post/index.html"); });
it("still matches the better of two posts claiming one file", () => { const { matches, warnings } = matchPostsToHtmlFiles( ["blog/my-post/index.html", "notes/my-post/index.html"], [ { filePath: "content/blog/my-post.md", slug: "blog/my-post", atUri }, { filePath: "content/notes/my-post.md", slug: "notes/my-post", atUri: otherAtUri, }, ], { contentDir: "./content", pathPrefix: "" }, );
expect(matches.get("blog/my-post/index.html")).toBe(atUri); expect(matches.get("notes/my-post/index.html")).toBe(otherAtUri); expect(warnings).toHaveLength(0); });
it("falls back to the last slug segment for a flat output layout", () => { const { matches } = matchPostsToHtmlFiles( ["my-post.html"], [{ filePath: "content/blog/my-post.md", slug: "blog/my-post", atUri }], { contentDir: "./content", pathPrefix: "" }, );
expect(matches.get("my-post.html")).toBe(atUri); });
it("matches a templated path the slug alone does not describe", () => { const { matches } = matchPostsToHtmlFiles( ["2024/12/my-post/index.html"], [{ filePath: "content/2024/my-post.md", slug: "my-post", atUri }], { contentDir: "./content", pathPrefix: "/posts", pathTemplate: "{year}/{month}/{slug}", }, );
expect(matches.get("2024/12/my-post/index.html")).toBe(atUri); });
it("matches a root index.html for a top-level page", () => { const { matches } = matchPostsToHtmlFiles( ["index.html", "about/index.html"], [{ filePath: "content/about.md", slug: "about", atUri }], { contentDir: "./content", pathPrefix: "" }, );
expect(matches.get("about/index.html")).toBe(atUri); expect(matches.has("index.html")).toBe(false); });});
describe("collectPublishedPosts", () => { it("skips unpublished posts and derives a slug from old state entries", () => { const posts = collectPublishedPosts({ posts: { "./content/blog/My-Post.md": { contentHash: "a", atUri }, "./content/blog/other.md": { contentHash: "b", atUri: "at://did:plc:abc123/site.standard.document/other", slug: "blog/other", }, "./content/blog/draft.md": { contentHash: "c" }, }, });
expect(posts).toEqual([ { filePath: "./content/blog/My-Post.md", slug: "my-post", atUri }, { filePath: "./content/blog/other.md", slug: "blog/other", atUri: "at://did:plc:abc123/site.standard.document/other", }, ]); });});