Hey is a decentralized and permissionless social media app built with Lens Protocol 馃尶
at main 33 lines 978 B view raw
1import type { Editor } from "prosekit/core"; 2import { htmlFromNode, nodeFromHTML } from "prosekit/core"; 3import { ListDOMSerializer } from "prosekit/extensions/list"; 4import type { EditorExtension } from "@/helpers/prosekit/extension"; 5import { 6 htmlFromMarkdown, 7 markdownFromHTML 8} from "@/helpers/prosekit/markdown"; 9 10export const getMarkdownContent = (editor: Editor<EditorExtension>): string => { 11 if (!editor.mounted) { 12 return ""; 13 } 14 15 const { doc } = editor.view.state; 16 const html = htmlFromNode(doc, { DOMSerializer: ListDOMSerializer }); 17 return markdownFromHTML(html); 18}; 19 20export const setMarkdownContent = ( 21 editor: Editor<EditorExtension>, 22 markdown: string 23): void => { 24 if (!editor.mounted) { 25 return; 26 } 27 28 const html = htmlFromMarkdown(markdown); 29 const { view } = editor; 30 const { state } = view; 31 const doc = nodeFromHTML(html, { schema: state.schema }); 32 view.dispatch(state.tr.replaceWith(0, state.doc.content.size, doc.content)); 33};