summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-05-05 17:58:40 +0200
committerArmand Philippot <git@armandphilippot.com>2022-05-05 18:20:18 +0200
commita13022cd4c0a7cf0f00a6db49fad13db22d63dd6 (patch)
tree80dce20873c8812a07ced25b6ded1c4d272fb23a /src/utils
parentee04742d1f0645908baa30e47845126c28848f50 (diff)
chore: add a Projects page
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/helpers/projects.ts85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/utils/helpers/projects.ts b/src/utils/helpers/projects.ts
new file mode 100644
index 0000000..02e0e02
--- /dev/null
+++ b/src/utils/helpers/projects.ts
@@ -0,0 +1,85 @@
+import { ProjectCard } from '@ts/types/app';
+import { MDXProjectMeta } from '@ts/types/mdx';
+import { readdirSync } from 'fs';
+import path from 'path';
+
+/**
+ * Retrieve all the projects filename.
+ *
+ * @returns {string[]} An array of filenames.
+ */
+export const getProjectFilenames = (): string[] => {
+ const projectsDirectory = path.join(process.cwd(), 'src/content/projects');
+ const filenames = readdirSync(projectsDirectory);
+
+ return filenames.map((filename) => filename.replace(/\.mdx$/, ''));
+};
+
+/**
+ * Retrieve project's data by filename.
+ *
+ * @param {string[]} filenames - The filenames without extension.
+ * @returns {Promise<ProjectCard[]>} - The project data.
+ */
+export const getProjectsData = async (
+ filenames: string[]
+): Promise<ProjectCard[]> => {
+ return Promise.all(
+ filenames.map(async (filename) => {
+ try {
+ const {
+ meta,
+ }: {
+ meta: MDXProjectMeta;
+ } = await import(`../../content/projects/${filename}.mdx`);
+
+ const { intro: _intro, title, ...projectMeta } = meta;
+
+ const cover = await import(`../../../public/projects/${filename}.jpg`);
+
+ return {
+ id: filename,
+ meta: {
+ ...projectMeta,
+ // Dynamic import source does not work so I use it only to get sizes
+ cover: {
+ ...cover.default,
+ alt: `${title} image`,
+ src: `/projects/${filename}.jpg`,
+ },
+ },
+ slug: filename,
+ title: title,
+ };
+ } catch (err) {
+ console.error(err);
+ throw err;
+ }
+ })
+ );
+};
+
+/**
+ * Method to sort an array of projects by publication date.
+ *
+ * @param {ProjectCard} a - A single project.
+ * @param {ProjectCard} b - A single project.
+ * @returns The result used by Array.sort() method: 1 || -1 || 0.
+ */
+const sortProjectsByPublicationDate = (a: ProjectCard, b: ProjectCard) => {
+ if (a.meta.dates.publication < b.meta.dates.publication) return 1;
+ if (a.meta.dates.publication > b.meta.dates.publication) return -1;
+ return 0;
+};
+
+/**
+ * Retrieve all projects in content folder sorted by publication date.
+ *
+ * @returns {Promise<ProjectCard[]>} An array of projects.
+ */
+export const getProjectsCard = async (): Promise<ProjectCard[]> => {
+ const filenames = getProjectFilenames();
+ const projects = await getProjectsData(filenames);
+
+ return [...projects].sort(sortProjectsByPublicationDate);
+};