-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Yusef Almamari
committed
Aug 13, 2024
1 parent
657429a
commit 88dc54b
Showing
2 changed files
with
18 additions
and
18 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
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 |
---|---|---|
@@ -1,23 +1,32 @@ | ||
import { useState, useEffect } from "react"; | ||
import ReactMarkdown from "react-markdown"; | ||
import { TopBar } from "../components/ui/MaterialComponents"; | ||
|
||
export default function MarkdownPage(props) { | ||
const { title, filePath } = props; | ||
export default function MarkdownPage({ title = undefined, filePath }) { | ||
const [content, setContent] = useState(""); | ||
const [firstHeading, setFirstHeading] = useState(""); | ||
|
||
useEffect(() => { | ||
async function fetchContent() { | ||
const response = await fetch(filePath); | ||
const text = await response.text(); | ||
setContent(text); | ||
|
||
const headings = text.split("\n").filter((line) => line.startsWith("#")); | ||
const firstHeadingMatch = headings.find((heading) => heading.startsWith("# ")); | ||
if (firstHeadingMatch && !title) { | ||
setContent(text.replace(firstHeadingMatch, "")); | ||
setFirstHeading(firstHeadingMatch.trim().slice(2)); | ||
} else { | ||
setContent(text); | ||
} | ||
} | ||
fetchContent(); | ||
}, [filePath]); | ||
|
||
return ( | ||
<div className="mx-auto max-w-[50rem] p-4"> | ||
<h1 className="hidden">{title}</h1> {/* For screen readers and tab title */} | ||
<ReactMarkdown>{content}</ReactMarkdown> | ||
<div className="mx-auto max-w-[50rem]"> | ||
<TopBar headline={title || firstHeading} /> | ||
<ReactMarkdown className="p-4">{content}</ReactMarkdown> | ||
</div> | ||
); | ||
} |