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 @@ -73,6 +73,7 @@ const Link = TipTapLink.extend({
{
...mark.attrs,
href,
'data-text-el': 'text-only-link',
'data-md-href': mark.attrs.href,
rel: 'noopener noreferrer nofollow',
},
Expand Down
19 changes: 12 additions & 7 deletions src/plugins/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,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"]')
) {
return false
}

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

View check run for this annotation

Codecov / codecov/patch

src/plugins/links.js#L167-L173

Added lines #L167 - L173 were not covered by tests

if (event.button === 0) {
// Stop browser from opening the link

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

View check run for this annotation

Codecov / codecov/patch

src/plugins/links.js#L175-L176

Added lines #L175 - L176 were not covered by tests
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

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

View check run for this annotation

Codecov / codecov/patch

src/plugins/links.js#L183

Added line #L183 was not covered by tests
window.open(linkEl.href, '_blank')
}
}
Expand Down
Loading