Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newsletters: add draft feature #61

Merged
merged 2 commits into from
Jul 24, 2024
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
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
1 change: 1 addition & 0 deletions src/content/newsletters/2024-07-09.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
description: Ladybird browser initiative launched
date: 2024-07-09
draft: true
---

# Content goes here
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);
}
});
});
});