diff --git a/src/content.config.ts b/src/content.config.ts new file mode 100644 index 0000000..92e102a --- /dev/null +++ b/src/content.config.ts @@ -0,0 +1,18 @@ +import { defineCollection, z } from 'astro:content'; +import { glob } from 'astro/loaders'; + +const blog = defineCollection({ + loader: glob({ pattern: '**/[^_]*.{md,mdx}', base: './src/content/blog' }), + schema: z.object({ + title: z.string(), + description: z.string(), + pubDate: z.coerce.date(), + updatedDate: z.coerce.date().optional(), + author: z.string().default('Diego Reséndez'), + tags: z.array(z.string()).optional(), + draft: z.boolean().default(false), + heroImage: z.string().optional(), + }), +}); + +export const collections = { blog }; diff --git a/src/content/blog/.gitkeep b/src/content/blog/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/layouts/Post.astro b/src/layouts/Post.astro new file mode 100644 index 0000000..4109075 --- /dev/null +++ b/src/layouts/Post.astro @@ -0,0 +1,99 @@ +--- +import Base from './Base.astro'; + +interface Props { + title: string; + description: string; + pubDate: Date; + updatedDate?: Date; + author: string; + tags?: string[]; + heroImage?: string; + body: string; +} + +const { title, description, pubDate, updatedDate, author, tags, heroImage, body } = Astro.props; + +const dateFormat = new Intl.DateTimeFormat('en-US', { month: 'long', day: 'numeric', year: 'numeric' }); + +const wordCount = body.trim().split(/\s+/).filter(Boolean).length; +const readingTime = Math.max(1, Math.round(wordCount / 200)); +--- + + +
+
+

← Back to the blog

+

{title}

+ + {tags && tags.length > 0 && ( + + )} +
+ +
+

← Back to the blog

+
+
+ + + diff --git a/src/pages/blog/[...slug].astro b/src/pages/blog/[...slug].astro new file mode 100644 index 0000000..82976fb --- /dev/null +++ b/src/pages/blog/[...slug].astro @@ -0,0 +1,29 @@ +--- +import { getCollection, render } from 'astro:content'; +import Post from '../../layouts/Post.astro'; + +export async function getStaticPaths() { + const posts = await getCollection('blog', ({ data }) => !data.draft); + + return posts.map((post) => ({ + params: { slug: post.id }, + props: { post }, + })); +} + +const { post } = Astro.props; +const { Content } = await render(post); +--- + + + + diff --git a/src/pages/blog/index.astro b/src/pages/blog/index.astro new file mode 100644 index 0000000..ba9d0ca --- /dev/null +++ b/src/pages/blog/index.astro @@ -0,0 +1,99 @@ +--- +import { getCollection } from 'astro:content'; +import Base from '../../layouts/Base.astro'; + +const title = 'Blog — Green Tea.'; +const description = 'Notes on building a zen, opinionated, type-safe HTTP framework.'; + +const posts = (await getCollection('blog', ({ data }) => !data.draft)).sort( + (a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf() +); + +const dateFormat = new Intl.DateTimeFormat('en-US', { month: 'long', day: 'numeric', year: 'numeric' }); +--- + + +
+
+

Blog

+

From the garden

+ + { + posts.length === 0 ? ( +

No posts yet.

+ ) : ( + + ) + } +
+
+ + + diff --git a/src/pages/rss.xml.js b/src/pages/rss.xml.js new file mode 100644 index 0000000..2fd8bc8 --- /dev/null +++ b/src/pages/rss.xml.js @@ -0,0 +1,19 @@ +import rss from '@astrojs/rss'; +import { getCollection } from 'astro:content'; + +export async function GET(context) { + const posts = await getCollection('blog', ({ data }) => !data.draft); + const sorted = posts.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf()); + + return rss({ + title: 'Green Tea — Blog', + description: 'Notes on building a zen, opinionated, type-safe HTTP framework.', + site: context.site, + items: sorted.map((post) => ({ + title: post.data.title, + description: post.data.description, + pubDate: post.data.pubDate, + link: `/blog/${post.id}/`, + })), + }); +}