Skip to content

Commit 9c7ce97

Browse files
committed
feat: editor block to html
1 parent b4b035b commit 9c7ce97

File tree

4 files changed

+318
-7
lines changed

4 files changed

+318
-7
lines changed

apps/web/@/molecules/editor-js/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const Editor: React.FC<EditorJSProps> = ({ onChange, data }) => {
2626
placeholder: "Write your post here...",
2727
onChange: async () => {
2828
const content = await editorRef.current?.save()
29+
2930
onChange(content)
3031
},
3132
})

apps/web/@/molecules/posts/post-detail/post-content/index.tsx

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
"use client"
2-
3-
import React from "react"
1+
import React, { useMemo } from "react"
42

3+
import edjsHTML from "editorjs-html"
54
import htmlReactParser, { attributesToProps, domToReact } from "html-react-parser"
5+
import DOMPurify from "isomorphic-dompurify"
66
import slugify from "slugify"
77

88
import { TPostItem } from "@/types/posts"
99

10+
const edjsParser = edjsHTML()
11+
1012
interface PostContentProps {
1113
post: TPostItem
1214
}
@@ -78,7 +80,30 @@ const HTMLParser: React.FC<PostContentProps> = ({ post }) => {
7880
}
7981

8082
const PostContent: React.FC<PostContentProps> = ({ post }) => {
81-
return <div />
83+
const contentHtml = useMemo(() => {
84+
if (!post.content) return ""
85+
86+
console.log(">>>>>>", post.content)
87+
88+
try {
89+
const content = JSON.parse(post.content)
90+
const htmlArray = edjsParser.parse(content)
91+
console.log(">>>>>>", htmlArray)
92+
const html = htmlArray.join("")
93+
return DOMPurify.sanitize(html)
94+
} catch (error) {
95+
console.log(">>>>>>", error)
96+
97+
return ""
98+
}
99+
}, [post.content])
100+
101+
return (
102+
<div
103+
className="post-content mt-8"
104+
dangerouslySetInnerHTML={{ __html: contentHtml }}
105+
/>
106+
)
82107
}
83108

84109
export default PostContent

apps/web/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
"clsx": "^2.0.0",
2222
"cmdk": "^0.2.1",
2323
"dayjs": "^1.11.9",
24+
"dompurify": "^3.1.6",
25+
"editorjs-html": "^3.4.3",
2426
"html-react-parser": "^5.0.11",
27+
"isomorphic-dompurify": "^2.14.0",
2528
"lowlight": "^3.1.0",
2629
"lucide-react": "^0.270.0",
2730
"negotiator": "^0.6.3",

0 commit comments

Comments
 (0)