aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/atoms/layout/section/index.ts
blob: 2786cf0d803a6a464cddfd22e6a1b583e68c4458 (plain)
1
export * from './section';
.sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
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<Project>} - The project data.
 */
export const getProjectData = async (id: string): Promise<Project> => {
  try {
    const {
      intro,
      meta,
      seo,
      tagline,
    }: {
      intro: string;
      meta: ProjectMeta & { title: string };
      seo: { title: string; description: string };
      tagline?: string;
    } = await import(`../../content/projects/${id}.mdx`);

    const { title, ...onlyMeta } = meta;

    return {
      id,
      intro: intro || '',
      meta: onlyMeta || {},
      slug: id,
      title,
      seo: seo || {},
      tagline: tagline || '',
    };
  } 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 with meta.
 */
const getProjectsWithMeta = async (filenames: string[]): Promise<Project[]> => {
  return Promise.all(
    filenames.map(async (filename) => {
      return getProjectData(filename);
    })
  );
};

/**
 * Method to sort an array of projects by publication date.
 * @param {Project} a - A single project.
 * @param {Project} b - A single project.
 * @returns The result used by Array.sort() method: 1 || -1 || 0.
 */
const sortProjectByPublicationDate = (a: Project, b: Project) => {
  if (a.meta.publishedOn < b.meta.publishedOn) return 1;
  if (a.meta.publishedOn > b.meta.publishedOn) return -1;
  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<Project[]>} An array of projects.
 */
export const getSortedProjects = async (): Promise<Project[]> => {
  const filenames = getAllProjectsFilename();
  const projects = await getProjectsWithMeta(filenames);

  return [...projects].sort(sortProjectByPublicationDate);
};