Skip to content

Commit

Permalink
fix(LinkBubble): Treat links to hashes in same page directly
Browse files Browse the repository at this point in the history
Fixes: #5495

Signed-off-by: Jonas <jonas@freesources.org>
  • Loading branch information
mejo- committed Mar 18, 2024
1 parent 08ccd25 commit 7d137e6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/extensions/LinkBubblePluginView.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { VueRenderer } from '@tiptap/vue-2'
import tippy from 'tippy.js'
import debounce from 'debounce'
import { domHref } from '../helpers/links.js'
import { domHref, isLinkToSelfWithHash } from '../helpers/links.js'
import LinkBubbleView from '../components/Link/LinkBubbleView.vue'

import { getViewerVue } from '../ViewerVue.js'
Expand Down Expand Up @@ -220,7 +220,7 @@ class LinkBubblePluginView {
}

// Don't open link bubble for anchor links
if (linkMark.attrs.href.startsWith('#')) {
if (isLinkToSelfWithHash(linkMark.attrs.href)) {
return false
}

Expand Down
6 changes: 6 additions & 0 deletions src/helpers/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ const parseHref = function(dom) {
return ref
}

const isLinkToSelfWithHash = function(href) {
const locationNoHash = window.location.origin + window.location.pathname + window.location.search
return href?.startsWith('#') || href?.startsWith(locationNoHash + '#')
}

export {
domHref,
parseHref,
isLinkToSelfWithHash,
}
4 changes: 2 additions & 2 deletions src/nodes/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/

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

import Preview from './Preview.vue'
Expand Down Expand Up @@ -149,7 +149,7 @@ function previewPossible({ selection }) {
return false
}
const href = extractHref($from.nodeAfter)
if (!href || href.startsWith('#')) {
if (!href || isLinkToSelfWithHash('#')) {
return false
}
return true
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

import { Plugin, PluginKey } from '@tiptap/pm/state'
import { isLinkToSelfWithHash } from '../helpers/links.js'

export function linkClicking() {
return new Plugin({
Expand Down Expand Up @@ -53,7 +54,8 @@ export function linkClicking() {
const linkEl = event.target.closest('a')
if (event.button === 0 && linkEl) {
event.preventDefault()
if (linkEl.attributes.href?.value?.startsWith('#')) {
const href = linkEl.attributes.href?.value

Check failure on line 57 in src/plugins/links.js

View workflow job for this annotation

GitHub Actions / NPM lint

'href' is assigned a value but never used
if (isLinkToSelfWithHash(linkEl.attributes.href?.value)) {
// Open anchor links directly
location.href = linkEl.attributes.href.value
} else if (event.ctrlKey || event.metaKey) {
Expand Down

0 comments on commit 7d137e6

Please sign in to comment.