diff --git a/src/components/BlogPosts.tsx b/src/components/BlogPosts.tsx index b474437..5ed5526 100644 --- a/src/components/BlogPosts.tsx +++ b/src/components/BlogPosts.tsx @@ -1,16 +1,16 @@ import type { Component } from "solid-js"; -import type { Frontmatter } from "~/lib/blog"; +import { type CollectionEntry } from "astro:content"; export interface Post { - url?: string; - frontmatter: Frontmatter; + url: string; + entry: CollectionEntry<'blog' | 'garden'>; } export interface Props { posts: Post[]; } -const format = (date: string) => +const format = (date: string | Date) => new Date(date).toLocaleDateString("en-us", { year: "numeric", month: "short", @@ -20,13 +20,13 @@ const format = (date: string) => const BlogPosts: Component = (props) => { return ( - {props.posts.map(({ frontmatter: fm, url }) => ( + {props.posts.map(({ entry: { data }, url }) => ( ))} diff --git a/src/components/BookmarkCard.astro b/src/components/BookmarkCard.astro index 8601d2c..e51997e 100644 --- a/src/components/BookmarkCard.astro +++ b/src/components/BookmarkCard.astro @@ -49,7 +49,7 @@ const { href, name, description, tags } = Astro.props; { tags.map((tag) => ( - {tag} + {tag} )) } diff --git a/src/content/config.ts b/src/content/config.ts index 45be79e..05e1762 100644 --- a/src/content/config.ts +++ b/src/content/config.ts @@ -5,8 +5,11 @@ const blog = defineCollection({ schema: z.object({ title: z.string(), description: z.string(), - tags: z.array(z.string()), - pubDate: z.date().optional(), + tags: z.array(z.enum([ + "journal", + "goals", + ])), + pubDate: z.date(), blog: z.boolean().optional(), draft: z.boolean().optional(), }), @@ -17,8 +20,19 @@ const garden = defineCollection({ schema: z.object({ title: z.string(), description: z.string().optional(), - tags: z.array(z.string()), - pubDate: z.date().optional(), + tags: z.array(z.enum([ + "computer-science", + "hobbies", + "homelab", + "ideas", + "kubernetes", + "opsec", + "philosophy", + "programming", + "security", + ])), + pubDate: z.date(), + modDate: z.date().optional(), draft: z.boolean().optional(), }), }); diff --git a/src/content/garden/Building Habits.md b/src/content/garden/Building Habits.md index 1daad14..ca417aa 100644 --- a/src/content/garden/Building Habits.md +++ b/src/content/garden/Building Habits.md @@ -3,6 +3,7 @@ title: Building Habits description: Ideas about habit building. pubDate: 2023-01-09T18:48:15.000Z tags: [] +slug: Building-Habits --- Habits are a common topic among self help gurus and its understandable, we all diff --git a/src/layouts/BlogPost.astro b/src/layouts/BlogPost.astro index 83b482b..ebf88c3 100644 --- a/src/layouts/BlogPost.astro +++ b/src/layouts/BlogPost.astro @@ -8,12 +8,13 @@ export interface Props { content: { title: string; description: string; - pubDate: Date; tags?: string[]; + pubDate: Date; + modDate?: Date | undefined; }; } const { - content: { title, description, tags, pubDate }, + content: { title, description, tags, pubDate, modDate }, } = Astro.props; const date = new Date(pubDate); --- @@ -38,7 +39,11 @@ const date = new Date(pubDate);
diff --git a/src/layouts/PostList.astro b/src/layouts/PostList.astro index 929c27a..be22c5e 100644 --- a/src/layouts/PostList.astro +++ b/src/layouts/PostList.astro @@ -3,10 +3,10 @@ import path from "~/lib/path"; import Base from "~/layouts/Base.astro"; import Nav from "~/components/Nav.astro"; import BlogPosts from "~/components/BlogPosts"; -import GithubSlugger from "github-slugger"; -import { getPosts, slug } from "~/lib/blog"; import { SITE_TITLE } from "~/config"; +import { getCollection } from "astro:content"; + export interface Props { blog: boolean; subTitle: string; @@ -15,21 +15,20 @@ export interface Props { const { subTitle, blog, description } = Astro.props; -const allPosts = await getPosts({ - blog: blog, - drafts: import.meta.env.DEV, -}); -const slugger = new GithubSlugger(); - +const allPosts = await getCollection(blog ? "blog" : "garden"); // TODO Build a search index for content and tags const posts = allPosts.map((p) => { - const s = slug(p, true, slugger); - if (!p.frontmatter.slug) p.frontmatter.slug = s; return { - url: p.url || path.join(Astro.url.pathname, s), - frontmatter: p.frontmatter, + url: path.join(Astro.url.pathname, p.slug), + entry: p, }; +}).filter((post) => !post.entry.data.draft); + +posts.sort((a, b) => { + if (!a.entry.data.pubDate || !b.entry.data.pubDate) return 0; + return b.entry.data.pubDate.getTime() - a.entry.data.pubDate.getTime(); }); + const title = `${SITE_TITLE} | ${subTitle}`; const props = { title, description } --- diff --git a/src/lib/dates.ts b/src/lib/dates.ts index c26d88d..4df5a36 100644 --- a/src/lib/dates.ts +++ b/src/lib/dates.ts @@ -6,5 +6,5 @@ export const humanDate = (d: Date) => hour: "2-digit", minute: "2-digit", second: "2-digit", - timeZoneName: "longGeneric", + timeZoneName: "shortGeneric", }); diff --git a/src/pages/blog/[slug].astro b/src/pages/blog/[slug].astro index 133e73f..1a5b3b3 100644 --- a/src/pages/blog/[slug].astro +++ b/src/pages/blog/[slug].astro @@ -1,20 +1,19 @@ --- export const prerender = true; -import type { MarkdownInstance, GetStaticPathsResult } from "astro"; -import { type Frontmatter, getPosts, slug } from "~/lib/blog"; -import GithubSlugger from "github-slugger"; +import type { GetStaticPathsResult } from "astro"; +import { type CollectionEntry, getCollection } from "astro:content"; +import BlogPost from "~/layouts/BlogPost.astro"; export interface Props { - post: MarkdownInstance; + post: CollectionEntry<"blog">; } export const getStaticPaths = async (): Promise => { - const slugger = new GithubSlugger(); - const posts = await getPosts({ blog: true }); + const posts = (await getCollection("blog")).filter((p) => !p.data.draft); return posts.map((post) => { return { params: { - slug: slug(post, true, slugger), + slug: post.slug, }, props: { post }, }; @@ -22,8 +21,21 @@ export const getStaticPaths = async (): Promise => { }; const { - post: { Content }, + post: { + data: { title, description, tags, pubDate }, + render, + }, } = Astro.props; +const rendered = await render(); +const Content = rendered.Content; +const info = { + title, + description, + tags, + pubDate: new Date(pubDate || ""), +}; --- - + + + diff --git a/src/pages/content/[date]/[slug].astro b/src/pages/content/[date]/[slug].astro index 8fac92b..dc03fe1 100644 --- a/src/pages/content/[date]/[slug].astro +++ b/src/pages/content/[date]/[slug].astro @@ -1,31 +1,63 @@ --- export const prerender = true; -import type { MarkdownInstance, GetStaticPathsResult } from "astro"; -import { type Frontmatter, getPosts, slug } from "~/lib/blog"; + +import BlogPost from "~/layouts/BlogPost.astro"; +import type { GetStaticPathsResult } from "astro"; import GithubSlugger from "github-slugger"; +import { getCollection } from "astro:content"; +import type { CollectionEntry } from "astro:content"; export interface Props { - post: MarkdownInstance; + post: CollectionEntry<"blog" | "garden">; } export const getStaticPaths = async (): Promise => { const slugger = new GithubSlugger(); - const posts = await getPosts({ drafts: import.meta.env.DEV }); - return posts.map((post) => { - let date: Date = new Date(post.frontmatter.pubDate); - return { - params: { - slug: slug(post, true, slugger), - date: date.toLocaleDateString().replaceAll("/", "-"), - }, - props: { post }, - }; - }); + const posts = (await getCollection("blog")).filter((p) => !p.data.draft); + const garden = (await getCollection("garden")).filter((p) => !p.data.draft); + let paths = [ + ...posts.map((post) => { + let date = new Date(post.data.pubDate || ""); + const slug = slugger.slug(post.id.replace(".md", ""), false); + return { + params: { + slug, + date: date.toLocaleDateString().replaceAll("/", "-"), + }, + props: { post }, + }; + }), + ...garden.map((post) => { + let date = new Date(post.data.pubDate || ""); + const slug = slugger.slug(post.id.replace(".md", ""), false); + return { + params: { + slug, + date: date.toLocaleDateString().replaceAll("/", "-"), + }, + props: { post }, + }; + }), + ]; + return paths; }; const { - post: { Content }, + post: { + render, + data: { title, description, tags, pubDate }, + }, } = Astro.props; + +const { Content } = await render(); +const info = { + title, + tags, + pubDate, + description: description || "", +}; --- - + + + diff --git a/src/pages/garden/[slug].astro b/src/pages/garden/[slug].astro index a9d3589..f1db556 100644 --- a/src/pages/garden/[slug].astro +++ b/src/pages/garden/[slug].astro @@ -1,22 +1,20 @@ --- export const prerender = true; -import type { MarkdownInstance, GetStaticPathsResult } from "astro"; -import { type Frontmatter, getPosts, slug } from "~/lib/blog"; -import GithubSlugger from "github-slugger"; +import type { GetStaticPathsResult } from "astro"; import BlogPost from "~/layouts/BlogPost.astro"; +import { type CollectionEntry, getCollection } from "astro:content"; export interface Props { - post: MarkdownInstance; + post: CollectionEntry<"garden">; } export const getStaticPaths = async (): Promise => { - const slugger = new GithubSlugger(); - const posts = await getPosts({ blog: false }); + const posts = (await getCollection("garden")).filter((p) => !p.data.draft); return posts.map((post) => { return { params: { - slug: slug(post, true, slugger), + slug: post.slug, }, props: { post }, }; @@ -25,15 +23,19 @@ export const getStaticPaths = async (): Promise => { const { post: { - Content, - frontmatter: { layout, title, description, pubDate, tags }, + data: { title, description, tags, pubDate, modDate }, + render, }, } = Astro.props; -const content = { +const layout = false; +const rendered = await render(); +const Content = rendered.Content; +const info = { title, - description, tags, - pubDate: new Date(pubDate), + description: description || "", + pubDate: pubDate, + modDate: modDate, }; --- @@ -44,7 +46,7 @@ const content = { layout ? ( ) : ( - + ) diff --git a/src/pages/rss.xml.ts b/src/pages/rss.xml.ts index 2dbf198..d37777c 100644 --- a/src/pages/rss.xml.ts +++ b/src/pages/rss.xml.ts @@ -1,22 +1,29 @@ import path from "~/lib/path"; import rss from "@astrojs/rss"; +import { getCollection, type ContentEntryMap } from "astro:content"; import { SITE_TITLE, SITE_DESCRIPTION, gardenBasePath } from "../config"; -import { getPosts, slug } from "~/lib/blog"; -import GithubSlugger from "github-slugger"; +import { collections } from "~/content/config"; export const GET = async () => { - const posts = await getPosts({ drafts: import.meta.env.DEV }); - const slugger = new GithubSlugger(); + let posts = []; + for (const k in collections) { + posts.push(...(await getCollection(k as keyof ContentEntryMap))); + } + const items = posts.filter((p) => !p.data.draft).map((p) => { + return { + link: p.collection === "garden" + ? path.join(gardenBasePath, p.slug) + : path.join("blog", p.slug), + title: p.data.title, + pubDate: p.data.pubDate, + description: p.data.description, + categories: p.data.tags, + }; + }); return rss({ title: SITE_TITLE, description: SITE_DESCRIPTION, site: import.meta.env.SITE, - items: posts.map((p) => ({ - link: p.url || path.join(gardenBasePath, slug(p, true, slugger)), - title: p.frontmatter.title, - pubDate: new Date(p.frontmatter.pubDate), - description: p.frontmatter.description, - content: p.compiledContent(), - })), + items, }); }; diff --git a/src/pages/stars.astro b/src/pages/stars.astro index 3cfb191..ee5f886 100644 --- a/src/pages/stars.astro +++ b/src/pages/stars.astro @@ -1,6 +1,5 @@ --- import StarField from "~/components/backgrounds/StarField.astro"; -console.log("stars page load"); let counter = 0; if (Astro.cookies.has("counter")) { const cookie = Astro.cookies.get("counter");
- + - {fm.title} + {data.title}