From 5b6f2119006eb6076af3a87ab23644758399370a Mon Sep 17 00:00:00 2001 From: fantasticit Date: Fri, 3 Jun 2022 14:17:14 +0800 Subject: [PATCH] tiptap: fix transform title --- packages/client/src/tiptap/core/extensions/title.tsx | 2 +- .../src/tiptap/markdown/prosemirror-to-markdown/helpers.ts | 4 +++- .../src/tiptap/markdown/prosemirror-to-markdown/index.ts | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/client/src/tiptap/core/extensions/title.tsx b/packages/client/src/tiptap/core/extensions/title.tsx index c0712d5..386764b 100644 --- a/packages/client/src/tiptap/core/extensions/title.tsx +++ b/packages/client/src/tiptap/core/extensions/title.tsx @@ -74,7 +74,7 @@ export const Title = Node.create({ const $head = state.selection.$head; const titleNode = $head.node($head.depth); - const endPos = titleNode.firstChild.nodeSize + 1; + const endPos = ((titleNode.firstChild && titleNode.firstChild.nodeSize) || 0) + 1; dispatch(state.tr.insert(endPos, paragraph.create())); diff --git a/packages/client/src/tiptap/markdown/prosemirror-to-markdown/helpers.ts b/packages/client/src/tiptap/markdown/prosemirror-to-markdown/helpers.ts index c40548d..7b2d7d1 100644 --- a/packages/client/src/tiptap/markdown/prosemirror-to-markdown/helpers.ts +++ b/packages/client/src/tiptap/markdown/prosemirror-to-markdown/helpers.ts @@ -132,7 +132,9 @@ export function openTag(tagName, attrs) { str += Object.entries(attrs || {}) .map(([key, value]) => { if ((ignoreAttrs[tagName] || []).includes(key) || defaultAttrs[tagName]?.[key] === value) return ''; - + if (!['class', 'id'].includes(key) && !key.startsWith('data-')) { + key = `data-${key}`; + } return ` ${key}="${htmlEncode(value?.toString())}"`; }) .join(''); diff --git a/packages/client/src/tiptap/markdown/prosemirror-to-markdown/index.ts b/packages/client/src/tiptap/markdown/prosemirror-to-markdown/index.ts index 3232c27..06b719a 100644 --- a/packages/client/src/tiptap/markdown/prosemirror-to-markdown/index.ts +++ b/packages/client/src/tiptap/markdown/prosemirror-to-markdown/index.ts @@ -158,7 +158,7 @@ const SerializerConfig = { state.renderList(node, ' ', () => (node.attrs.bullet || '*') + ' '); }, [Text.name]: defaultMarkdownSerializer.nodes.text, - [Title.name]: renderCustomContainer('title'), + [Title.name]: renderHTMLNode('div', false, true, { class: 'title' }), }, };