Buckets:
| import type { Post } from "../interfaces"; | |
| import fs from "fs"; | |
| import { join } from "path"; | |
| import matter from "gray-matter"; | |
| const postsDirectory = join(process.cwd(), "_posts"); | |
| export function getPostSlugs() { | |
| return fs.readdirSync(postsDirectory); | |
| } | |
| export function getPostBySlug(slug: string, fields: string[] = []) { | |
| const realSlug = slug.replace(/\.md$/, ""); | |
| const fullPath = join(postsDirectory, `${realSlug}.md`); | |
| const fileContents = fs.readFileSync(fullPath, "utf8"); | |
| const { data, content } = matter(fileContents); | |
| const items: Post = {}; | |
| // Ensure only the minimal needed data is exposed | |
| fields.forEach((field) => { | |
| if (field === "slug") { | |
| items[field] = realSlug; | |
| } | |
| if (field === "content") { | |
| items[field] = content; | |
| } | |
| if (typeof data[field] !== "undefined") { | |
| items[field] = data[field]; | |
| } | |
| }); | |
| return items; | |
| } | |
| export function getAllPosts(fields: string[] = []) { | |
| const slugs = getPostSlugs(); | |
| const posts = slugs | |
| .map((slug) => getPostBySlug(slug, fields)) | |
| // sort posts by date in descending order | |
| .sort((post1, post2) => (post1.date > post2.date ? -1 : 1)); | |
| return posts; | |
| } | |
Xet Storage Details
- Size:
- 1.18 kB
- Xet hash:
- f6cdd7b24f19aab7782c394fa0986afd750eb06fcac2093519adbb4ac832f484
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.