refactor: enhance hyperlink handling in markdown shortcuts (#4238)
This commit is contained in:
parent
82b5c8ab07
commit
bc0bac94ed
1 changed files with 14 additions and 5 deletions
|
|
@ -19,13 +19,22 @@ export const hyperlinkHighlightedText = (editor: EditorRefActions, url?: string)
|
||||||
const cursorPosition = editor.getCursorPosition();
|
const cursorPosition = editor.getCursorPosition();
|
||||||
const selectedContent = editor.getSelectedContent();
|
const selectedContent = editor.getSelectedContent();
|
||||||
const blankURL = "url";
|
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) {
|
editor.insertText(`[${selectedContent}](${url})`);
|
||||||
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue