Download next.js/examples/blog-starter/src/lib/api.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 840 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/examples/blog-starter/src/lib/api.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/examples/blog-starter/src/lib/api.ts
-
curl -L -o api.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/examples/blog-starter/src/lib/api.ts
840 Bytes
| import { Post } from "@/interfaces/post"; | |
| import fs from "fs"; | |
| import matter from "gray-matter"; | |
| import { join } from "path"; | |
| const postsDirectory = join(process.cwd(), "_posts"); | |
| export function getPostSlugs() { | |
| return fs.readdirSync(postsDirectory); | |
| } | |
| export function getPostBySlug(slug: string) { | |
| const realSlug = slug.replace(/\.md$/, ""); | |
| const fullPath = join(postsDirectory, `${realSlug}.md`); | |
| const fileContents = fs.readFileSync(fullPath, "utf8"); | |
| const { data, content } = matter(fileContents); | |
| return { ...data, slug: realSlug, content } as Post; | |
| } | |
| export function getAllPosts(): Post[] { | |
| const slugs = getPostSlugs(); | |
| const posts = slugs | |
| .map((slug) => getPostBySlug(slug)) | |
| // sort posts by date in descending order | |
| .sort((post1, post2) => (post1.date > post2.date ? -1 : 1)); | |
| return posts; | |
| } | |