Add helper functions for fetching events (#115)
continuous-integration/drone/push Build is passing Details

helper functions for [year] and [year]/[term] only

closes #110

Co-authored-by: Jared He <66887902+jaredjhe@users.noreply.github.com>
Reviewed-on: #115
Reviewed-by: Aditya Thakral <a3thakra@csclub.uwaterloo.ca>
Co-authored-by: j285he <j285he@localhost>
Co-committed-by: j285he <j285he@localhost>
This commit is contained in:
Jared He 2021-08-17 14:58:06 -04:00
parent 2ced987f42
commit 64fbabf204
2 changed files with 66 additions and 2 deletions

64
lib/events.ts Normal file
View File

@ -0,0 +1,64 @@
import fs from "fs/promises";
import path from "path";
import matter from "gray-matter";
import { MDXRemoteSerializeResult } from "next-mdx-remote";
import { serialize } from "next-mdx-remote/serialize";
const EVENTS_PATH = path.join("content", "events");
const TERMS = ["winter", "spring", "fall"];
export async function getEventYears(): Promise<string[]> {
return (await fs.readdir(EVENTS_PATH, { withFileTypes: true }))
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
.sort();
}
export async function getEventTermsByYear(year: string): Promise<string[]> {
return (
await fs.readdir(path.join(EVENTS_PATH, year), { withFileTypes: true })
)
.filter((dirent) => dirent.isDirectory() && TERMS.includes(dirent.name))
.map((dirent) => dirent.name)
.sort((a, b) => TERMS.indexOf(a) - TERMS.indexOf(b));
}
interface Metadata {
name: string;
short: string;
date: string;
online: boolean;
location: string;
}
export interface Event {
content: MDXRemoteSerializeResult<Record<string, unknown>>;
metadata: Metadata;
}
export async function getEventBySlug(
year: string,
term: string,
slug: string
): Promise<Event> {
const raw = await fs.readFile(
path.join(EVENTS_PATH, year, term, `${slug}.md`),
"utf-8"
);
const { content, data: metadata } = matter(raw);
return {
content: await serialize(content),
metadata: metadata as Metadata,
};
}
export async function getEventsByTerm(
year: string,
term: string
): Promise<string[]> {
return (await fs.readdir(path.join(EVENTS_PATH, year, term)))
.filter((name) => name.endsWith(".md"))
.map((name) => name.slice(0, -".md".length));
}

4
package-lock.json generated
View File

@ -1,5 +1,5 @@
{
"name": "website",
"name": "www-new",
"version": "0.1.0",
"lockfileVersion": 2,
"requires": true,
@ -39,7 +39,7 @@
"typescript": "4.3.5"
},
"engines": {
"node": ">14",
"node": "^16",
"npm": "^7"
}
},