-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb1bdf4
commit e1aa223
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }), | ||
}; | ||
} |