A blog - Markdown
March 31, 2025
准备
- File
- code
开始
- 文章
在app
目录下创建content/blogs
用于存放文章
已複製!--- title: 'A blog - Install' date: '2025-3-30' description: 'How to set up a new Next.js project' authors: - Kyle Lou --- ## 准备 - Node.js ( >=18.18 version ) - npm ## 开始 To create a project, run: ```command npx create-next-app@latest ``` ## References - [Next.js Installation](https://nextjs.org/docs/app/getting-started/installation)
- Markdown 语法
TODO 学习语法
工具
在app
目录下创建lib/reader.ts
已複製!import { PostProps } from '@/types' import { readFileSync, readdirSync } from 'fs' import { join } from 'path' import matter from 'gray-matter' import readingTime from 'reading-time' const postsDirectory = join(process.cwd(), 'content', 'blogs') export const getPostSlugs = () => { return readdirSync(postsDirectory) } export const getPostBySlug = (slug: string): PostProps => { const realSlug = slug.replace(/\.mdx$/, '') const fullPath = join(postsDirectory, `${realSlug}/index.mdx`) const fileContents = readFileSync(fullPath) const { data, content } = matter(fileContents) const readTime = readingTime(content) return { ...data, date: data.date, slug: realSlug, readingTime: readTime, content, } as PostProps } export const getAllPosts = (): PostProps[] => { const slugs = getPostSlugs() const posts = slugs.map((slug) => getPostBySlug(slug)) return posts.sort((a, b) => b.date.localeCompare(a.date)) }