Skip to content

Commit cd2af87

Browse files
committed
Validate link before opening
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 42488fa commit cd2af87

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

src/marks/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
*
2121
*/
2222

23+
import { Plugin } from 'tiptap'
24+
import { getMarkAttrs } from 'tiptap-utils'
2325
import { Bold, Italic as TipTapItalic, Strike as TipTapStrike, Link as TipTapLink } from 'tiptap-extensions'
26+
import { markdownit } from '../EditorFactory'
2427

2528
/**
2629
* This file maps prosemirror mark names to tiptap classes,
@@ -100,6 +103,35 @@ class Link extends TipTapLink {
100103
}
101104
}
102105

106+
get plugins() {
107+
if (!this.options.openOnClick) {
108+
return []
109+
}
110+
111+
return [
112+
new Plugin({
113+
props: {
114+
handleClick: (view, pos, event) => {
115+
const { schema } = view.state
116+
const attrs = getMarkAttrs(view.state, schema.marks.link)
117+
118+
if (attrs.href && event.target instanceof HTMLAnchorElement) {
119+
event.stopPropagation()
120+
const htmlHref = event.target.href
121+
122+
if (!markdownit.validateLink(htmlHref)) {
123+
console.error('Invalid link', htmlHref)
124+
return
125+
}
126+
127+
window.open(htmlHref)
128+
}
129+
},
130+
},
131+
}),
132+
]
133+
}
134+
103135
}
104136

105137
/** Strike is currently unsupported by prosemirror-markdown */

0 commit comments

Comments
 (0)