Skip to content

Commit e983e1c

Browse files
feat: /render (#879)
* feat: /render * style: dark mode --------- Co-authored-by: MasterIceZ <64594596+MasterIceZ@users.noreply.github.com>
1 parent 1eef4c1 commit e983e1c

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/app/api/render/route.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { NextRequest } from 'next/server'
2+
3+
import { mdxToHtml } from '@/lib/renderMarkdown'
4+
import { badRequest, json } from '@/utils/apiResponse'
5+
6+
export async function POST(req: NextRequest) {
7+
const body = await req.json()
8+
9+
const { content } = body
10+
11+
if (!('content' in body) || typeof content !== 'string') {
12+
return badRequest()
13+
}
14+
15+
const mdxSource = await mdxToHtml(content)
16+
return json(mdxSource)
17+
}

src/app/render/page.tsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use client'
2+
3+
import { useState } from 'react'
4+
5+
import { MDXRemote, MDXRemoteSerializeResult } from 'next-mdx-remote'
6+
7+
import components from '@/components/common/MDXComponents'
8+
9+
import 'katex/dist/katex.min.css'
10+
11+
export default function Render() {
12+
const [md, setMd] = useState('')
13+
const [loading, setLoading] = useState(false)
14+
15+
const [rendered, setRendered] = useState<MDXRemoteSerializeResult>()
16+
17+
async function render() {
18+
setLoading(true)
19+
const res = await fetch('/api/render', {
20+
method: 'POST',
21+
body: JSON.stringify({ content: md })
22+
})
23+
setRendered(await res.json())
24+
setLoading(false)
25+
}
26+
27+
return (
28+
<div className="flex w-screen flex-col items-center gap-4 py-4">
29+
<div>
30+
<button
31+
className="trasition-colors mt-4 inline rounded-md bg-prog-primary-500 px-9 py-2.5 text-lg font-bold text-white hover:bg-prog-primary-600"
32+
onClick={() => render()}
33+
disabled={loading}
34+
>
35+
Render
36+
</button>
37+
</div>
38+
39+
<div className="flex w-screen items-start justify-between px-8">
40+
<textarea
41+
className="w-[45vw] rounded-md border border-black p-2"
42+
cols={80}
43+
rows={100}
44+
value={md}
45+
onChange={e => setMd(e.currentTarget.value)}
46+
/>
47+
48+
<article className="prose w-[45vw] max-w-none dark:prose-invert dark:prose-headings:text-white">
49+
{loading ? (
50+
<h1>Loading...</h1>
51+
) : (
52+
rendered && <MDXRemote {...rendered} components={components} />
53+
)}
54+
</article>
55+
</div>
56+
</div>
57+
)
58+
}

0 commit comments

Comments
 (0)