diff options
Diffstat (limited to 'src/utils/helpers')
| -rw-r--r-- | src/utils/helpers/projects.ts | 80 | ||||
| -rw-r--r-- | src/utils/helpers/slugify.ts | 18 | ||||
| -rw-r--r-- | src/utils/helpers/strings.ts | 29 |
3 files changed, 77 insertions, 50 deletions
diff --git a/src/utils/helpers/projects.ts b/src/utils/helpers/projects.ts index 02e0e02..a0f0c04 100644 --- a/src/utils/helpers/projects.ts +++ b/src/utils/helpers/projects.ts @@ -1,4 +1,4 @@ -import { ProjectCard } from '@ts/types/app'; +import { ProjectCard, ProjectPreview } from '@ts/types/app'; import { MDXProjectMeta } from '@ts/types/mdx'; import { readdirSync } from 'fs'; import path from 'path'; @@ -16,45 +16,61 @@ export const getProjectFilenames = (): string[] => { }; /** - * Retrieve project's data by filename. + * Retrieve the data of a project by filename. + * + * @param {string} filename - The project filename. + * @returns {Promise<ProjectPreview>} + */ +export const getProjectData = async ( + filename: string +): Promise<ProjectPreview> => { + try { + const { + meta, + }: { + meta: MDXProjectMeta; + } = await import(`../../content/projects/${filename}.mdx`); + + const { dates, intro, title, ...projectMeta } = meta; + const { publication, update } = dates; + const cover = await import(`../../../public/projects/${filename}.jpg`); + + return { + id: filename, + intro, + meta: { + ...projectMeta, + dates: { publication, update }, + // 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; + } +}; + +/** + * Retrieve all the projects data using filenames. * * @param {string[]} filenames - The filenames without extension. - * @returns {Promise<ProjectCard[]>} - The project data. + * @returns {Promise<ProjectCard[]>} - An array of projects 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; - } + const { id, meta, slug, title } = await getProjectData(filename); + const { cover, dates, tagline, technologies } = meta; + return { id, meta: { cover, dates, tagline, technologies }, slug, title }; }) ); }; diff --git a/src/utils/helpers/slugify.ts b/src/utils/helpers/slugify.ts deleted file mode 100644 index 55ff583..0000000 --- a/src/utils/helpers/slugify.ts +++ /dev/null @@ -1,18 +0,0 @@ -/** - * Convert a text into a slug or id. - * https://gist.github.com/codeguy/6684588#gistcomment-3332719 - * - * @param {string} text Text to slugify. - */ -export const slugify = (text: string) => { - return text - .toString() - .normalize('NFD') - .replace(/[\u0300-\u036f]/g, '') - .toLowerCase() - .trim() - .replace(/\s+/g, '-') - .replace(/[^\w\-]+/g, '-') - .replace(/\-\-+/g, '-') - .replace(/(^-)|(-$)/g, ''); -}; diff --git a/src/utils/helpers/strings.ts b/src/utils/helpers/strings.ts new file mode 100644 index 0000000..5d90161 --- /dev/null +++ b/src/utils/helpers/strings.ts @@ -0,0 +1,29 @@ +/** + * Convert a text into a slug or id. + * https://gist.github.com/codeguy/6684588#gistcomment-3332719 + * + * @param {string} text - A text to slugify. + * @returns {string} The slug. + */ +export const slugify = (text: string): string => { + return text + .toString() + .normalize('NFD') + .replace(/[\u0300-\u036f]/g, '') + .toLowerCase() + .trim() + .replace(/\s+/g, '-') + .replace(/[^\w\-]+/g, '-') + .replace(/\-\-+/g, '-') + .replace(/(^-)|(-$)/g, ''); +}; + +/** + * Capitalize the first letter of a string. + * + * @param {string} text - A text to capitalize. + * @returns {string} The capitalized text. + */ +export const capitalize = (text: string): string => { + return text.replace(/^\w/, (firstLetter) => firstLetter.toUpperCase()); +}; |
