Fix content utilities

This commit is contained in:
Hazem Krimi
2022-02-13 19:17:00 +01:00
parent 04cc13815d
commit 763db6f5c1
2 changed files with 78 additions and 62 deletions
+39 -31
View File
@@ -7,46 +7,54 @@ const blogPostsDirectory = path.join(process.cwd(), '_blog');
export const getBlogPosts = () => {
const fileNames = fs.readdirSync(blogPostsDirectory);
const allBlogPostsData = fileNames.map(filename => {
const slug = filename.replace('.mdx', '');
if (fileNames) {
const allBlogPostsData = fileNames.map(filename => {
const slug = filename.replace('.mdx', '');
const fullPath = path.join(blogPostsDirectory, filename);
const fileContents = fs.readFileSync(fullPath, 'utf8');
const { data } = matter(fileContents);
const fullPath = path.join(blogPostsDirectory, filename);
const fileContents = fs.readFileSync(fullPath, 'utf8');
const { data } = matter(fileContents);
const options = { month: 'long', day: 'numeric', year: 'numeric' };
// @ts-ignore
const formattedDate = new Date(data.date).toLocaleDateString('en-IN', options);
const options = { month: 'long', day: 'numeric', year: 'numeric' };
// @ts-ignore
const formattedDate = new Date(data.date).toLocaleDateString('en-IN', options);
const frontmatter = {
...data,
date: formattedDate
};
return {
slug,
...frontmatter
};
});
const frontmatter = {
...data,
date: formattedDate
};
return {
slug,
...frontmatter
};
});
return allBlogPostsData.sort((a, b) => {
if (new Date(a.date) < new Date(b.date)) {
return 1;
} else {
return -1;
}
});
return allBlogPostsData.sort((a, b) => {
if (new Date(a.date) < new Date(b.date)) {
return 1;
} else {
return -1;
}
});
}
return [];
};
export const getBlogPostsSlugs = () => {
const fileNames = fs.readdirSync(blogPostsDirectory);
return fileNames.map(filename => {
return {
params: {
slug: filename.replace('.mdx', '')
}
};
});
if (fileNames) {
return fileNames.map(filename => {
return {
params: {
slug: filename.replace('.mdx', '')
}
};
});
}
return [];
};
export const getBlogPostdata = async (slug: string) => {