From 763db6f5c1c3f87ac2246b02ab12e4781810bca1 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Sun, 13 Feb 2022 19:17:00 +0100 Subject: [PATCH] Fix content utilities --- utils/blog.ts | 70 ++++++++++++++++++++++++++-------------------- utils/portfolio.ts | 70 ++++++++++++++++++++++++++-------------------- 2 files changed, 78 insertions(+), 62 deletions(-) diff --git a/utils/blog.ts b/utils/blog.ts index 48ae207..1ef2525 100644 --- a/utils/blog.ts +++ b/utils/blog.ts @@ -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) => { diff --git a/utils/portfolio.ts b/utils/portfolio.ts index 8eaaff9..be53093 100644 --- a/utils/portfolio.ts +++ b/utils/portfolio.ts @@ -7,46 +7,54 @@ const portfolioProjects = path.join(process.cwd(), '_portfolio'); export const getPortfolioProjects = () => { const fileNames = fs.readdirSync(portfolioProjects); - const allPortfolioProjectsData = fileNames.map(filename => { - const slug = filename.replace('.mdx', ''); + if (fileNames) { + const allPortfolioProjectsData = fileNames.map(filename => { + const slug = filename.replace('.mdx', ''); - const fullPath = path.join(portfolioProjects, filename); - const fileContents = fs.readFileSync(fullPath, 'utf8'); - const { data } = matter(fileContents); + const fullPath = path.join(portfolioProjects, 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 allPortfolioProjectsData.sort((a, b) => { - if (new Date(a.date) < new Date(b.date)) { - return 1; - } else { - return -1; - } - }); + return allPortfolioProjectsData.sort((a, b) => { + if (new Date(a.date) < new Date(b.date)) { + return 1; + } else { + return -1; + } + }); + } + + return []; }; export const getPortfolioPorjectsSlugs = () => { const fileNames = fs.readdirSync(portfolioProjects); - 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 getPortfolioProjectdata = async (slug: string) => {