From 335b6b6c7aa331a908091478eac657c821b4d384 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sat, 19 Oct 2024 17:29:58 -0700 Subject: [PATCH] refac --- .../components/common/RichTextInput.svelte | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/lib/components/common/RichTextInput.svelte b/src/lib/components/common/RichTextInput.svelte index ce68cf577..f12e50800 100644 --- a/src/lib/components/common/RichTextInput.svelte +++ b/src/lib/components/common/RichTextInput.svelte @@ -70,14 +70,45 @@ // Method to convert markdown content to ProseMirror-compatible document function markdownToProseMirrorDoc(markdown: string) { console.log('Markdown:', markdown); + // Parse the Markdown content into a ProseMirror document let doc = defaultMarkdownParser.parse(markdown || ''); + return doc; } + // Create a custom serializer for paragraphs + // Custom paragraph serializer to preserve newlines for empty paragraphs (empty block). + function serializeParagraph(state, node: Node) { + const content = node.textContent.trim(); + + // If the paragraph is empty, just add an empty line. + if (content === '') { + state.write('\n'); + } else { + state.renderInline(node); + state.closeBlock(node); + } + } + + const customMarkdownSerializer = new defaultMarkdownSerializer.constructor( + { + ...defaultMarkdownSerializer.nodes, + + paragraph: (state, node) => { + serializeParagraph(state, node); // Use custom paragraph serialization + } + + // Customize other block formats if needed + }, + + // Copy marks directly from the original serializer (or customize them if necessary) + defaultMarkdownSerializer.marks + ); + // Utility function to convert ProseMirror content back to markdown text function serializeEditorContent(doc) { - const markdown = defaultMarkdownSerializer.serialize(doc); + const markdown = customMarkdownSerializer.serialize(doc); return unescapeMarkdown(markdown); }