Skip to content

Commit

Permalink
Add syntax highlighting to code blocks in post
Browse files Browse the repository at this point in the history
editor form.
  • Loading branch information
thebkht committed Nov 4, 2023
1 parent 0b37c8b commit 8608010
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 7 additions & 0 deletions app/editor/[posturl]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ async function getPostForUser(postUrl: Post['url'], userId: User["id"]) {
url: postUrl,
authorId: userId,
},
include: {
tags: {
include: {
tag: true,
},
}
},
})
}

Expand Down
26 changes: 25 additions & 1 deletion components/editor/post-editor-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ import TextareaAutosize from 'react-textarea-autosize';
import { Icons } from "../icon"
import { useRouter } from "next/navigation"
import Markdown from "markdown-to-jsx";
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { vscDarkPlus, vs, prism, oneLight, oneDark } from 'react-syntax-highlighter/dist/esm/styles/prism';

const components = {
code({className, children,}: { className: string, children: any }) {
let lang = 'text'; // default monospaced text
if (className && className.startsWith('lang-')) {
lang = className.replace('lang-', '');
}
console.log(lang);
return (
<SyntaxHighlighter style={oneDark} language={lang} >
{children}
</SyntaxHighlighter>
)
}
}

export function PostEditorForm(props: { post: any, user: any }) {
const router = useRouter();
Expand Down Expand Up @@ -68,6 +85,7 @@ export function PostEditorForm(props: { post: any, user: any }) {


type PostFormValues = z.infer<typeof postFormSchema>
console.log(props.post)
// This can come from your database or API.
const defaultValues: Partial<PostFormValues> = {
title: props.post?.title,
Expand Down Expand Up @@ -261,7 +279,13 @@ const defaultValues: Partial<PostFormValues> = {
/></TabsContent>
<TabsContent value="preview" className="px-5 pb-5 bg-popover py-4 text-sm rounded-md">
<article className="article__content markdown-body">
<Markdown>{markdownContent}</Markdown>
<Markdown options={{
overrides: {
code: {
component: components.code,
},
},
}}>{markdownContent}</Markdown>
{/* <div dangerouslySetInnerHTML={{ __html: post?.content }} className="markdown-body" /> */}
</article>
</TabsContent>
Expand Down

0 comments on commit 8608010

Please sign in to comment.