Unverified Commit bc0bac94 authored by Chris Curry's avatar Chris Curry Committed by GitHub

refactor: enhance hyperlink handling in markdown shortcuts (#4238)

parent 82b5c8ab
......@@ -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;
editor.insertText(`[${selectedContent}](${url})`);
if (url === blankURL) {
const newCursorStart = cursorPosition + selectedContent.length + 3;
editor.setCursorPosition(newCursorStart, newCursorStart + url.length);
if (url === blankURL) {
const newCursorStart = cursorPosition + selectedContent.length + 3;
editor.setCursorPosition(newCursorStart, newCursorStart + url.length);
}
}
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment