From fb8ffd3027f7d5bb8d223a96a7512e9f38c2538c Mon Sep 17 00:00:00 2001 From: Ticruz Date: Wed, 20 Nov 2024 13:38:44 +0100 Subject: [PATCH] Fix link editor (for good) (#477) * fix double link matchin * fix #473 and improved behaviour --- src/app/editor/LinkExtension.ts | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/app/editor/LinkExtension.ts b/src/app/editor/LinkExtension.ts index 5616e097..e8168a9e 100644 --- a/src/app/editor/LinkExtension.ts +++ b/src/app/editor/LinkExtension.ts @@ -23,6 +23,7 @@ export const LinkExtension = Node.create({ name: "inlineLink", group: "inline", inline: true, + inclusive: false, selectable: true, draggable: true, priority: 1000, @@ -65,32 +66,36 @@ export const LinkExtension = Node.create({ return [ new InputRule({ find: text => { + const realText = this.editor.state.doc.textContent const match = last(Array.from(text.matchAll(LINK_REGEX))) - if (match && text.length === match.index + match[0].length + 1) { + const realMatch = last(Array.from(realText.matchAll(LINK_REGEX))) + if (match && realMatch && text.length === match.index + match[0].length + 1) { return { index: match.index!, text: match[0], data: { - url: match[0], + url: match[0].trim(), }, } } return null }, - handler: ({state, range, match}) => { - const {tr} = state + handler: ({range, match, chain}) => { if (match[0]) { - try { - tr.insert(range.from - 1, this.type.create(match.data)) - .delete(tr.mapping.map(range.from - 1), tr.mapping.map(range.to)) - .insertText(last(Array.from(match.input!))) - } catch (e) { - // If the node was already linkified, the above code breaks for whatever reason + const lastChar = last(Array.from(match.input!)) + const chainCommand = chain() + .deleteRange({from: range.from - 1, to: range.from - 1 + match[0].length}) + .insertLink({url: match[0]}) + + if (lastChar === "\n") { + chainCommand.splitBlock() + } else { + chainCommand.insertContent(" ") } - } - tr.scrollIntoView() + chainCommand.run() + } }, }), ]