Skip to content

Commit de3f7fc

Browse files
authored
Merge pull request #5615 from nextcloud/backport/5521/stable29
[stable29] fix(LinkBubble): Treat links to hashes in same page directly
2 parents 8d0c82a + 3170dc7 commit de3f7fc

4 files changed

Lines changed: 13 additions & 4 deletions

File tree

src/helpers/links.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ const parseHref = function(dom) {
6868
return ref
6969
}
7070

71+
const isLinkToSelfWithHash = function(href) {
72+
const locationNoHash = window.location.origin + window.location.pathname + window.location.search
73+
return href?.startsWith('#') || href?.startsWith(locationNoHash + '#')
74+
}
75+
7176
export {
7277
domHref,
7378
parseHref,
79+
isLinkToSelfWithHash,
7480
}

src/nodes/Preview.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222

2323
import { Node, isNodeActive, getNodeType } from '@tiptap/core'
24-
import { domHref, parseHref } from './../helpers/links.js'
24+
import { domHref, parseHref, isLinkToSelfWithHash } from './../helpers/links.js'
2525
import { VueNodeViewRenderer } from '@tiptap/vue-2'
2626

2727
import Preview from './Preview.vue'
@@ -149,7 +149,7 @@ function previewPossible({ selection }) {
149149
return false
150150
}
151151
const href = extractHref($from.parent.firstChild)
152-
if (!href || href.startsWith('#')) {
152+
if (!href || isLinkToSelfWithHash(href)) {
153153
return false
154154
}
155155
return true

src/plugins/linkHelpers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*
2121
*/
2222

23+
import { isLinkToSelfWithHash } from '../helpers/links.js'
24+
2325
/**
2426
* Get the link mark applied at the current selection or next to it.
2527
*
@@ -78,7 +80,7 @@ function linkMark(node) {
7880
return undefined
7981
}
8082
// Don't open link bubble for anchor links
81-
if (linkMark.attrs.href.startsWith('#')) {
83+
if (isLinkToSelfWithHash(linkMark.attrs.href)) {
8284
return undefined
8385
} else {
8486
return linkMark

src/plugins/links.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import { Plugin, PluginKey } from '@tiptap/pm/state'
2424
import LinkBubblePluginView from './LinkBubblePluginView.js'
2525
import { activeLinkFromSelection } from './linkHelpers.js'
26+
import { isLinkToSelfWithHash } from '../helpers/links.js'
2627

2728
// Commands
2829

@@ -167,7 +168,7 @@ export function linkClicking() {
167168
const linkEl = event.target.closest('a')
168169
if (event.button === 0 && linkEl) {
169170
event.preventDefault()
170-
if (linkEl.attributes.href?.value?.startsWith('#')) {
171+
if (isLinkToSelfWithHash(linkEl.attributes.href?.value)) {
171172
// Open anchor links directly
172173
location.href = linkEl.attributes.href.value
173174
} else if (event.ctrlKey || event.metaKey) {

0 commit comments

Comments
 (0)