Skip to content

Commit

Permalink
content collections!
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbloxhub committed Jul 24, 2023
1 parent 861402e commit d4ec4bf
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"yaml.schemas": {
"https://json.schemastore.org/github-workflow.json": "file:///mnt/wd-blue-2tb/home/jonahgam/bitbloxhub.github.io/.github/workflows/deploy.yaml"
},
"files.associations": {
"*.mdx": "markdown"
}
}
2 changes: 1 addition & 1 deletion src/components/BlogPost.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const { title, url, minutesRead, pubDate } = Astro.props

<li>
<a href={url}>{title}</a> <br />
Approximately {minutesRead}, published {pubDate.slice(0,10)}
Approximately {minutesRead}, published {pubDate.toISOString().slice(0,10)}
</li>
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
layout: ../../layouts/MarkdownPostLayout.astro
title: "I have a new blog!"
pubDate: 2023-07-22
description: "This is the first post of my new blog."
Expand Down
16 changes: 16 additions & 0 deletions src/content/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { z, defineCollection } from "astro:content"

const blogCollection = defineCollection({
type: "content",
schema: z.object({
title: z.string(),
pubDate: z.date(),
description: z.string(),
author: z.string(),
minutesRead: z.string()
})
})

export const collections = {
"blog": blogCollection
}
1 change: 1 addition & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference types="astro/client" />
2 changes: 1 addition & 1 deletion src/layouts/MarkdownPostLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { frontmatter } = Astro.props
<main class="m-auto p-6 max-w-6xl text-left">
<h1>{frontmatter.title}</h1>
<p>Written by {frontmatter.author}</p>
<p>Published on {frontmatter.pubDate.slice(0,10)}</p>
<p>Published on {frontmatter.pubDate.toISOString().slice(0,10)}</p>
<p>Approximately {frontmatter.minutesRead}</p>
<slot />
</main>
Expand Down
5 changes: 3 additions & 2 deletions src/pages/blog.astro
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
---
import { getCollection } from "astro:content"
import Layout from "../layouts/Layout.astro"
import BlogPost from "../components/BlogPost.astro"
const allPosts = await Astro.glob("../pages/posts/*.mdx")
const allPosts = await getCollection("blog")
const pageTitle = "bitbloxhub's blog"
---
<Layout title={pageTitle}>
<main class="m-auto p-6 max-w-6xl text-left">
<h1>bitbloxhub's blog!</h1>
<ul class="text-base">
{allPosts.map((post) => <BlogPost url={post.url} title={post.frontmatter.title} minutesRead={post.frontmatter.minutesRead} pubDate={post.frontmatter.pubDate} />)}
{allPosts.map((post) => <BlogPost url={`/posts/${post.slug}/`} title={post.data.title} minutesRead={post.data.minutesRead} pubDate={post.data.pubDate} />)}
</ul>
</main>
</Layout>
17 changes: 17 additions & 0 deletions src/pages/posts/[...slug].astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
import { getCollection } from "astro:content"
import MarkdownPostLayout from "../../layouts/MarkdownPostLayout.astro"
// 1. Generate a new path for every collection entry
export async function getStaticPaths() {
const blogEntries = await getCollection("blog")
return blogEntries.map(entry => ({
params: { slug: entry.slug }, props: { entry }
}))
}
// 2. When it's time to render, you can get the entry directly from the prop
const { entry } = Astro.props
const { Content } = await entry.render()
---
<MarkdownPostLayout frontmatter={entry.data}>
<Content />
</MarkdownPostLayout>

0 comments on commit d4ec4bf

Please sign in to comment.