From bc0bac94ed70f818ffa4a16ad537571e7d8b5d31 Mon Sep 17 00:00:00 2001 From: Chris Curry Date: Mon, 30 Dec 2024 17:46:24 +0800 Subject: [PATCH] refactor: enhance hyperlink handling in markdown shortcuts (#4238) --- web/src/components/MemoEditor/handlers.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/web/src/components/MemoEditor/handlers.ts b/web/src/components/MemoEditor/handlers.ts index 6988fad2f..2e0c2bb20 100644 --- a/web/src/components/MemoEditor/handlers.ts +++ b/web/src/components/MemoEditor/handlers.ts @@ -19,13 +19,22 @@ export const hyperlinkHighlightedText = (editor: EditorRefActions, url?: string) const cursorPosition = editor.getCursorPosition(); const selectedContent = editor.getSelectedContent(); const blankURL = "url"; - url = url ?? blankURL; - editor.insertText(`[${selectedContent}](${url})`); + // If the selected content looks like a URL and no URL is provided, + // create a link with empty text and the URL + const urlRegex = /^(https?:\/\/[^\s]+)$/; + if (!url && urlRegex.test(selectedContent.trim())) { + editor.insertText(`[](${selectedContent})`); + editor.setCursorPosition(cursorPosition + 1, cursorPosition + 1); + } else { + url = url ?? blankURL; - if (url === blankURL) { - const newCursorStart = cursorPosition + selectedContent.length + 3; - editor.setCursorPosition(newCursorStart, newCursorStart + url.length); + editor.insertText(`[${selectedContent}](${url})`); + + if (url === blankURL) { + const newCursorStart = cursorPosition + selectedContent.length + 3; + editor.setCursorPosition(newCursorStart, newCursorStart + url.length); + } } };