From 8966fde6ff520cfbe74c031c8b2e3a66d298b172 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Thu, 20 Jan 2022 16:23:48 +0100 Subject: chore: add single project view --- src/utils/helpers/projects.ts | 86 +++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 36 deletions(-) (limited to 'src/utils/helpers/projects.ts') diff --git a/src/utils/helpers/projects.ts b/src/utils/helpers/projects.ts index 3736242..12e5912 100644 --- a/src/utils/helpers/projects.ts +++ b/src/utils/helpers/projects.ts @@ -2,47 +2,51 @@ import { Project, ProjectMeta } from '@ts/types/app'; import { readdirSync } from 'fs'; import path from 'path'; +/** + * Retrieve project's data by id. + * @param {string} id - The filename without extension. + * @returns {Promise} - The project data. + */ +export const getProjectData = async (id: string): Promise => { + try { + const { + image, + intro, + meta, + seo, + }: { + image: string; + intro: string; + meta: ProjectMeta & { title: string }; + seo: { title: string; description: string }; + } = await import(`../../content/projects/${id}.mdx`); + + const { title, ...onlyMeta } = meta; + + return { + id, + cover: image || '', + intro: intro || '', + meta: onlyMeta || {}, + slug: id, + title, + seo: seo || {}, + }; + } catch (err) { + console.error(err); + throw err; + } +}; + /** * Retrieve the projects data from filenames. * @param {string[]} filenames - An array of filenames. - * @returns {Promise} An array of projects. + * @returns {Promise} An array of projects with meta. */ const getProjectsWithMeta = async (filenames: string[]): Promise => { return Promise.all( filenames.map(async (filename) => { - const id = filename.replace(/\.mdx$/, ''); - - try { - const { - image, - intro, - meta, - }: { image: string; intro: string; meta: ProjectMeta } = await import( - `../../content/projects/${filename}` - ); - - const projectMeta: ProjectMeta = meta - ? meta - : { - title: '', - publishedOn: '', - updatedOn: '', - license: '', - }; - const projectIntro = intro ? intro : ''; - const projectCover = image ? image : ''; - - return { - id, - cover: projectCover, - intro: projectIntro, - meta: projectMeta, - slug: id, - }; - } catch (err) { - console.error(err); - throw err; - } + return getProjectData(filename); }) ); }; @@ -59,13 +63,23 @@ const sortProjectByPublicationDate = (a: Project, b: Project) => { return 0; }; +/** + * Retrieve all the projects filename. + * @returns {string[]} An array of filenames. + */ +export const getAllProjectsFilename = (): string[] => { + const projectsDirectory = path.join(process.cwd(), 'src/content/projects'); + const filenames = readdirSync(projectsDirectory); + + return filenames.map((filename) => filename.replace(/\.mdx$/, '')); +}; + /** * Retrieve all projects in content folder sorted by publication date. * @returns {Promise} An array of projects. */ export const getSortedProjects = async (): Promise => { - const projectsDirectory = path.join(process.cwd(), 'src/content/projects'); - const filenames = readdirSync(projectsDirectory); + const filenames = getAllProjectsFilename(); const projects = await getProjectsWithMeta(filenames); return [...projects].sort(sortProjectByPublicationDate); -- cgit v1.2.3