diff --git a/src/components/blog/Headline.astro b/src/components/blog/Headline.astro index 9d42645..5d3ccc6 100644 --- a/src/components/blog/Headline.astro +++ b/src/components/blog/Headline.astro @@ -3,9 +3,7 @@ const { title = await Astro.slots.render('default'), subtitle = await Astro.slot ---
-

- {title} -

+

{ subtitle && (
diff --git a/src/components/blog/ListItem.astro b/src/components/blog/ListItem.astro index 7acf199..9401304 100644 --- a/src/components/blog/ListItem.astro +++ b/src/components/blog/ListItem.astro @@ -43,8 +43,19 @@ const link = !BLOG?.post?.disabled ? getPermalink(post.permalink, 'post') : '';
+ + { + post.category && ( + <> + {' '} · + + {post.category.replaceAll('-', ' ')} + + + ) + }

diff --git a/src/components/blog/SinglePost.astro b/src/components/blog/SinglePost.astro index d53fb6f..577c21f 100644 --- a/src/components/blog/SinglePost.astro +++ b/src/components/blog/SinglePost.astro @@ -5,6 +5,7 @@ import { Picture } from '@astrojs/image/components'; import PostTags from '~/components/blog/Tags.astro'; import SocialShare from '~/components/common/SocialShare.astro'; +import { getPermalink } from '~/utils/permalinks'; import { getFormattedDate } from '~/utils/utils'; import type { Post } from '~/types'; @@ -22,8 +23,19 @@ const { post, url } = Astro.props;

- + + { + post.category && ( + <> + {' '} + · + + {post.category.replaceAll('-', ' ')} + + + ) + }

- Category {category} + {category.replaceAll('-', ' ')}
diff --git a/src/utils/blog.ts b/src/utils/blog.ts index cfd5b3d..2eac082 100644 --- a/src/utils/blog.ts +++ b/src/utils/blog.ts @@ -1,7 +1,7 @@ import { getCollection } from 'astro:content'; import type { CollectionEntry } from 'astro:content'; import type { Post } from '~/types'; -import { cleanSlug, POST_PERMALINK_PATTERN } from './permalinks'; +import { cleanSlug, trimSlash, POST_PERMALINK_PATTERN } from './permalinks'; const generatePermalink = async ({ id, slug, publishDate, category }) => { const year = String(publishDate.getFullYear()).padStart(4, '0'); @@ -11,25 +11,30 @@ const generatePermalink = async ({ id, slug, publishDate, category }) => { const minute = String(publishDate.getMinutes()).padStart(2, '0'); const second = String(publishDate.getSeconds()).padStart(2, '0'); - return POST_PERMALINK_PATTERN - .replace('%slug%', slug) + const permalink = POST_PERMALINK_PATTERN.replace('%slug%', slug) .replace('%id%', id) - .replace('%category%', category) + .replace('%category%', category || '') .replace('%year%', year) .replace('%month%', month) .replace('%day%', day) .replace('%hour%', hour) .replace('%minute%', minute) .replace('%second%', second); + + return permalink + .split('/') + .map((el) => trimSlash(el)) + .filter((el) => !!el) + .join('/'); }; -const getNormalizedPost = async (post: CollectionEntry<'posts'>): Promise => { +const getNormalizedPost = async (post: CollectionEntry<'post'>): Promise => { const { id, slug: rawSlug = '', data } = post; const { Content } = await post.render(); const { tags: rawTags = [], - category: rawCategory = 'default', + category: rawCategory, author = 'Anonymous', publishDate: rawPublishDate = new Date(), ...rest @@ -37,7 +42,7 @@ const getNormalizedPost = async (post: CollectionEntry<'posts'>): Promise const slug = cleanSlug(rawSlug.split('/').pop()); const publishDate = new Date(rawPublishDate); - const category = cleanSlug(rawCategory); + const category = rawCategory ? cleanSlug(rawCategory) : undefined; const tags = rawTags.map((tag: string) => cleanSlug(tag)); return { @@ -59,7 +64,7 @@ const getNormalizedPost = async (post: CollectionEntry<'posts'>): Promise }; const load = async function (): Promise> { - const posts = await getCollection('posts'); + const posts = await getCollection('post'); const normalizedPosts = posts.map(async (post) => await getNormalizedPost(post)); const results = (await Promise.all(normalizedPosts)) diff --git a/src/utils/permalinks.ts b/src/utils/permalinks.ts index aec0c56..83576e4 100644 --- a/src/utils/permalinks.ts +++ b/src/utils/permalinks.ts @@ -3,7 +3,7 @@ import slugify from 'limax'; import { SITE, BLOG } from '~/config.mjs'; import { trim } from '~/utils/utils'; -const trimSlash = (s: string) => trim(trim(s, '/')); +export const trimSlash = (s: string) => trim(trim(s, '/')); const createPath = (...params: string[]) => { const paths = params .map((el) => trimSlash(el))