Add helper functions for fetching events #115
|
@ -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"];
|
||||
|
||||
j285he marked this conversation as resolved
Outdated
|
||||
export async function getEventYears(): Promise<string[]> {
|
||||
return (await fs.readdir(EVENTS_PATH, { withFileTypes: true }))
|
||||
j285he marked this conversation as resolved
Outdated
a3thakra
commented
We are missing something here ... read the documentation for We are missing something here ... read the documentation for `withFileTypes` a bit more carefully. What does it return? What do we want to return?
|
||||
.filter((dirent) => dirent.isDirectory())
|
||||
.map((dirent) => dirent.name)
|
||||
j285he marked this conversation as resolved
Outdated
a3thakra
commented
same here same here
|
||||
.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"
|
||||
j285he marked this conversation as resolved
a3thakra
commented
imo this function should just return event slugs, and not the complete events. imo this function should just return event slugs, and not the complete events.
|
||||
);
|
||||
const { content, data: metadata } = matter(raw);
|
||||
|
||||
return {
|
||||
content: await serialize(content),
|
||||
metadata: metadata as Metadata,
|
||||
j285he marked this conversation as resolved
Outdated
a3thakra
commented
Can we switch to Can we switch to `foo.md` instead of `foo.event.md`?
a3thakra
commented
We were using We were using `.event.mdx` because that enabled us to easily cast the type of the file to what we wanted. But we can't really do that here.
|
||||
};
|
||||
}
|
||||
|
||||
export async function getEventsByTerm(
|
||||
year: string,
|
||||
term: string
|
||||
j285he marked this conversation as resolved
Outdated
a3thakra
commented
there is no need for doing this here there is no need for doing this here
|
||||
): Promise<string[]> {
|
||||
return (await fs.readdir(path.join(EVENTS_PATH, year, term)))
|
||||
.filter((name) => name.endsWith(".md"))
|
||||
.map((name) => name.slice(0, -".md".length));
|
||||
}
|
|
@ -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"
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue
You should use
options.withFileTypes
here and only return the items that are directoriesMore info here: https://nodejs.org/api/fs.html#fs_fspromises_readdir_path_options