@@ -118,20 +118,26 @@ export function removeAttachmentLinksFromMarkdown(text: string, fileUuid: string
118118 return text ;
119119}
120120
121- function handleClipboardText ( textarea : HTMLTextAreaElement , e : ClipboardEvent , text : string , isShiftDown : boolean ) {
122- // pasting with "shift" means "paste as original content" in most applications
123- if ( isShiftDown ) return ; // let the browser handle it
124-
125- // when pasting links over selected text, turn it into [text](link)
121+ export function pasteAsMarkdownLink ( textarea : { value : string , selectionStart : number , selectionEnd : number } , pastedText : string ) : string | null {
126122 const { value, selectionStart, selectionEnd} = textarea ;
127123 const selectedText = value . substring ( selectionStart , selectionEnd ) ;
128- const trimmedText = text . trim ( ) ;
124+ const trimmedText = pastedText . trim ( ) ;
129125 const beforeSelection = value . substring ( 0 , selectionStart ) ;
130126 const afterSelection = value . substring ( selectionEnd ) ;
131127 const isInMarkdownLink = beforeSelection . endsWith ( '](' ) && afterSelection . startsWith ( ')' ) ;
132- if ( selectedText && isUrl ( trimmedText ) && ! isUrl ( selectedText ) && ! isInMarkdownLink ) {
128+ const asMarkdownLink = selectedText && isUrl ( trimmedText ) && ! isUrl ( selectedText ) && ! isInMarkdownLink ;
129+ return asMarkdownLink ? `[${ selectedText } ](${ trimmedText } )` : null ;
130+ }
131+
132+ function handleClipboardText ( textarea : HTMLTextAreaElement , e : ClipboardEvent , pastedText : string , isShiftDown : boolean ) {
133+ // pasting with "shift" means "paste as original content" in most applications
134+ if ( isShiftDown ) return ; // let the browser handle it
135+
136+ // when pasting links over selected text, turn it into [text](link)
137+ const pastedAsMarkdown = pasteAsMarkdownLink ( textarea , pastedText ) ;
138+ if ( pastedText ) {
133139 e . preventDefault ( ) ;
134- replaceTextareaSelection ( textarea , `[ ${ selectedText } ]( ${ trimmedText } )` ) ;
140+ replaceTextareaSelection ( textarea , pastedAsMarkdown ) ;
135141 }
136142 // else, let the browser handle it
137143}
0 commit comments