@@ -61,6 +61,8 @@ export type {YfmLangOptions};
61
61
62
62
type Autocompletion = Parameters < typeof autocompletion > [ 0 ] ;
63
63
64
+ const linkRegex = / \[ [ \s \S ] * ?] \( [ \s \S ] * ?\) / g;
65
+
64
66
export type CreateCodemirrorParams = {
65
67
doc : EditorViewConfig [ 'doc' ] ;
66
68
placeholder : Parameters < typeof placeholder > [ 0 ] ;
@@ -232,21 +234,27 @@ export function createCodemirror(params: CreateCodemirrorParams) {
232
234
}
233
235
234
236
if ( parseInsertedUrlAsImage ) {
235
- const { imageUrl, title} =
236
- parseInsertedUrlAsImage (
237
- event . clipboardData . getData ( DataTransferType . Text ) ?? '' ,
238
- ) || { } ;
237
+ const linkMatches = currentLine . matchAll ( linkRegex ) ;
238
+ const isInsertedInsideLink = linkMatches . some (
239
+ ( item ) => from >= item . index && from <= item . index + ( item [ 0 ] ?. length ?? 0 ) ,
240
+ ) ;
241
+ if ( ! isInsertedInsideLink ) {
242
+ const { imageUrl, title} =
243
+ parseInsertedUrlAsImage (
244
+ event . clipboardData . getData ( DataTransferType . Text ) ?? '' ,
245
+ ) || { } ;
239
246
240
- if ( imageUrl ) {
241
- event . preventDefault ( ) ;
242
- logger . event ( { event : 'paste-url-as-image' } ) ;
243
- insertImages ( [
244
- {
245
- url : imageUrl ,
246
- alt : title ,
247
- title,
248
- } ,
249
- ] ) ( editor ) ;
247
+ if ( imageUrl ) {
248
+ event . preventDefault ( ) ;
249
+ logger . event ( { event : 'paste-url-as-image' } ) ;
250
+ insertImages ( [
251
+ {
252
+ url : imageUrl ,
253
+ alt : title ,
254
+ title,
255
+ } ,
256
+ ] ) ( editor ) ;
257
+ }
250
258
}
251
259
}
252
260
0 commit comments