feat: allow blog posts to be organized by subdirectories

Blog posts can now be structured into subdirectories, which determine their slugs.

Example:
- `/src/data/blog/2025/testing.md` → `/posts/2025/testing`

Directories prefixed with `_` will be excluded from the slug.

Example:
- `/src/data/blog/_examples/tailwind-typography.md` → `/posts/tailwind-typography`

Closes #470, #366
This commit is contained in:
satnaing
2025-03-16 20:09:03 +07:00
committed by Sat Naing
parent 67b9536cce
commit feaf5e122a
19 changed files with 64 additions and 39 deletions
+2 -2
View File
@@ -69,8 +69,8 @@ const months = [
new Date(a.data.pubDatetime).getTime() / 1000
)
)
.map(({ data, id }) => (
<Card href={`/posts/${id}`} frontmatter={data} />
.map(data => (
<Card {...data} />
))}
</ul>
</div>
+4 -10
View File
@@ -76,8 +76,8 @@ const recentPosts = sortedPosts.filter(({ data }) => !data.featured);
<section id="featured" class="pt-12 pb-6">
<h2 class="text-2xl font-semibold tracking-wide">Featured</h2>
<ul>
{featuredPosts.map(({ data, id }) => (
<Card href={`/posts/${id}/`} frontmatter={data} variant="h3" />
{featuredPosts.map(data => (
<Card variant="h3" {...data} />
))}
</ul>
</section>
@@ -92,14 +92,8 @@ const recentPosts = sortedPosts.filter(({ data }) => !data.featured);
<h2 class="text-2xl font-semibold tracking-wide">Recent Posts</h2>
<ul>
{recentPosts.map(
({ data, id }, index) =>
index < SITE.postPerIndex && (
<Card
href={`/posts/${id}/`}
frontmatter={data}
variant="h3"
/>
)
(data, index) =>
index < SITE.postPerIndex && <Card variant="h3" {...data} />
)}
</ul>
</section>
+1 -5
View File
@@ -22,11 +22,7 @@ const { page } = Astro.props;
<Header />
<Main pageTitle="Posts" pageDesc="All the articles I've posted.">
<ul>
{
page.data.map(({ data, id }) => (
<Card href={`/posts/${id}`} frontmatter={data} />
))
}
{page.data.map(data => <Card {...data} />)}
</ul>
</Main>
@@ -2,6 +2,7 @@
import { type CollectionEntry, getCollection } from "astro:content";
import PostDetails from "@/layouts/PostDetails.astro";
import getSortedPosts from "@/utils/getSortedPosts";
import { getPath } from "@/utils/getPath";
export interface Props {
post: CollectionEntry<"blog">;
@@ -9,9 +10,8 @@ export interface Props {
export async function getStaticPaths() {
const posts = await getCollection("blog", ({ data }) => !data.draft);
const postResult = posts.map(post => ({
params: { slug: post.id },
params: { slug: getPath(post.id, post.filePath, false) },
props: { post },
}));
@@ -1,7 +1,7 @@
import type { APIRoute } from "astro";
import { getCollection, type CollectionEntry } from "astro:content";
import { getPath } from "@/utils/getPath";
import { generateOgImageForPost } from "@/utils/generateOgImages";
import { slugifyStr } from "@/utils/slugify";
import { SITE } from "@/config";
export async function getStaticPaths() {
@@ -14,7 +14,7 @@ export async function getStaticPaths() {
);
return posts.map(post => ({
params: { slug: slugifyStr(post.data.title) },
params: { slug: getPath(post.id, post.filePath, false) },
props: post,
}));
}
+3 -2
View File
@@ -1,5 +1,6 @@
import rss from "@astrojs/rss";
import { getCollection } from "astro:content";
import { getPath } from "@/utils/getPath";
import getSortedPosts from "@/utils/getSortedPosts";
import { SITE } from "@/config";
@@ -10,8 +11,8 @@ export async function GET() {
title: SITE.title,
description: SITE.desc,
site: SITE.website,
items: sortedPosts.map(({ data, id }) => ({
link: `posts/${id}/`,
items: sortedPosts.map(({ data, id, filePath }) => ({
link: getPath(id, filePath),
title: data.title,
description: data.description,
pubDate: new Date(data.modDatetime ?? data.pubDatetime),
+1 -5
View File
@@ -40,11 +40,7 @@ const { page, tagName } = Astro.props;
>
<h1 slot="title" transition:name={tag}>{`Tag:${tag}`}</h1>
<ul>
{
page.data.map(({ data, id }) => (
<Card href={`/posts/${id}`} frontmatter={data} />
))
}
{page.data.map(data => <Card {...data} />)}
</ul>
</Main>