Skip to content

Commit

Permalink
Render draft posts
Browse files Browse the repository at this point in the history
  • Loading branch information
ncjones committed Jun 13, 2024
1 parent 55c9144 commit 28e6ef9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
75 changes: 39 additions & 36 deletions src/data/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,51 @@ type MarkdownInstance = import('astro').MarkdownInstance<any>;
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: `<language>en-us</language>`,
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: `<language>en-us</language>`,
items: publishedPosts(posts).map((post: Post) => ({
title: post.title,
description: post.desc,
link: post.slug,
pubDate: post.date,
})),
}
}

4 changes: 2 additions & 2 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -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 );
---

<Blog>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/posts/[post].astro
Original file line number Diff line number Diff line change
@@ -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));
Expand Down

0 comments on commit 28e6ef9

Please sign in to comment.