From 28e6ef9054250b3225619795ab8df60a5b7b67b2 Mon Sep 17 00:00:00 2001 From: Nathan Jones Date: Fri, 14 Jun 2024 09:03:57 +1200 Subject: [PATCH] Render draft posts --- src/data/post.ts | 75 +++++++++++++++++++----------------- src/pages/index.astro | 4 +- src/pages/posts/[post].astro | 4 +- 3 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/data/post.ts b/src/data/post.ts index b3313e5..5c908ed 100644 --- a/src/data/post.ts +++ b/src/data/post.ts @@ -3,48 +3,51 @@ type MarkdownInstance = import('astro').MarkdownInstance; const { MODE } = import.meta.env; export type Post = { - title: string, - slug: string, - desc: string, - author: string, - timestamp: number, - draft: boolean, - date: string, - file: URL, - img: URL, + title: string, + slug: string, + desc: string, + author: string, + timestamp: number, + draft: boolean, + date: string, + file: URL, + img: URL, } -export function single(post: MarkdownInstance): Post { - const slug = post.file.split('/').reverse()[0].replace('.md', ''); - return { - ...post.frontmatter, - Content: post.Content, - slug: slug, - draft: post.file.split('/').reverse()[1] === 'drafts', - timestamp: (new Date(post.frontmatter.date)).valueOf() - } +function createPost(post: MarkdownInstance): Post { + const slug = post.file.split('/').reverse()[0].replace('.md', ''); + return { + ...post.frontmatter, + Content: post.Content, + slug: slug, + draft: post.file.split('/').reverse()[1] === 'drafts', + timestamp: (new Date(post.frontmatter.date)).valueOf() + } } -export function published(posts: MarkdownInstance[]): Post[] { - return posts - .filter(post => post.frontmatter.title) - .map(post => single(post)) - .filter(post => MODE === 'development' || !post.draft) - .sort((a, b) => b.timestamp - a.timestamp) +export function allPosts(posts: MarkdownInstance[]): Post[] { + return posts + .filter(post => post.frontmatter.title) + .map(createPost) + .sort((a, b) => b.timestamp - a.timestamp); +} + +export function publishedPosts(posts: MarkdownInstance[]): Post[] { + return allPosts(posts).filter(post => !post.draft); } export function getRSS(posts: MarkdownInstance[]) { - return { - title: 'Astro Blog', - description: 'Astro Blog Feed', - stylesheet: true, - customData: `en-us`, - items: published(posts).map((post: Post) => ({ - title: post.title, - description: post.desc, - link: post.slug, - pubDate: post.date, - })), - } + return { + title: 'Astro Blog', + description: 'Astro Blog Feed', + stylesheet: true, + customData: `en-us`, + items: publishedPosts(posts).map((post: Post) => ({ + title: post.title, + description: post.desc, + link: post.slug, + pubDate: post.date, + })), + } } diff --git a/src/pages/index.astro b/src/pages/index.astro index 1fd4972..21f21d5 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -1,10 +1,10 @@ --- -import { published } from "@data/post" +import { publishedPosts } from "@data/post" import Blog from "@layouts/blog"; import Preview from "@components/blog/preview"; const markdownFiles = await Astro.glob('../content/**/*.md') -const posts = published( markdownFiles ); +const posts = publishedPosts( markdownFiles ); --- diff --git a/src/pages/posts/[post].astro b/src/pages/posts/[post].astro index 7e5598c..9ce3e23 100644 --- a/src/pages/posts/[post].astro +++ b/src/pages/posts/[post].astro @@ -1,11 +1,11 @@ --- import Blog from "@layouts/blog"; import Date from "@components/utilities/Date"; -import { published, getRSS } from "@data/post" +import { allPosts, getRSS } from "@data/post" export async function getStaticPaths({ rss }) { const markdownFiles = await Astro.glob('../../content/**/*.md'); - const posts = published( markdownFiles ); + const posts = allPosts( markdownFiles ); rss(getRSS(markdownFiles));