-
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.
feat: mdx playground + height full on layout
- Loading branch information
Showing
5 changed files
with
136 additions
and
71 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 |
---|---|---|
@@ -1,56 +1,55 @@ | ||
"use client"; | ||
|
||
import { MDXProvider } from "@mdx-js/react"; | ||
import { MDXRemote, type MDXRemoteSerializeResult } from "next-mdx-remote"; | ||
import { serialize } from "next-mdx-remote/serialize"; | ||
import { compileMDX } from "next-mdx-remote/rsc"; | ||
import { type ChangeEvent, useState } from "react"; | ||
|
||
const MdxPlayground = () => { | ||
import { defaultMdxComponents } from "@/mdx-components"; | ||
|
||
export default function MDXEditorPage() { | ||
const [mdxContent, setMdxContent] = useState<string>(""); | ||
const [renderedContent, setRenderedContent] = useState<MDXRemoteSerializeResult | null>(null); | ||
const [error, setError] = useState<string | null>(null); | ||
const [renderedContent, setRenderedContent] = useState<React.ReactNode | null>(null); | ||
|
||
const handleMdxChange = async (event: ChangeEvent<HTMLTextAreaElement>) => { | ||
const content = event.target.value; | ||
setMdxContent(content); | ||
setError(null); | ||
const handleRender = async (event: ChangeEvent<HTMLTextAreaElement>) => { | ||
const mdxContent = event.target.value; | ||
setMdxContent(mdxContent); | ||
|
||
try { | ||
setRenderedContent(await serialize(content)); | ||
const { content } = await compileMDX({ | ||
source: mdxContent, | ||
components: defaultMdxComponents, | ||
options: { | ||
parseFrontmatter: false, | ||
}, | ||
}); | ||
|
||
setRenderedContent(content); | ||
} catch (error) { | ||
console.error(error); | ||
console.error("MDX Compilation Error:", error); | ||
|
||
if (error instanceof Error) { | ||
setError(error.toString()); | ||
} | ||
setRenderedContent(null); | ||
return; | ||
setRenderedContent( | ||
<div className="text-red-500"> | ||
<p>Error rendering MDX</p> | ||
{error instanceof Error && <p>{error.toString()}</p>} | ||
</div>, | ||
); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="flex min-h-full col-span-3"> | ||
<textarea | ||
className="w-1/2 h-full p-4 text-lg" | ||
value={mdxContent} | ||
onChange={event => { | ||
handleMdxChange(event).catch(console.error); | ||
}} | ||
placeholder="Enter your MDX content here..." | ||
/> | ||
<div className="w-1/2 h-full p-4 overflow-y-auto bg-gray-100"> | ||
<MDXProvider> | ||
{error ? ( | ||
<p className="text-red-500">{error}</p> | ||
) : renderedContent ? ( | ||
<MDXRemote {...renderedContent} /> | ||
) : ( | ||
<p>Enter MDX content to see the rendered output.</p> | ||
)} | ||
</MDXProvider> | ||
<div className="flex h-full col-span-3"> | ||
<div className="w-1/2 p-4 h-full bg-gray-100"> | ||
<textarea | ||
className="w-full h-full p-2 border rounded" | ||
value={mdxContent} | ||
onChange={event => { | ||
handleRender(event).catch(console.error); | ||
}} | ||
placeholder="Write your MDX here. " | ||
/> | ||
</div> | ||
<div className="w-1/2 p-4 bg-white h-full overflow-x-hidden overflow-y-auto"> | ||
<div className="border p-4">{renderedContent}</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default MdxPlayground; | ||
} |
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,56 @@ | ||
"use client"; | ||
|
||
import { MDXProvider } from "@mdx-js/react"; | ||
import { MDXRemote, type MDXRemoteSerializeResult } from "next-mdx-remote"; | ||
import { serialize } from "next-mdx-remote/serialize"; | ||
import { type ChangeEvent, useState } from "react"; | ||
|
||
const MdxPlayground = () => { | ||
const [mdxContent, setMdxContent] = useState<string>(""); | ||
const [renderedContent, setRenderedContent] = useState<MDXRemoteSerializeResult | null>(null); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
const handleMdxChange = async (event: ChangeEvent<HTMLTextAreaElement>) => { | ||
const content = event.target.value; | ||
setMdxContent(content); | ||
setError(null); | ||
|
||
try { | ||
setRenderedContent(await serialize(content)); | ||
} catch (error) { | ||
console.error(error); | ||
|
||
if (error instanceof Error) { | ||
setError(error.toString()); | ||
} | ||
setRenderedContent(null); | ||
return; | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="flex min-h-full col-span-3"> | ||
<textarea | ||
className="w-1/2 h-full p-4 text-lg" | ||
value={mdxContent} | ||
onChange={event => { | ||
handleMdxChange(event).catch(console.error); | ||
}} | ||
placeholder="Enter your MDX content here..." | ||
/> | ||
<div className="w-1/2 h-full p-4 overflow-y-auto bg-gray-100"> | ||
<MDXProvider> | ||
{error ? ( | ||
<p className="text-red-500">{error}</p> | ||
) : renderedContent ? ( | ||
<MDXRemote {...renderedContent} /> | ||
) : ( | ||
<p>Enter MDX content to see the rendered output.</p> | ||
)} | ||
</MDXProvider> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default MdxPlayground; |
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
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