Skip to content

Commit

Permalink
Update blog posts to support content collections from astro 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
luigieai committed Jan 31, 2023
1 parent 8119688 commit 9f52301
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 37 deletions.
25 changes: 24 additions & 1 deletion .astro/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,30 @@ declare module 'astro:content' {
};

const entryMap: {
"test": {
"blog": {
"post1.md": {
id: "post1.md",
slug: "post1",
body: string,
collection: "blog",
data: InferEntrySchema<"blog">
},
"post2.md": {
id: "post2.md",
slug: "post2",
body: string,
collection: "blog",
data: InferEntrySchema<"blog">
},
"post3.md": {
id: "post3.md",
slug: "post3",
body: string,
collection: "blog",
data: InferEntrySchema<"blog">
},
},
"test": {
"1-test.md": {
id: "1-test.md",
slug: "1",
Expand Down
1 change: 0 additions & 1 deletion src/pages/blog/post1.md → src/content/blog/post1.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
layout: "../../layouts/PostLayout.astro"
title: "Demo Post 1"
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
pubDate: "Sep 10 2022"
Expand Down
1 change: 0 additions & 1 deletion src/pages/blog/post2.md → src/content/blog/post2.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
layout: "../../layouts/PostLayout.astro"
title: "Demo Post 2"
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
pubDate: "Sep 11 2022"
Expand Down
1 change: 0 additions & 1 deletion src/pages/blog/post3.md → src/content/blog/post3.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
layout: "../../layouts/PostLayout.astro"
title: "Demo Post 3"
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
pubDate: "Sep 12 2022"
Expand Down
14 changes: 13 additions & 1 deletion src/content/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { z, defineCollection } from "astro:content";

const blogSchema = z.object({
title: z.string(),
description: z.string(),
pubDate: z.coerce.date(),
updatedDate : z.string().optional(),
heroImage: z.string().optional(),
});
const testSchema = z.object({
nome: z.string(),
lista: z.array(z.string()),
data: z.string().transform(str => new Date(str)),
});
});

export type SchemaTest = z.infer<typeof testSchema>;
export type BlogSchema = z.infer<typeof blogSchema>;

const testCollection = defineCollection( { schema : testSchema } ) ;
const blogCollection = defineCollection( { schema : blogSchema } ) ;

export const collections = {
'blog' : blogCollection,
'test' : testCollection,
}
16 changes: 3 additions & 13 deletions src/layouts/PostLayout.astro
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
---
import BaseHead from "../components/BaseHead.astro";
import { BlogSchema } from "../content/config";
import BaseLayout from "./BaseLayout.astro";
export interface Props {
content: {
title: string;
description: string;
pubDate?: string;
updatedDate?: string;
heroImage?: string;
};
}
export interface Props extends BlogSchema {}
const {
content: { title, description, pubDate, updatedDate, heroImage }
} = Astro.props;
const { title, description, pubDate, updatedDate, heroImage } = Astro.props;
---

<BaseLayout title={title} description={description} image={heroImage}>
Expand Down
9 changes: 4 additions & 5 deletions src/layouts/TestContentLayout.astro
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
---
import BaseLayout from "./BaseLayout.astro";
import { SchemaTest } from "../content/config";
interface Props extends SchemaTest {}
interface Props {
nome: string;
lista: string[];
data: Date;
}
const {nome, lista, data} = Astro.props;
---

<BaseLayout title={nome}>
Expand Down
15 changes: 8 additions & 7 deletions src/pages/blog/[page].astro → src/pages/blog/[...page].astro
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
---
import BaseLayout from "../../layouts/BaseLayout.astro";
import HorizontalCard from "../../components/HorizontalCard.astro";
import { getCollection } from "astro:content";
export async function getStaticPaths({ paginate }) {
const posts = (await Astro.glob("./*.{md,mdx}")).sort(
(a, b) => new Date(b.frontmatter.pubDate).valueOf() - new Date(a.frontmatter.pubDate).valueOf()
);
const posts = (await getCollection('blog'));
return paginate(posts, { pageSize: 10 });
}
const { page } = Astro.props;
---

Expand All @@ -17,10 +18,10 @@ const { page } = Astro.props;
<!--List the array of astronaut info-->
{page.data.map(( post ) => (
<HorizontalCard
title={post.frontmatter.title}
img={post.frontmatter.heroImage}
desc={post.frontmatter.description}
url={post.url}
title={post.data.title}
img={post.data.heroImage}
desc={post.data.description}
url={post.slug}
target="_self"
/>
<div class="divider my-0"></div>
Expand Down
24 changes: 24 additions & 0 deletions src/pages/blog/[slug].astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
import { CollectionEntry, getCollection } from 'astro:content';
import { BlogSchema } from '../../content/config';
import PostLayout from '../../layouts/PostLayout.astro';
export async function getStaticPaths() {
const postEntries = await getCollection('blog');
return postEntries.map(entry => ( {
params: { slug: entry.slug }, props: { entry },
} ));
}
interface Props {
entry: CollectionEntry<"blog">;
}
const { entry } = Astro.props;
const post : BlogSchema = entry.data;
const { Content } = await entry.render();
---

<PostLayout title={post.title} description={post.description} pubDate={post.pubDate} heroImage={post.heroImage} updatedDate={post.updatedDate}>
<Content/>
</PostLayout>
14 changes: 7 additions & 7 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
import BaseLayout from "../layouts/BaseLayout.astro";
import HorizontalCard from "../components/HorizontalCard.astro";
import { getCollection } from "astro:content";
const posts = (await Astro.glob("./blog/*.{md,mdx}")).sort(
(a, b) => new Date(b.frontmatter.pubDate).valueOf() - new Date(a.frontmatter.pubDate).valueOf()
const posts = (await getCollection('blog')).sort(
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf()
);
const last_posts = posts.slice(0, 3);
---

Expand Down Expand Up @@ -36,10 +36,10 @@ const last_posts = posts.slice(0, 3);
{
last_posts.map((post) => (
<HorizontalCard
title={post.frontmatter.title}
img={post.frontmatter.heroImage}
desc={post.frontmatter.description}
url={post.url}
title={post.data.title}
img={post.data.heroImage}
desc={post.data.description}
url={post.slug}
target="_self"
/>
<div class="divider my-0"></div>
Expand Down

0 comments on commit 9f52301

Please sign in to comment.