Skip to content

Commit 2fddf0a

Browse files
authored
fix(markup): fix pasting image urls to link lines (#721)
1 parent 00335f0 commit 2fddf0a

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/markup/codemirror/create.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export type {YfmLangOptions};
6161

6262
type Autocompletion = Parameters<typeof autocompletion>[0];
6363

64+
const linkRegex = /\[[\s\S]*?]\([\s\S]*?\)/g;
65+
6466
export type CreateCodemirrorParams = {
6567
doc: EditorViewConfig['doc'];
6668
placeholder: Parameters<typeof placeholder>[0];
@@ -232,21 +234,27 @@ export function createCodemirror(params: CreateCodemirrorParams) {
232234
}
233235

234236
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+
) || {};
239246

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+
}
250258
}
251259
}
252260

0 commit comments

Comments
 (0)