steven-tey / novel

Notion-style WYSIWYG editor with AI-powered autocompletion.
https://novel.sh
Apache License 2.0
13.22k stars 1.08k forks source link

how to render content as article page after edit ? #456

Open thunderwin opened 1 month ago

thunderwin commented 1 month ago

Describe the feature you'd like to request

how to render content as article page after edit ?

Describe the solution you'd like to see

the editing result is the JSON , now I need save it to the DB... my question is how to render it back to web page later and keep the format like i see in the editor?

Additional information

No response

ByeongminLee commented 1 month ago

Here’s the method I used

  1. You can retrieve the JSON as an HTML-formatted string like this https://github.com/steven-tey/novel/discussions/313#discussioncomment-8591476

  2. Render the string obtained above as HTML

    export default function ContentRenderer({ json }: { json: any }) {
    const output = useMemo(() => {
    return generateHTML(json, [
      Bold,
      BulletList,
      Document,
      Heading,
      Link,
      Paragraph,
      ListItem,
      Text,
    ]);
    }, [json]);
    
    return (
    <pre>
        <div dangerouslySetInnerHTML={{__html:output}}></div>
    </pre>
    );
    }
  3. By specifying the following className, you can define the style for the Novel editor

    <div className="tiptap ProseMirror prose prose-lg dark:prose-invert prose-headings:font-title font-default focus:outline-none max-w-full">
    <ContentRenderer json={value} />
    </div>
  4. result 스크린샷 2024-10-27 오후 6 17 47

keyproco commented 3 weeks ago

I think there is a better way to do that by using <EditorContent />, to avoid XSS vulnerabiilites and HTML conversion is done internally.

zeiflonte commented 3 weeks ago

@keyproco Could you elaborate on how to use <EditorContent /> for rendering?

Zain-ul-din commented 2 weeks ago

is there any way to do it on the server-side?