summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-01-20 16:23:48 +0100
committerArmand Philippot <git@armandphilippot.com>2022-01-20 16:23:48 +0100
commit8966fde6ff520cfbe74c031c8b2e3a66d298b172 (patch)
tree9144e961db587adb22bff88a23c6881ea7a935f9 /src/utils
parentf5ba1046b13acd239c472e361f345902937662fb (diff)
chore: add single project view
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/helpers/projects.ts86
1 files changed, 50 insertions, 36 deletions
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
@@ -3,46 +3,50 @@ import { readdirSync } from 'fs';
import path from 'path';
/**
+ * Retrieve project's data by id.
+ * @param {string} id - The filename without extension.
+ * @returns {Promise<Project>} - The project data.
+ */
+export const getProjectData = async (id: string): Promise<Project> => {
+ 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<Project[]>} An array of projects.
+ * @returns {Promise<Project[]>} An array of projects with meta.
*/
const getProjectsWithMeta = async (filenames: string[]): Promise<Project[]> => {
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);
})
);
};
@@ -60,12 +64,22 @@ const sortProjectByPublicationDate = (a: Project, b: Project) => {
};
/**
+ * 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<Project[]>} An array of projects.
*/
export const getSortedProjects = async (): Promise<Project[]> => {
- 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);