feat: add own wiki
This commit is contained in:
1 parent
5f41e47958
commit
49ed2577ac
5 files changed
+106
No files matched your search
+40
@@ -0,0 +1,40 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import matter from 'gray-matter';
|
||||
import { remark } from 'remark';
|
||||
import html from 'remark-html';
|
||||
|
||||
export type WikiArticle = {
|
||||
slug: string;
|
||||
title: string;
|
||||
contentHtml: 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}.md`);
|
||||
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,
|
||||
contentHtml,
|
||||
title: data.title || 'Unbenannt',
|
||||
updatedAt: data.updatedAt || null,
|
||||
...data,
|
||||
} as WikiArticle;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user