From 924b6508f9211ba281b159676d4800748870b565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20K=C3=BChn?= Date: Fri, 3 Dec 2021 20:54:05 +0100 Subject: [PATCH] fix: calculate correct autolink position when using leaf nodes in the same text block, fix #2231 --- packages/extension-link/src/helpers/autolink.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/extension-link/src/helpers/autolink.ts b/packages/extension-link/src/helpers/autolink.ts index 1474df30..9fdee28e 100644 --- a/packages/extension-link/src/helpers/autolink.ts +++ b/packages/extension-link/src/helpers/autolink.ts @@ -43,8 +43,8 @@ export default function autolink(options: AutolinkOptions): Plugin { } const newMark = newMarks[0] - const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to) - const newLinkText = newState.doc.textBetween(newMark.from, newMark.to) + const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to, undefined, ' ') + const newLinkText = newState.doc.textBetween(newMark.from, newMark.to, undefined, ' ') const wasLink = test(oldLinkText) const isLink = test(newLinkText) @@ -58,7 +58,16 @@ export default function autolink(options: AutolinkOptions): Plugin { // now let’s see if we can add new links findChildrenInRange(newState.doc, newRange, node => node.isTextblock) .forEach(textBlock => { - find(textBlock.node.textContent) + // we need to define a placeholder for leaf nodes + // so that the link position can be calculated correctly + const text = newState.doc.textBetween( + textBlock.pos, + textBlock.pos + textBlock.node.nodeSize, + undefined, + ' ', + ) + + find(text) .filter(link => link.isLink) // calculate link position .map(link => ({