diff --git a/src/pages/posts/post-1.md b/src/blog/post-1.md similarity index 95% rename from src/pages/posts/post-1.md rename to src/blog/post-1.md index 6d0250e..0ab866c 100644 --- a/src/pages/posts/post-1.md +++ b/src/blog/post-1.md @@ -1,5 +1,4 @@ --- -layout: "../../layouts/BlogPostLayout.astro" title: My first blog post description: This is my first blog post pubDate: 2024-12-30 diff --git a/src/pages/posts/post-2.md b/src/blog/post-2.md similarity index 91% rename from src/pages/posts/post-2.md rename to src/blog/post-2.md index 6ee6791..8309c9e 100644 --- a/src/pages/posts/post-2.md +++ b/src/blog/post-2.md @@ -1,5 +1,4 @@ --- -layout: "../../layouts/BlogPostLayout.astro" title: My Second Blog Post author: Astro Learner description: "After learning some Astro, I couldn't stop!" diff --git a/src/pages/posts/post-3.md b/src/blog/post-3.md similarity index 91% rename from src/pages/posts/post-3.md rename to src/blog/post-3.md index cff659e..1e7eddf 100644 --- a/src/pages/posts/post-3.md +++ b/src/blog/post-3.md @@ -1,5 +1,4 @@ --- -layout: "../../layouts/BlogPostLayout.astro" title: My Third Blog Post author: Astro Learner description: "I had some challenges, but asking in the community really helped!" diff --git a/src/pages/posts/post-4.md b/src/blog/post-4.md similarity index 90% rename from src/pages/posts/post-4.md rename to src/blog/post-4.md index 3ef1ef0..4c4d8c5 100644 --- a/src/pages/posts/post-4.md +++ b/src/blog/post-4.md @@ -1,5 +1,4 @@ --- -layout: "../../layouts/BlogPostLayout.astro" title: My Fourth Blog Post author: Astro Learner description: "This post will show up on its own!" diff --git a/src/content.config.ts b/src/content.config.ts new file mode 100644 index 0000000..4f8db2b --- /dev/null +++ b/src/content.config.ts @@ -0,0 +1,19 @@ +import { glob } from "astro/loaders"; +import { defineCollection, z } from "astro:content"; + +const blog = defineCollection({ + loader: glob({ pattern: "**/[^_]*.md", base: "./src/blog" }), + schema: z.object({ + title: z.string(), + description: z.string(), + pubDate: z.date(), + author: z.string(), + tags: z.array(z.string()), + image: z.object({ + url: z.string(), + alt: z.string(), + }), + }), +}); + +export const collections = { blog }; diff --git a/src/layouts/BlogPostLayout.astro b/src/layouts/BlogPostLayout.astro index 920b134..da2f242 100644 --- a/src/layouts/BlogPostLayout.astro +++ b/src/layouts/BlogPostLayout.astro @@ -4,7 +4,7 @@ const { frontmatter } = Astro.props; --- -

Published on: {frontmatter.pubDate.toString().slice(0, 10)}

+

Published on: {frontmatter.pubDate.toLocaleDateString()}

{frontmatter.description}

Written by: {frontmatter.author}

{frontmatter.image.alt} diff --git a/src/pages/blog.astro b/src/pages/blog.astro index 63fd67f..1dfa62e 100644 --- a/src/pages/blog.astro +++ b/src/pages/blog.astro @@ -2,12 +2,11 @@ import "../styles/global.css"; import BaseLayout from "../layouts/BaseLayout.astro"; import BLogPost from "../components/BLogPost.astro"; +import { getCollection } from "astro:content"; const pageTitle = "Blog posts"; -const allPosts = Object.values( - import.meta.glob("./posts/*.md", { eager: true }), -); +const allPosts = await getCollection("blog"); --- @@ -16,7 +15,7 @@ const allPosts = Object.values( diff --git a/src/pages/blog/[...slug].astro b/src/pages/blog/[...slug].astro new file mode 100644 index 0000000..18348fb --- /dev/null +++ b/src/pages/blog/[...slug].astro @@ -0,0 +1,19 @@ +--- +import { getCollection, render } from "astro:content"; +import BlogPostLayout from "../../layouts/BlogPostLayout.astro"; + +export async function getStaticPaths() { + const posts = await getCollection("blog"); + return posts.map((post) => ({ + params: { slug: post.id }, + props: { post }, + })); +} + +const { post } = Astro.props; +const { Content } = await render(post); +--- + + + + diff --git a/src/pages/tags/[tag].astro b/src/pages/tags/[tag].astro index 5a23c0e..6027d34 100644 --- a/src/pages/tags/[tag].astro +++ b/src/pages/tags/[tag].astro @@ -1,18 +1,13 @@ --- +import { getCollection } from "astro:content"; import BLogPost from "../../components/BLogPost.astro"; import BaseLayout from "../../layouts/BaseLayout.astro"; export async function getStaticPaths() { - const posts = Object.values( - import.meta.glob("../posts/*.md", { eager: true }), - ); - const tags = [ - ...new Set(posts.flatMap((post: any) => post.frontmatter.tags)), - ]; - return tags.map((tag: any) => { - const filteredPosts = posts.filter((post: any) => - post.frontmatter.tags?.includes(tag), - ); + const posts = await getCollection("blog"); + const tags = [...new Set(posts.flatMap((post) => post.data.tags))]; + return tags.map((tag) => { + const filteredPosts = posts.filter((post) => post.data.tags?.includes(tag)); return { params: { tag }, props: { posts: filteredPosts } }; }); } @@ -25,8 +20,8 @@ const { posts } = Astro.props;

Posts tagged with {tag}

diff --git a/src/pages/tags/index.astro b/src/pages/tags/index.astro index a58c561..35aead3 100644 --- a/src/pages/tags/index.astro +++ b/src/pages/tags/index.astro @@ -1,13 +1,9 @@ --- +import { getCollection } from "astro:content"; import BaseLayout from "../../layouts/BaseLayout.astro"; -const tags = [ - ...new Set( - Object.values(import.meta.glob("../posts/*.md", { eager: true })).flatMap( - (post: any) => post.frontmatter.tags, - ), - ), -]; +const posts = await getCollection("blog"); +const tags = [...new Set(posts.flatMap((post) => post.data.tags))]; ---