From fed808f4e891aa6e2a86a063f8466246cbe586b0 Mon Sep 17 00:00:00 2001 From: Jonas Date: Wed, 2 Jul 2025 17:50:57 +0200 Subject: [PATCH] fix(links): Use custom link handling only for text-only links All other links inside the editor (like previews via reference widgets or links in mermaid diagrams) should be treated the standard way. Fixes: #7384 Signed-off-by: Jonas Signed-off-by: Max --- src/marks/Link.js | 1 + src/plugins/links.js | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/marks/Link.js b/src/marks/Link.js index 1878f43958b..31b43bd566e 100644 --- a/src/marks/Link.js +++ b/src/marks/Link.js @@ -72,6 +72,7 @@ const Link = TipTapLink.extend({ return ['a', { ...mark.attrs, href, + 'data-text-el': 'text-only-link', 'data-md-href': mark.attrs.href, rel: 'noopener noreferrer nofollow', }, 0] diff --git a/src/plugins/links.js b/src/plugins/links.js index 293858bbe94..521fb4e3041 100644 --- a/src/plugins/links.js +++ b/src/plugins/links.js @@ -153,21 +153,26 @@ export function linkClicking() { event.stopImmediatePropagation() } }, - // Prevent open link (except anchor links) on left click (required for read-only mode) - // Open link in new tab on Ctrl/Cmd + left click + // Prevent open link for text-only links on left click. Required for read-only mode. click: (view, event) => { const linkEl = event.target.closest('a') - if (event.button === 0 && linkEl) { - // No special handling in mermaid diagrams to not break links there - if (linkEl.closest('svg[id^="mermaid-view"]')) { - return false - } + // Only text-only links need special handling (e.g. don't handle links inside preview or mermaid diagrams) + if ( + !linkEl + || !linkEl.matches('a[data-text-el="text-only-link"]') + ) { + return false + } + if (event.button === 0) { + // Stop browser from opening the link event.preventDefault() + if (isLinkToSelfWithHash(linkEl.attributes.href?.value)) { // Open anchor links directly location.href = linkEl.attributes.href.value } else if (event.ctrlKey || event.metaKey) { + // Open link in new tab on Ctrl/Cmd + left click window.open(linkEl.href, '_blank') } }