From f1ed6df7a11300191abc0b1068e0458ae242de6c Mon Sep 17 00:00:00 2001 From: prototypa Date: Fri, 6 Jan 2023 15:15:50 -0500 Subject: [PATCH] Change posts.js to blog.js and improve default values --- src/components/blog/HighlightedPosts.astro | 2 +- src/components/blog/LatestPosts.astro | 2 +- src/content/config.ts | 7 ++++--- src/pages/[...blog]/[...page].astro | 7 +++---- src/pages/[...blog]/[category]/[...page].astro | 6 +++--- src/pages/[...blog]/[slug].astro | 9 ++++----- src/pages/[...blog]/[tag]/[...page].astro | 6 +++--- src/pages/rss.xml.ts | 2 +- src/types.ts | 2 +- src/utils/{posts.ts => blog.ts} | 11 ++++++++++- 10 files changed, 31 insertions(+), 23 deletions(-) rename src/utils/{posts.ts => blog.ts} (86%) diff --git a/src/components/blog/HighlightedPosts.astro b/src/components/blog/HighlightedPosts.astro index 1a327d9..f365c6b 100644 --- a/src/components/blog/HighlightedPosts.astro +++ b/src/components/blog/HighlightedPosts.astro @@ -2,7 +2,7 @@ import Grid from '~/components/blog/Grid.astro'; import { getBlogPermalink } from '~/utils/permalinks'; -import { findPostsByIds } from '~/utils/posts'; +import { findPostsByIds } from '~/utils/blog'; const ids = [ 'get-started-website-with-astro-tailwind-css.md', diff --git a/src/components/blog/LatestPosts.astro b/src/components/blog/LatestPosts.astro index 2dd8522..b46cead 100644 --- a/src/components/blog/LatestPosts.astro +++ b/src/components/blog/LatestPosts.astro @@ -2,7 +2,7 @@ import Grid from '~/components/blog/Grid.astro'; import { getBlogPermalink } from '~/utils/permalinks'; -import { findLatestPosts } from '~/utils/posts'; +import { findLatestPosts } from '~/utils/blog'; const count = 4; const posts = await findLatestPosts({ count }); diff --git a/src/content/config.ts b/src/content/config.ts index e855425..11a1241 100644 --- a/src/content/config.ts +++ b/src/content/config.ts @@ -1,4 +1,5 @@ import { z, defineCollection } from 'astro:content'; +import { cleanSlug } from '~/utils/permalinks'; const blog = defineCollection({ schema: { @@ -9,16 +10,16 @@ const blog = defineCollection({ canonical: z.string().url().optional(), permalink: z.string().optional(), - publishDate: z.string().transform((str) => new Date(str)), + publishDate: z.date().optional(), draft: z.boolean().optional(), excerpt: z.string().optional(), category: z.string().optional(), tags: z.array(z.string()).optional(), - authors: z.array(z.string()).optional(), + author: z.string().optional(), }, slug: ({ defaultSlug, data }) => { - return data.permalink || defaultSlug; + return cleanSlug(data.permalink || defaultSlug); }, }); diff --git a/src/pages/[...blog]/[...page].astro b/src/pages/[...blog]/[...page].astro index d8c7122..92a4b66 100644 --- a/src/pages/[...blog]/[...page].astro +++ b/src/pages/[...blog]/[...page].astro @@ -5,14 +5,13 @@ import Layout from '~/layouts/PageLayout.astro'; import BlogList from '~/components/blog/List.astro'; import Pagination from '~/components/common/Pagination.astro'; -import { fetchPosts } from '~/utils/posts'; -import { getCanonical, getPermalink, BLOG_BASE } from '~/utils/permalinks'; +import { fetchPosts } from '~/utils/blog'; +import { BLOG_BASE } from '~/utils/permalinks'; import Title from '~/components/blog/Title.astro'; export async function getStaticPaths({ paginate }) { if (BLOG?.disabled || BLOG?.blog?.disabled) return []; - const posts = await fetchPosts(); - return paginate(posts, { + return paginate(await fetchPosts(), { params: { blog: BLOG_BASE || undefined }, pageSize: BLOG.postsPerPage, }); diff --git a/src/pages/[...blog]/[category]/[...page].astro b/src/pages/[...blog]/[category]/[...page].astro index ae9b488..ea7e93e 100644 --- a/src/pages/[...blog]/[category]/[...page].astro +++ b/src/pages/[...blog]/[category]/[...page].astro @@ -5,8 +5,8 @@ import Layout from '~/layouts/PageLayout.astro'; import BlogList from '~/components/blog/List.astro'; import Pagination from '~/components/common/Pagination.astro'; -import { fetchPosts } from '~/utils/posts'; -import { cleanSlug, CATEGORY_BASE } from '~/utils/permalinks'; +import { fetchPosts } from '~/utils/blog'; +import { CATEGORY_BASE } from '~/utils/permalinks'; import Title from '~/components/blog/Title.astro'; export async function getStaticPaths({ paginate }) { @@ -22,7 +22,7 @@ export async function getStaticPaths({ paginate }) { paginate( posts.filter((post) => typeof post.category === 'string' && category === post.category.toLowerCase()), { - params: { category: cleanSlug(category), blog: CATEGORY_BASE || undefined }, + params: { category: category, blog: CATEGORY_BASE || undefined }, pageSize: BLOG.postsPerPage, props: { category }, } diff --git a/src/pages/[...blog]/[slug].astro b/src/pages/[...blog]/[slug].astro index 252073d..0978505 100644 --- a/src/pages/[...blog]/[slug].astro +++ b/src/pages/[...blog]/[slug].astro @@ -5,16 +5,15 @@ import Layout from '~/layouts/PageLayout.astro'; import SinglePost from '~/components/blog/SinglePost.astro'; import ToBlogLink from '~/components/blog/ToBlogLink.astro'; -import { getCanonical, getPermalink, cleanSlug, POST_BASE } from '~/utils/permalinks'; -import { fetchPosts } from '~/utils/posts'; +import { getCanonical, getPermalink, POST_BASE } from '~/utils/permalinks'; +import { fetchPosts } from '~/utils/blog'; import { findImage } from '~/utils/images'; export async function getStaticPaths() { if (BLOG?.disabled || BLOG?.post?.disabled) return []; - const posts = await fetchPosts(); - return posts.map((post) => ({ + return (await fetchPosts()).map((post) => ({ params: { - slug: cleanSlug(post.slug), + slug: post.slug, blog: POST_BASE || undefined, }, props: { post }, diff --git a/src/pages/[...blog]/[tag]/[...page].astro b/src/pages/[...blog]/[tag]/[...page].astro index 7019404..d91027a 100644 --- a/src/pages/[...blog]/[tag]/[...page].astro +++ b/src/pages/[...blog]/[tag]/[...page].astro @@ -5,8 +5,8 @@ import Layout from '~/layouts/PageLayout.astro'; import BlogList from '~/components/blog/List.astro'; import Pagination from '~/components/common/Pagination.astro'; -import { fetchPosts } from '~/utils/posts'; -import { getCanonical, getPermalink, cleanSlug, TAG_BASE } from '~/utils/permalinks'; +import { fetchPosts } from '~/utils/blog'; +import { TAG_BASE } from '~/utils/permalinks'; import Title from '~/components/blog/Title.astro'; export async function getStaticPaths({ paginate }) { @@ -22,7 +22,7 @@ export async function getStaticPaths({ paginate }) { paginate( posts.filter((post) => Array.isArray(post.tags) && post.tags.find((elem) => elem.toLowerCase() === tag)), { - params: { tag: cleanSlug(tag), blog: TAG_BASE || undefined }, + params: { tag: tag, blog: TAG_BASE || undefined }, pageSize: BLOG.postsPerPage, props: { tag }, } diff --git a/src/pages/rss.xml.ts b/src/pages/rss.xml.ts index d56438d..bf12472 100644 --- a/src/pages/rss.xml.ts +++ b/src/pages/rss.xml.ts @@ -1,7 +1,7 @@ import rss from '@astrojs/rss'; import { SITE, BLOG } from '~/config.mjs'; -import { fetchPosts } from '~/utils/posts'; +import { fetchPosts } from '~/utils/blog'; import { getPermalink } from '~/utils/permalinks'; export const get = async () => { diff --git a/src/types.ts b/src/types.ts index 7164312..075127b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -16,7 +16,7 @@ export interface Post { excerpt?: string; category?: string; tags?: Array; - authors?: Array; + author?: string; Content: unknown; content?: string; diff --git a/src/utils/posts.ts b/src/utils/blog.ts similarity index 86% rename from src/utils/posts.ts rename to src/utils/blog.ts index 3476cb3..f83c462 100644 --- a/src/utils/posts.ts +++ b/src/utils/blog.ts @@ -1,15 +1,24 @@ import { getCollection, getEntry } from 'astro:content'; import type { CollectionEntry } from 'astro:content'; import type { Post } from '~/types'; +import { cleanSlug } from './permalinks'; const getNormalizedPost = async (post: CollectionEntry<'blog'>): Promise => { const { id, slug, data } = post; const { Content, injectedFrontmatter } = await post.render(); + const { tags = [], category = 'default', author = 'Anonymous', publishDate, ...rest } = data; + return { id: id, slug: slug, - ...data, + + publishDate: new Date(publishDate), + category: cleanSlug(category), + tags: tags.map((tag: string) => cleanSlug(tag)), + author, + + ...rest, Content: Content, // or 'body' in case you consume from API