Skip to content

Commit

Permalink
newsletters: add draft feature
Browse files Browse the repository at this point in the history
  • Loading branch information
samfry13 committed Jul 20, 2024
1 parent 44aae87 commit 346ef9f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/content/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const newsletter = defineCollection({
schema: z.object({
description: z.string().max(280),
date: z.date(),
draft: z.boolean().optional(),
}),
});

Expand Down
11 changes: 11 additions & 0 deletions src/content/newsletters/2024-07-20.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
description: Ladybird browser initiative launched
date: 2024-07-20
draft: true
---

# Content goes here

This is a test of the draft feature

This page will only appear in development, and not in production
4 changes: 3 additions & 1 deletion src/pages/newsletter/[...slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import Layout from "@/layouts/newsletter.astro";
import { markdown } from "@/components/md";
export async function getStaticPaths() {
const newsletters = await getCollection("newsletters");
const newsletters = await getCollection("newsletters", ({ data }) =>
import.meta.env.PROD ? data.draft !== true : true
);
return newsletters.map((newsletter) => ({
params: { slug: newsletter.data.date.toISOString().split("T")[0] },
props: { newsletter },
Expand Down
29 changes: 29 additions & 0 deletions tests/newsletters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect, test, describe } from "bun:test";
import fs from "fs";
import path from "path";

const rootDir: string = path.join(__dirname, "../");

describe("Newsletters", () => {
const srcDir = path.join(rootDir, "/src/content/newsletters");
const distDir = path.join(rootDir, "/dist/newsletter");

const mdFiles = fs
.readdirSync(srcDir)
.filter((file) => file.endsWith(".mdx"));

test("Draft pages should be excluded in build", async () => {
mdFiles.forEach((file) => {
const filePath = path.join(srcDir, file);
const htmlFile = file.replace(".mdx", "/index.html");
const htmlFilePath = path.join(distDir, htmlFile);

const fileContent = fs.readFileSync(filePath);
if (fileContent.toString().includes("draft: true")) {
expect(fs.existsSync(htmlFilePath)).toBe(false);
} else {
expect(fs.existsSync(htmlFilePath)).toBe(true);
}
});
});
});

0 comments on commit 346ef9f

Please sign in to comment.