77 lines
1.8 KiB
TypeScript
77 lines
1.8 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, { withFileTypes: true }, (err, files)=>err ? console.log(err));
|
|
// return await fs.readdir(
|
|
// __dirname,
|
|
// { withFileTypes: true },
|
|
// (err: Error, files: string[]) => {
|
|
// if (err) console.log(err);
|
|
// else {
|
|
// files.forEach((file) => {
|
|
// console.log(file);
|
|
// });
|
|
// }
|
|
// }
|
|
// );
|
|
}
|
|
|
|
fs.readdir(__dirname, { withFileTypes: true }, (err, files) => {
|
|
console.log("\nCurrent directory files:");
|
|
if (err) console.log(err);
|
|
else {
|
|
files.forEach((file) => {
|
|
console.log(file);
|
|
});
|
|
}
|
|
});
|
|
|
|
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: 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));
|
|
}
|