Skip to content

Commit d9b5a1d

Browse files
committed
add safe content parsing
1 parent 76b905e commit d9b5a1d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/components/Editor.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Image from "@tiptap/extension-image";
1212
import { nodePasteRule, type PasteRuleFinder } from "@tiptap/core";
1313
import EditorMenu from "./EditorMenu";
1414
import { useEffect } from "react";
15+
import { toast } from "sonner";
1516

1617
const getExtensions = () => {
1718
const ImageFinder: PasteRuleFinder = /data:image\//g;
@@ -60,9 +61,18 @@ const getExtensions = () => {
6061
const Editor = () => {
6162
const [content, setContent] = useAtom(contentAtom);
6263

64+
const safeParse = (value: string | null) => {
65+
try {
66+
return value ? JSON.parse(value) : "";
67+
} catch (e) {
68+
toast.error(`Failed to parse content due to ${e}`);
69+
return "";
70+
}
71+
};
72+
6373
const editor = useEditor({
6474
extensions: getExtensions(),
65-
content: content ? JSON.parse(content) : "",
75+
content: safeParse(content),
6676
onUpdate: ({ editor }) => {
6777
setContent(JSON.stringify(editor.getJSON()));
6878
},

0 commit comments

Comments
 (0)