Files
psalmtafel-webservice/lib/wiki.ts
T

46 lines
1.2 KiB
TypeScript

import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
import { remark } from 'remark';
import html from 'remark-html';
import rehypePrettyCode from 'rehype-pretty-code';
const options = {
theme: 'github-dark', // oder 'nord', 'one-dark-pro', etc.
keepBackground: true,
};
export type WikiArticle = {
slug: string;
title: string;
rawContent: string;
updatedAt?: string;
};
const wikiDirectory = path.join(process.cwd(), "data/wiki/pages");
export async function getWikiPageBySlug(slug: string): Promise<WikiArticle | null> {
try {
const fullPath = path.join(wikiDirectory, `${slug}.mdx`);
const fileContents = fs.readFileSync(fullPath, "utf-8");
// read metadata
const { data, content } = matter(fileContents);
// convert markdown in HTML
const processedContent = await remark()
.use(html)
.process(content);
const contentHtml = processedContent.toString();
return {
slug,
rawContent: content,
title: data.title || 'Unbenannt',
updatedAt: data.updatedAt || null,
...data,
} as WikiArticle;
} catch (error) {
return null;
}
}