-
Notifications
You must be signed in to change notification settings - Fork 4
Website: Refactor Blog Utils into General Utils #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e6cb64
refactor blog utils into general utils
mdroidian 854ec58
schema
mdroidian 0382fea
make schema super generic for now
mdroidian cbeaeec
Update apps/website/app/utils/getHtmlFromMarkdown.ts
mdroidian c504632
Update apps/website/app/utils/getMarkdownFile.ts
mdroidian 674097a
review feedback (good bot)
mdroidian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 @@ | ||
| export const BLOG_PATH = "app/(home)/blog/posts"; | ||
8 changes: 4 additions & 4 deletions
8
apps/website/app/(home)/blog/schema.tsx → apps/website/app/types/schema.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -1,14 +1,14 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| export const BlogSchema = z.object({ | ||
| export const PageSchema = z.object({ | ||
| title: z.string(), | ||
| published: z.boolean().default(false), | ||
| date: z.string(), | ||
| author: z.string(), | ||
| published: z.boolean().default(false), | ||
| }); | ||
|
|
||
| export type BlogFrontmatter = z.infer<typeof BlogSchema>; | ||
| export type PageFrontmatter = z.infer<typeof PageSchema>; | ||
|
|
||
| export type Blog = BlogFrontmatter & { | ||
| export type PageData = PageFrontmatter & { | ||
| slug: string; | ||
| }; |
This file contains hidden or 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,25 @@ | ||
| import path from "path"; | ||
| import fs from "fs/promises"; | ||
|
|
||
| type Props = { | ||
| filename: string; | ||
| directory: string; | ||
| }; | ||
|
|
||
| export const getFileContent = async ({ | ||
| filename, | ||
| directory, | ||
| }: Props): Promise<string> => { | ||
| try { | ||
| const safeFilename = path.basename(filename); | ||
| const filePath = path.join(directory, safeFilename); | ||
| const fileContent = await fs.readFile(filePath, "utf-8"); | ||
| return fileContent; | ||
| } catch (error) { | ||
| throw error instanceof Error | ||
| ? new Error(`Failed to read file ${filename}: ${error.message}`, { | ||
| cause: error, | ||
| }) | ||
| : new Error(`Failed to read file ${filename}: Unknown error`); | ||
| } | ||
mdroidian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
This file contains hidden or 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,43 @@ | ||
| import { unified } from "unified"; | ||
| import remarkParse from "remark-parse"; | ||
| import remarkRehype from "remark-rehype"; | ||
| import rehypeStringify from "rehype-stringify"; | ||
| import { toString } from "mdast-util-to-string"; | ||
| import { visit } from "unist-util-visit"; | ||
| import type { Root } from "mdast"; | ||
|
|
||
| function remarkHeadingId() { | ||
| return (tree: Root) => { | ||
| visit(tree, "heading", (node) => { | ||
| const text = toString(node); | ||
| const id = text | ||
| .toLowerCase() | ||
| .replace(/[^a-z0-9]+/g, "-") | ||
| .replace(/(^-|-$)/g, ""); | ||
|
|
||
| node.data = { | ||
| hName: `h${node.depth}`, | ||
| hProperties: { id }, | ||
| }; | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| export async function getHtmlFromMarkdown(markdown: string): Promise<string> { | ||
| if (!markdown) { | ||
| throw new Error('Markdown content is required'); | ||
| } | ||
|
|
||
| try { | ||
| const htmlString = await unified() | ||
| .use(remarkParse) | ||
| .use(remarkHeadingId) | ||
| .use(remarkRehype) | ||
| .use(rehypeStringify) | ||
| .process(markdown); | ||
| return htmlString.toString(); | ||
| } catch (error) { | ||
| console.error('Error processing markdown:', error); | ||
| throw new Error('Failed to process markdown content'); | ||
| } | ||
| } |
This file contains hidden or 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,50 @@ | ||
| import { getHtmlFromMarkdown } from "~/utils/getHtmlFromMarkdown"; | ||
| import { getFileContent } from "~/utils/getFileContent"; | ||
| import { notFound } from "next/navigation"; | ||
| import { PageFrontmatter, PageSchema } from "~/types/schema"; | ||
| import matter from "gray-matter"; | ||
|
|
||
| type Props = { | ||
| slug: string; | ||
| directory: string; | ||
| }; | ||
|
|
||
| type ProcessedMarkdownPage = { | ||
| data: PageFrontmatter; | ||
| contentHtml: string; | ||
| }; | ||
|
|
||
| export const getMarkdownPage = async ({ | ||
| slug, | ||
| directory, | ||
| }: Props): Promise<ProcessedMarkdownPage> => { | ||
sid597 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try { | ||
| if (!slug || !directory) { | ||
| throw new Error('Both slug and directory are required'); | ||
| } | ||
|
|
||
| // Prevent directory traversal | ||
| if (slug.includes('..') || directory.includes('..')) { | ||
| throw new Error('Invalid path'); | ||
| } | ||
|
|
||
| const fileContent = await getFileContent({ | ||
| filename: `${slug}.md`, | ||
| directory, | ||
| }); | ||
mdroidian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const { data: rawData, content } = matter(fileContent); | ||
| const data = PageSchema.parse(rawData); | ||
|
|
||
| if (!data.published) { | ||
| console.log(`Post ${slug} is not published`); | ||
| return notFound(); | ||
| } | ||
|
|
||
| const contentHtml = await getHtmlFromMarkdown(content); | ||
|
|
||
| return { data, contentHtml }; | ||
| } catch (error) { | ||
| console.error("Error loading blog post:", error); | ||
| return notFound(); | ||
| } | ||
| }; | ||
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.