Skip to content

Commit fed808f

Browse files
mejo-max-nextcloud
authored andcommitted
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 <[email protected]> Signed-off-by: Max <[email protected]>
1 parent c67150b commit fed808f

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

src/marks/Link.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const Link = TipTapLink.extend({
7272
return ['a', {
7373
...mark.attrs,
7474
href,
75+
'data-text-el': 'text-only-link',
7576
'data-md-href': mark.attrs.href,
7677
rel: 'noopener noreferrer nofollow',
7778
}, 0]

src/plugins/links.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,26 @@ export function linkClicking() {
153153
event.stopImmediatePropagation()
154154
}
155155
},
156-
// Prevent open link (except anchor links) on left click (required for read-only mode)
157-
// Open link in new tab on Ctrl/Cmd + left click
156+
// Prevent open link for text-only links on left click. Required for read-only mode.
158157
click: (view, event) => {
159158
const linkEl = event.target.closest('a')
160-
if (event.button === 0 && linkEl) {
161-
// No special handling in mermaid diagrams to not break links there
162-
if (linkEl.closest('svg[id^="mermaid-view"]')) {
163-
return false
164-
}
159+
// Only text-only links need special handling (e.g. don't handle links inside preview or mermaid diagrams)
160+
if (
161+
!linkEl
162+
|| !linkEl.matches('a[data-text-el="text-only-link"]')
163+
) {
164+
return false
165+
}
165166

167+
if (event.button === 0) {
168+
// Stop browser from opening the link
166169
event.preventDefault()
170+
167171
if (isLinkToSelfWithHash(linkEl.attributes.href?.value)) {
168172
// Open anchor links directly
169173
location.href = linkEl.attributes.href.value
170174
} else if (event.ctrlKey || event.metaKey) {
175+
// Open link in new tab on Ctrl/Cmd + left click
171176
window.open(linkEl.href, '_blank')
172177
}
173178
}

0 commit comments

Comments
 (0)