61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
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<string[]> {
|
|
return await fs.readdir(EVENTS_PATH);
|
|
}
|
|
|
|
export async function getTermsByYear(year: string): Promise<string[]> {
|
|
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<Record<string, unknown>>;
|
|
metadata: Metadata;
|
|
}
|
|
|
|
export async function getEventBySlug(
|
|
year: string,
|
|
term: string,
|
|
slug: string,
|
|
convert = true
|
|
): Promise<Event> {
|
|
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<Event[]> {
|
|
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;
|
|
}
|