From 1eba6123de6381109d9edeb468be67eb7ffaea03 Mon Sep 17 00:00:00 2001 From: Nat Welch Date: Wed, 30 Aug 2023 00:59:59 +0000 Subject: [PATCH] fixes to posts --- contentlayer.config.ts | 13 +++++++------ src/lib/posts.ts | 27 ++++++++++++++++++++++++++- src/lib/util.ts | 13 ------------- 3 files changed, 33 insertions(+), 20 deletions(-) delete mode 100644 src/lib/util.ts diff --git a/contentlayer.config.ts b/contentlayer.config.ts index f2a6a807..8864c72f 100644 --- a/contentlayer.config.ts +++ b/contentlayer.config.ts @@ -6,6 +6,7 @@ import remarkGfm from "remark-gfm" import { remarkHashtags } from "./src/lib/hashtags" import { GenerateSocialImage } from "./src/lib/socialimage" +import { Post } from "contentlayer/generated" const hashtagRegex = /#(?\w+)/g @@ -24,7 +25,7 @@ export const Post = defineDocumentType(() => ({ computedFields: { url: { type: "string", - resolve: (post) => { + resolve: (post: Post) => { if (post.draft) { return `/api/draft?secret=${process.env.SECRET_TOKEN}&slug=${post.id}` } @@ -33,11 +34,11 @@ export const Post = defineDocumentType(() => ({ }, tags: { type: "list", - resolve: (post) => { + resolve: (post: Post) => { const match = post.body.raw.match(hashtagRegex) if (!match) return [] const tags = new Set( - match.map((m) => m.replace(hashtagRegex, "$").toLowerCase()) + match.map((m: string) => m.replace(hashtagRegex, "$").toLowerCase()) ) return Array.from(tags) @@ -45,7 +46,7 @@ export const Post = defineDocumentType(() => ({ }, social_image: { type: "string", - resolve: (post) => + resolve: (post: Post) => GenerateSocialImage( post.title, format(parseISO(post.datetime), "LLLL d, yyyy") @@ -53,11 +54,11 @@ export const Post = defineDocumentType(() => ({ }, readingTime: { type: "number", - resolve: (post) => readingTime(post.body.raw).minutes, + resolve: (post: Post) => readingTime(post.body.raw).minutes, }, wordCount: { type: "number", - resolve: (post) => readingTime(post.body.raw).words, + resolve: (post: Post) => readingTime(post.body.raw).words, }, }, })) diff --git a/src/lib/posts.ts b/src/lib/posts.ts index 65b3e397..31cb530c 100644 --- a/src/lib/posts.ts +++ b/src/lib/posts.ts @@ -1,6 +1,7 @@ import { compareDesc, isFuture } from "date-fns" +import { notFound } from "next/navigation" -import { allPosts } from "contentlayer/generated" +import { Post, allPosts } from "contentlayer/generated" export default function publishedPosts() { const posts = allPosts @@ -10,3 +11,27 @@ export default function publishedPosts() { return posts } + +export function previousPost(slug: string) { + const posts = publishedPosts() + const currentIndex = posts.findIndex((post) => post.id === slug) + const previousPost = posts[currentIndex + 1] + return previousPost +} + +export function nextPost(slug: string) { + const posts = publishedPosts() + const currentIndex = posts.findIndex((post) => post.id === slug) + const nextPost = posts[currentIndex - 1] + return nextPost +} + +export function getPostBySlug(slug: string): Post { + const slugNumber = parseInt(slug) + const post: Post | undefined = allPosts.find((post) => post.id === slugNumber) + if (!post) { + notFound() + } + + return post +} \ No newline at end of file diff --git a/src/lib/util.ts b/src/lib/util.ts deleted file mode 100644 index 83e5f387..00000000 --- a/src/lib/util.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { notFound } from "next/navigation" - -import { allPosts, Post } from "contentlayer/generated" - -export function getPostBySlug(slug: string): Post { - const slugNumber = parseInt(slug) - const post: Post | undefined = allPosts.find((post) => post.id === slugNumber) - if (!post) { - notFound() - } - - return post -}