diff --git a/lib/events.ts b/lib/events.ts new file mode 100644 index 00000000..4213f9b3 --- /dev/null +++ b/lib/events.ts @@ -0,0 +1,60 @@ +import fs from "fs/promises"; +import path from "path"; +import matter from "gray-matter"; +import { serialize } from "next-mdx-remote/serialize"; +import { MDXRemoteSerializeResult } from "next-mdx-remote"; + +const EVENTS_PATH = path.join("content", "events"); + +export async function getYears(): Promise { + return await fs.readdir(EVENTS_PATH); +} + +export async function getTermsByYear(year: string): Promise { + return await fs.readdir(path.join(EVENTS_PATH, year)); +} + +interface Metadata { + name: string; + short: string; + date: string; + online: boolean; + location: string; +} + +export interface Event { + content: string | MDXRemoteSerializeResult>; + metadata: Metadata; +} + +export async function getEventBySlug( + year: string, + term: string, + slug: string, + convert = true +): Promise { + const raw = await fs.readFile( + path.join(EVENTS_PATH, year, term, `${slug}.event.md`), + "utf-8" + ); + const { content, data: metadata } = matter(raw); + + return { + content: convert ? await serialize(content) : content, + metadata: metadata as Metadata, + }; +} + +export async function getEventsByTerm( + year: string, + term: string +): Promise { + const files = (await fs.readdir(path.join(EVENTS_PATH, year, term))) + .filter((name) => name.endsWith(".event.md")) + .map((name) => name.slice(0, -".event.md".length)); + + const props = await Promise.all( + files.map((file) => getEventBySlug(year, term, file)) + ); + return props; +}