mirror of
https://github.com/go-gitea/gitea
synced 2025-07-05 10:07:22 +00:00
Backport #34745 by MaxWebZ When adding a link using the "Add a link" button in comment editor, pasting a URL resulted in incorrect Markdown formatting (double brackets) instead of replacing the placeholder text. This fix adds a context check to prevent creating a new markdown link when we're already inside an existing one. Fixes #34740 Co-authored-by: MaxWebZ <5054326@gmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@ -118,17 +118,26 @@ export function removeAttachmentLinksFromMarkdown(text: string, fileUuid: string
|
||||
return text;
|
||||
}
|
||||
|
||||
function handleClipboardText(textarea: HTMLTextAreaElement, e: ClipboardEvent, text: string, isShiftDown: boolean) {
|
||||
export function pasteAsMarkdownLink(textarea: {value: string, selectionStart: number, selectionEnd: number}, pastedText: string): string | null {
|
||||
const {value, selectionStart, selectionEnd} = textarea;
|
||||
const selectedText = value.substring(selectionStart, selectionEnd);
|
||||
const trimmedText = pastedText.trim();
|
||||
const beforeSelection = value.substring(0, selectionStart);
|
||||
const afterSelection = value.substring(selectionEnd);
|
||||
const isInMarkdownLink = beforeSelection.endsWith('](') && afterSelection.startsWith(')');
|
||||
const asMarkdownLink = selectedText && isUrl(trimmedText) && !isUrl(selectedText) && !isInMarkdownLink;
|
||||
return asMarkdownLink ? `[${selectedText}](${trimmedText})` : null;
|
||||
}
|
||||
|
||||
function handleClipboardText(textarea: HTMLTextAreaElement, e: ClipboardEvent, pastedText: string, isShiftDown: boolean) {
|
||||
// pasting with "shift" means "paste as original content" in most applications
|
||||
if (isShiftDown) return; // let the browser handle it
|
||||
|
||||
// when pasting links over selected text, turn it into [text](link)
|
||||
const {value, selectionStart, selectionEnd} = textarea;
|
||||
const selectedText = value.substring(selectionStart, selectionEnd);
|
||||
const trimmedText = text.trim();
|
||||
if (selectedText && isUrl(trimmedText) && !isUrl(selectedText)) {
|
||||
const pastedAsMarkdown = pasteAsMarkdownLink(textarea, pastedText);
|
||||
if (pastedText) {
|
||||
e.preventDefault();
|
||||
replaceTextareaSelection(textarea, `[${selectedText}](${trimmedText})`);
|
||||
replaceTextareaSelection(textarea, pastedAsMarkdown);
|
||||
}
|
||||
// else, let the browser handle it
|
||||
}
|
||||
|
Reference in New Issue
Block a user