diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts index 28abbba..781e0dc 100644 --- a/packages/cli/src/commands/init.ts +++ b/packages/cli/src/commands/init.ts @@ -341,7 +341,7 @@ export const initCommand = command({ imagesDir: siteConfig.imagesDir || undefined, publicDir: siteConfig.publicDir || "./public", outputDir: siteConfig.outputDir || "./dist", - pathPrefix: siteConfig.pathPrefix || "/posts", + pathPrefix: siteConfig.pathPrefix ?? "/posts", publicationUri, pdsUrl, frontmatter: frontmatterMapping, diff --git a/packages/cli/src/commands/update.ts b/packages/cli/src/commands/update.ts index 017244d..11257ea 100644 --- a/packages/cli/src/commands/update.ts +++ b/packages/cli/src/commands/update.ts @@ -70,7 +70,7 @@ async function updateConfigFlow( const configSummary = [ `Site URL: ${config.siteUrl}`, `Content Dir: ${config.contentDir}`, - `Path Prefix: ${config.pathPrefix || "/posts"}`, + `Path Prefix: ${config.pathPrefix ?? "/posts"}`, `Publication URI: ${config.publicationUri}`, config.imagesDir ? `Images Dir: ${config.imagesDir}` : null, config.outputDir ? `Output Dir: ${config.outputDir}` : null, @@ -194,14 +194,14 @@ async function editSiteSettings( const pathPrefix = exitOnCancel( await text({ message: "URL path prefix for posts:", - initialValue: config.pathPrefix || "/posts", + initialValue: config.pathPrefix ?? "/posts", }), ); return { ...config, siteUrl, - pathPrefix: pathPrefix || undefined, + pathPrefix, }; } diff --git a/packages/cli/src/lib/markdown.ts b/packages/cli/src/lib/markdown.ts index 9a7650f..2b80ba8 100644 --- a/packages/cli/src/lib/markdown.ts +++ b/packages/cli/src/lib/markdown.ts @@ -231,8 +231,8 @@ export function resolvePostPath( if (pathTemplate) { return resolvePathTemplate(pathTemplate, post); } - const prefix = pathPrefix || "/posts"; - return `${prefix}/${post.slug}`; + const prefix = pathPrefix ?? "/posts"; + return prefix ? `${prefix}/${post.slug}` : `/${post.slug}`; } export async function getContentHash(content: string): Promise { diff --git a/packages/cli/test/markdown.test.ts b/packages/cli/test/markdown.test.ts index f7aa692..905df9d 100644 --- a/packages/cli/test/markdown.test.ts +++ b/packages/cli/test/markdown.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from "bun:test"; -import { parseFrontmatter } from "../src/lib/markdown"; +import { parseFrontmatter, resolvePostPath } from "../src/lib/markdown"; +import type { BlogPost } from "../src/lib/types"; describe("parseFrontmatter", () => { describe("delimiters", () => { @@ -385,3 +386,32 @@ title: Post }); }); }); + +describe("resolvePostPath", () => { + const post: BlogPost = { + filePath: "/tmp/hello.md", + slug: "hello-world", + frontmatter: { title: "Hello" } as BlogPost["frontmatter"], + content: "", + rawContent: "", + rawFrontmatter: {}, + }; + + it("defaults to /posts when pathPrefix is undefined", () => { + expect(resolvePostPath(post)).toBe("/posts/hello-world"); + }); + + it("uses custom prefix when provided", () => { + expect(resolvePostPath(post, "/blog")).toBe("/blog/hello-world"); + }); + + it("omits prefix when pathPrefix is an empty string", () => { + expect(resolvePostPath(post, "")).toBe("/hello-world"); + }); + + it("pathTemplate overrides pathPrefix", () => { + expect(resolvePostPath(post, "", "/custom/{slug}")).toBe( + "/custom/hello-world", + ); + }); +});