Skip to content

Commit

Permalink
Add resource page functions
Browse files Browse the repository at this point in the history
  • Loading branch information
FoggyMtnDrifter committed Mar 8, 2024
1 parent fb1bdf4 commit e1aa223
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/resourcesPages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import fs from "fs";
import path from "path";

import matter from "gray-matter";
import { processMarkdownAsHTML } from "@/utils/remarkUtils";

const contentDirectory = path.join(process.cwd(), "resources");

export async function checkIfSlugIsValid(slug: string) {
if (!slug || typeof slug !== "string") {
return false;
}

// Check that the slug does not contain any slashes to prevent directory traversal
if (slug.includes("/") || slug.includes("\\")) {
return false;
}

const fullPath = path.join(contentDirectory, `${slug}.md`);

try {
await fs.promises.access(fullPath);
return true;
} catch {
return false;
}
}

export async function getContentData(slug: string) {
const fullPath = path.join(contentDirectory, `${slug}.md`);

const fileContents = await fs.promises.readFile(fullPath, "utf8");

if (!fileContents) {
throw new Error(`Page with slug "${slug}" does not exist.`);
}

const matterResult = matter(fileContents);

const contentHtml = await processMarkdownAsHTML(matterResult.content);

return {
slug,
contentHtml,
...(matterResult.data as { title: string; description: string }),
};
}

0 comments on commit e1aa223

Please sign in to comment.