Download next.js/examples/cms-tina/lib/api.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/examples/cms-tina/lib/api.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/examples/cms-tina/lib/api.js
-
curl -L -o api.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/examples/cms-tina/lib/api.js
1.1 kB
| 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, fields = []) { | |
| const realSlug = slug.replace(/\.md$/, ""); | |
| const fullPath = join(postsDirectory, `${realSlug}.md`); | |
| const fileContents = fs.readFileSync(fullPath, "utf8"); | |
| const { data, content } = matter(fileContents); | |
| const items = {}; | |
| // 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 = []) { | |
| 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; | |
| } | |