lib: remove source field from Post interface, use gray-matter for reading time only
This commit is contained in:
10
lib/posts.ts
10
lib/posts.ts
@@ -16,9 +16,7 @@ export interface PostMeta {
|
|||||||
readingTime: number
|
readingTime: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Post extends PostMeta {
|
export interface Post extends PostMeta {}
|
||||||
source: string // raw MDX string, NOT compiled
|
|
||||||
}
|
|
||||||
|
|
||||||
const getMdxFiles = cache(async () => {
|
const getMdxFiles = cache(async () => {
|
||||||
try {
|
try {
|
||||||
@@ -64,9 +62,10 @@ export const getPost = async (slug: string): Promise<Post | null> => {
|
|||||||
|
|
||||||
const filePath = path.join(postsDirectory, file)
|
const filePath = path.join(postsDirectory, file)
|
||||||
const raw = await fs.promises.readFile(filePath, 'utf8')
|
const raw = await fs.promises.readFile(filePath, 'utf8')
|
||||||
const { data, content } = matter(raw)
|
const { data } = matter(raw)
|
||||||
|
|
||||||
const readingTime = Math.max(1, Math.ceil(content.split(/\s+/).length / 200))
|
const contentForReadingTime = raw.replace(/^---[\s\S]*?---\s*?/, '')
|
||||||
|
const readingTime = Math.max(1, Math.ceil(contentForReadingTime.split(/\s+/).filter(Boolean).length / 200))
|
||||||
|
|
||||||
return {
|
return {
|
||||||
slug,
|
slug,
|
||||||
@@ -76,7 +75,6 @@ export const getPost = async (slug: string): Promise<Post | null> => {
|
|||||||
tags: data.tags ?? [],
|
tags: data.tags ?? [],
|
||||||
author: data.author ?? null,
|
author: data.author ?? null,
|
||||||
coverImage: data.coverImage ?? null,
|
coverImage: data.coverImage ?? null,
|
||||||
source: content,
|
|
||||||
readingTime,
|
readingTime,
|
||||||
} satisfies Post
|
} satisfies Post
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user