1
1
mirror of https://github.com/theoludwig/theoludwig.git synced 2024-11-05 04:51:30 +01:00
.profile/utils/blog.ts

86 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-11-08 15:10:26 +01:00
import fs from 'node:fs'
import path from 'node:path'
import type { MDXRemoteSerializeResult } from 'next-mdx-remote'
import { nodeTypes } from '@mdx-js/mdx'
import rehypeRaw from 'rehype-raw'
2021-11-08 15:10:26 +01:00
import { serialize } from 'next-mdx-remote/serialize'
import remarkGfm from 'remark-gfm'
2021-11-09 15:14:31 +01:00
import rehypeSlug from 'rehype-slug'
2021-11-08 15:10:26 +01:00
import matter from 'gray-matter'
import { getHighlighter } from 'shiki'
2021-11-08 15:10:26 +01:00
import { remarkSyntaxHighlightingPlugin } from './remarkSyntaxHighlightingPlugin'
export const POSTS_PATH = path.join(process.cwd(), 'posts')
2021-11-08 15:10:26 +01:00
export type MDXSource = MDXRemoteSerializeResult<Record<string, unknown>>
export interface FrontMatter {
title: string
description: string
isPublished: boolean
publishedOn: string
}
export interface PostMetadata {
frontmatter: FrontMatter
slug: string
content: string
}
export interface Post extends PostMetadata {
source: MDXSource
}
export const getPosts = async (): Promise<PostMetadata[]> => {
const posts = await fs.promises.readdir(POSTS_PATH)
2021-11-08 15:10:26 +01:00
const postsWithTime = await Promise.all(
posts.map(async (postFilename) => {
const [slug] = postFilename.split('.')
const blogPostPath = path.join(POSTS_PATH, `${slug}.mdx`)
2021-11-08 15:10:26 +01:00
const blogPostContent = await fs.promises.readFile(blogPostPath, {
encoding: 'utf8'
})
const { data, content } = matter(blogPostContent) as any
const date = new Date(data.publishedOn)
return {
slug,
content,
frontmatter: data,
time: date.getTime()
}
})
)
const postsWithTimeSorted = postsWithTime
.filter((post) => post.frontmatter.isPublished)
.sort((a, b) => b.time - a.time)
return postsWithTimeSorted
}
export const getPostBySlug = async (
slug?: string | string[]
): Promise<Post | undefined> => {
const posts = await getPosts()
const post = posts.find((post) => post.slug === slug)
if (post == null) {
return undefined
}
const highlighter = await getHighlighter({
theme: 'dark-plus'
})
2021-11-08 15:10:26 +01:00
const source = await serialize(post.content, {
mdxOptions: {
remarkPlugins: [
remarkGfm as any,
[remarkSyntaxHighlightingPlugin, { highlighter }]
],
rehypePlugins: [
rehypeSlug as any,
[rehypeRaw, { passThrough: nodeTypes }]
]
2021-11-08 15:10:26 +01:00
}
})
return { ...post, source }
}