Skip to content

Commit

Permalink
fixes to posts
Browse files Browse the repository at this point in the history
  • Loading branch information
icco committed Aug 30, 2023
1 parent 54036cd commit 1eba612
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
13 changes: 7 additions & 6 deletions contentlayer.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = /#(?<tag>\w+)/g

Expand All @@ -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}`
}
Expand All @@ -33,31 +34,31 @@ 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<string>(
match.map((m) => m.replace(hashtagRegex, "$<tag>").toLowerCase())
match.map((m: string) => m.replace(hashtagRegex, "$<tag>").toLowerCase())
)

return Array.from(tags)
},
},
social_image: {
type: "string",
resolve: (post) =>
resolve: (post: Post) =>
GenerateSocialImage(
post.title,
format(parseISO(post.datetime), "LLLL d, yyyy")
),
},
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,
},
},
}))
Expand Down
27 changes: 26 additions & 1 deletion src/lib/posts.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
13 changes: 0 additions & 13 deletions src/lib/util.ts

This file was deleted.

0 comments on commit 1eba612

Please sign in to comment.