4466ba436b
## Description This PR ensures that the default prettier config is used for examples and templates. This config is compatible with `prettier@3` as well (upgrading prettier is bigger change that can be a future PR). ## Changes - Updated `.prettierrc.json` in root with `"trailingComma": "es5"` (will be needed upgrading to prettier@3) - Added `examples/.prettierrc.json` with default config (this will change every example) - Added `packages/create-next-app/templates/.prettierrc.json` with default config (this will change every template) ## Related - Fixes #54402 - Closes #54409
43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
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;
|
|
}
|