Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/marks/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
19 changes: 12 additions & 7 deletions src/plugins/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,26 @@
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"]')

Check warning on line 162 in src/plugins/links.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/links.js#L161-L162

Added lines #L161 - L162 were not covered by tests
) {
return false

Check warning on line 164 in src/plugins/links.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/links.js#L164

Added line #L164 was not covered by tests
}

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')
}
}
Expand Down
Loading