Skip to content

Commit b88b196

Browse files
authored
Merge pull request #9 from jhilker98/bug_contentError
This merge should fix a build error due to the updated content collections API in Astro.
2 parents 869ce17 + 649c139 commit b88b196

File tree

14 files changed

+126
-148
lines changed

14 files changed

+126
-148
lines changed

package-lock.json

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"reading-time": "^1.5.0",
6363
"remark-toc": "^9.0.0",
6464
"sanitize-html": "^2.11.0",
65+
"tailwind-scrollbar": "^4.0.0",
6566
"unplugin-icons": "^0.18.1"
6667
}
6768
}

src/components/blog/PostInfo.astro

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
---
2-
import type { BlogPost } from "~/content/config.ts";
2+
import type { BlogPost } from "~/content.config";
33
import { formatPostDate, calcReadingTime, slugifyPostDate } from "~/lib/utils";
4-
4+
import { marked } from "marked";
5+
import readingTime from "reading-time";
6+
import { Icon } from "astro-icon/components";
57
interface Props {
68
post: BlogPost;
79
wordCount: number;
810
}
911
const { post, wordCount } = Astro.props as Props;
1012
11-
import { marked } from "marked";
12-
13-
const body = `${post.body
14-
.split(" ")
15-
.slice(0, wordCount + 1)
16-
.join(" ")}...`;
17-
const postBody = marked.parse(body);
13+
const body = post?.body?.split(" ").slice(0, wordCount).join(" ") || "";
14+
const content = marked(body);
1815
---
1916

2017
<article class="py-3.5 grid md:grid-cols-[18rem_auto]">
@@ -24,13 +21,20 @@ const postBody = marked.parse(body);
2421
</span>
2522
<time
2623
datetime={post.data.pubDate.toISOString()}
27-
class="font-semibold text-sm text-gray-700 dark:text-gray-400"
24+
class="font-semibold text-sm text-branding-gray-700 dark:text-gray-400"
2825
>{formatPostDate(post.data.pubDate)}</time
2926
>
30-
<span
31-
class="block mt-1 text-branding-gray-600 dark:text-branding-gray-400 text-sm font-semibold">
32-
{calcReadingTime(post.body)}
33-
</span>
27+
{
28+
post.body && (
29+
<div class="block mt-1 text-branding-gray-600 dark:text-branding-gray-400 text-sm font-semibold ml-1">
30+
<div class="inline-flex items-center justify-between">
31+
<Icon name="mdi:clock" class="inline" />
32+
<span class="ml-1">{readingTime(post.body).text}</span>
33+
</div>
34+
</div>
35+
)
36+
}
37+
3438
{
3539
post.data.series && (
3640
<div class="text-gray-700 dark:text-gray-400 text-sm mt-1 break-words">
@@ -46,10 +50,10 @@ const postBody = marked.parse(body);
4650
</h2>
4751
<div
4852
class="prose prose-neutral dark:prose-invert max-w-[80ch] prose-branding-blue"
49-
set:html={postBody}
53+
set:html={content}
5054
/>
5155
<a
52-
href={`/blog/${slugifyPostDate(post.data.pubDate)}/${post.slug}`}
56+
href={`/blog/${slugifyPostDate(post.data.pubDate)}/${post.id}`}
5357
class="block rounded-sm text-sm bg-branding-navy-600 w-fit text-white h-fit px-2 py-1 mt-1 dark:hover:bg-branding-navy-500 duration-100 transition-colors hover:bg-branding-navy-700"
5458
>Read More</a
5559
>

src/content.config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { z, defineCollection, type CollectionEntry } from "astro:content";
2+
import { glob } from "astro/loaders";
23

3-
const blogSchema = defineCollection({
4-
type: "content",
4+
const blog = defineCollection({
5+
loader: glob({ pattern: "**/*.{md,mdx}", base: "src/content/blog" }),
56
schema: ({ image }) =>
67
z.object({
78
title: z.string({
@@ -48,8 +49,8 @@ export type BlogPost = CollectionEntry<"blog">;
4849

4950
//export type BlogPost = CollectionEntry<"blog">;
5051

51-
const projectSchema = defineCollection({
52-
type: "content",
52+
const projects = defineCollection({
53+
loader: glob({ pattern: "**/*.{md,mdx}", base: "src/content/projects" }),
5354
schema: z.object({
5455
title: z.string({ required_error: "A title is required for a project." }),
5556
draft: z
@@ -106,6 +107,5 @@ const projectSchema = defineCollection({
106107
export type Project = CollectionEntry<"projects">;
107108

108109
export const collections = {
109-
blog: blogSchema,
110-
projects: projectSchema,
110+
blog, projects
111111
};

src/content/config.ts

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/layouts/BaseLayout.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const { title, description } = Astro.props;
4949
<Header />
5050
<FixedSidebar />
5151
<main
52-
class="grid-area-main overflow-y-scroll scrollbar-thin scroll-smooth motion-reduce:scroll-auto">
52+
class="grid-area-main overflow-y-scroll scrollbar-thin scroll-smooth motion-reduce:scroll-auto scrollbar-h-32">
5353
<slot />
5454
</main>
5555
</div>

src/lib/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ export function toTitleCase(str: string): string {
5656
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
5757
});
5858
}
59+

src/pages/blog/[...page].astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import PostInfo from "~/components/blog/PostInfo.astro";
55
import { getCollection, type CollectionEntry } from "astro:content";
66
import Pagination from "~/components/Pagination.astro";
77
import { RECENT_POST_COUNT } from "~/consts";
8-
import type { BlogPost } from "~/content/config.ts";
8+
import type { BlogPost } from "~/content.config";
99
export const getStaticPaths = (async ({ paginate }) => {
1010
const posts: CollectionEntry<"blog">[] = await getCollection(
1111
"blog",
@@ -22,7 +22,7 @@ const { page } = Astro.props;
2222

2323
<BaseLayout title="Blog" description="All the blog posts on my site.">
2424
<section
25-
class="min-h-full grid grid-rows-[1fr_auto] container mx-auto px-5 overflow-hidden">
25+
class="min-h-full grid grid-rows-(1fr_auto) container mx-auto px-5 overflow-hidden">
2626
<div class="divide-y-2 divide-gray-500">
2727
{
2828
page.data.map((post: BlogPost) => (

src/pages/blog/[...slug].astro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import type {
44
InferGetStaticPropsType,
55
GetStaticPaths,
66
} from "astro";
7-
import { getCollection } from "astro:content";
7+
import { getCollection, render } from "astro:content";
88
import BlogPostLayout from "~/layouts/BlogPostLayout.astro";
99
import { slugifyPostDate } from "~/lib/utils";
1010
1111
export const getStaticPaths = (async () => {
1212
const posts = await getCollection("blog");
1313
return posts.map((post) => {
1414
return {
15-
params: { slug: `${slugifyPostDate(post.data.pubDate)}/${post.slug}` },
15+
params: { slug: `${slugifyPostDate(post.data.pubDate)}/${post.id}` },
1616
props: { post },
1717
};
1818
});
@@ -24,7 +24,7 @@ type Props = InferGetStaticPropsType<typeof getStaticPaths>;
2424
// eslint-disable-next-line
2525
const { slug } = Astro.params as Params;
2626
const { post } = Astro.props;
27-
const { Content } = await post.render();
27+
const { Content } = await render(post);
2828
---
2929

3030
<BlogPostLayout frontmatter={post.data}>
Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,52 @@
11
---
2+
import type { GetStaticPaths } from "astro";
3+
4+
import { getCollection } from "astro:content";
5+
import { slugifyUrl, toTitleCase } from "~/lib/utils";
6+
import type { BlogPost } from "~/content.config";
7+
28
import BaseLayout from "~/layouts/BaseLayout.astro";
39
import PostInfo from "~/components/blog/PostInfo.astro";
410
import Pagination from "~/components/Pagination.astro";
511
6-
import { getCollection } from "astro:content";
7-
import { slugifyUrl, toTitleCase } from "~/lib/utils";
12+
export async function getStaticPaths({ paginate }) {
13+
const allPosts = await getCollection("blog");
14+
const uniqueCats = [
15+
...new Set(allPosts.map((post) => post.data?.categories).flat()),
16+
].filter((category) => category !== undefined);
17+
18+
return uniqueCats.flatMap((category) => {
19+
const filteredPosts = allPosts.filter((post) =>
20+
post.data.categories?.includes(category!),
21+
);
22+
return paginate(filteredPosts, {
23+
params: { category: slugifyUrl(category) },
24+
props: { category, slug: slugifyUrl(category) },
25+
pageSize: 4,
26+
});
27+
});
28+
}
29+
30+
const { category } = Astro.params;
31+
const { page } = Astro.props;
832
---
33+
34+
<BaseLayout
35+
title=`Category: ${toTitleCase(category)}`
36+
description={`All blog posts categorized with ${category}`}>
37+
<article class="p-2 prose prose-neutral dark:prose-invert prose-h1:mb-2">
38+
<h1>Category: {toTitleCase(category)}</h1>
39+
</article>
40+
<section class="min-h-screen text-gray-600">
41+
<div class="container mx-auto px-5 h-screen">
42+
<div class="divide-y-2 divide-gray-500">
43+
{
44+
page.data.map((post: BlogPost) => (
45+
<PostInfo post={post} wordCount={35} />
46+
))
47+
}
48+
</div>
49+
<Pagination prevUrl={page.url.prev} nextUrl={page.url.next} />
50+
</div>
51+
</section>
52+
</BaseLayout>

0 commit comments

Comments
 (0)