Commit 0681914

HPCesia <me@hpcesia.com>
2026-08-08 09:29:31
Basic blog post content loader
1 parent 204a7b5
Changed files (5)
src/contents/test.typ → src/contents/posts/test.typ
@@ -1,3 +1,11 @@
+#metadata(
+  (
+    title: "Test page",
+    author: "HPCesia",
+    desc: "Test Page",
+  )
+)<frontmatter>
+
 This _Is_ *A* _*TEST*_ Page. Current Typst version: #sys.version
 
 == Equations
src/pages/posts/[slug].astro
@@ -0,0 +1,29 @@
+---
+import { getCollection, render } from "astro:content";
+import type { GetStaticPaths, InferGetStaticPropsType } from "astro";
+
+import BaseLayout from "@/layouts/BaseLayout.astro";
+
+type Props = InferGetStaticPropsType<typeof getStaticPaths>;
+
+export const getStaticPaths = (async () => {
+  const entries = await getCollection("posts");
+  return entries.map((entry) => ({
+    params: {
+      slug: entry.id,
+    },
+    props: { entry },
+  }));
+}) satisfies GetStaticPaths;
+
+const { entry } = Astro.props;
+const data = entry.data;
+const { Content } = await render(entry);
+---
+<BaseLayout
+  title={data.title}
+  author={data.author}
+  description={data.desc}
+>
+  <Content />
+</BaseLayout>
src/pages/posts/index.astro
@@ -0,0 +1,14 @@
+---
+import { getCollection } from "astro:content";
+
+import BaseLayout from "@/layouts/BaseLayout.astro";
+
+const posts = await getCollection("posts");
+---
+<BaseLayout title="Posts">
+
+    <h1>Posts</h1>
+    {
+        posts.map(post => (<li><a href={"/posts/" + post.id}>{post.data.title}</a></li>))
+    }
+</BaseLayout>
src/pages/index.astro
@@ -1,8 +1,7 @@
 ---
 import BaseLayout from "@/layouts/BaseLayout.astro";
-import TypstTestPage from "@/contents/test.typ?html";
 ---
 <BaseLayout title="Astro">
   <h1>Astro</h1>
-  <TypstTestPage />
+  <a href="/posts">Posts</a>
 </BaseLayout>
src/content.config.ts
@@ -0,0 +1,19 @@
+import { defineCollection } from "astro:content";
+import { glob } from "astro/loaders";
+import { z } from "astro/zod";
+
+const typCollection = defineCollection({
+  loader: glob({
+    pattern: "**/[^_]*.{typ,typst}",
+    base: "./src/contents/posts",
+  }),
+  schema: z.object({
+    title: z.string(),
+    author: z.string().optional(),
+    desc: z.any().optional(),
+  }),
+});
+
+export const collections = {
+  posts: typCollection,
+};