@@ -33,7 +33,9 @@ function onPaste(event: ClipboardEvent) {
3333 // Generate DOM tree from HTML string
3434 const parser = new DOMParser ( )
3535 const doc = parser . parseFromString ( textHTMLClean , 'text/html' )
36- const walker = doc . createTreeWalker ( doc . body , NodeFilter . SHOW_ELEMENT )
36+ const walker = doc . createTreeWalker ( doc . body , NodeFilter . SHOW_ELEMENT , node =>
37+ node . parentNode && isLink ( node . parentNode ) ? NodeFilter . FILTER_REJECT : NodeFilter . FILTER_ACCEPT
38+ )
3739
3840 const markdown = convertToMarkdown ( plaintext , walker )
3941
@@ -56,7 +58,9 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
5658 // Walk through the DOM tree
5759 while ( currentNode && index < NODE_LIMIT ) {
5860 index ++
59- const text = isLink ( currentNode ) ? currentNode . textContent || '' : ( currentNode . firstChild as Text ) ?. wholeText || ''
61+ const text = isLink ( currentNode )
62+ ? ( currentNode . textContent || '' ) . replace ( / [ \t \n \r ] + / g, ' ' )
63+ : ( currentNode . firstChild as Text ) ?. wholeText || ''
6064
6165 // No need to transform whitespace
6266 if ( isEmptyString ( text ) ) {
@@ -69,7 +73,7 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
6973
7074 if ( markdownFoundIndex >= 0 ) {
7175 if ( isLink ( currentNode ) ) {
72- const markdownLink = linkify ( currentNode )
76+ const markdownLink = linkify ( currentNode , text )
7377 // Transform 'example link plus more text' into 'example [link](example link) plus more text'
7478 // Method: 'example [link](example link) plus more text' = 'example ' + '[link](example link)' + ' plus more text'
7579 markdown =
@@ -100,8 +104,7 @@ function hasHTML(transfer: DataTransfer): boolean {
100104}
101105
102106// Makes markdown link from a link element, avoiding special GitHub links
103- function linkify ( element : HTMLAnchorElement ) : string {
104- const label = element . textContent || ''
107+ function linkify ( element : HTMLAnchorElement , label : string ) : string {
105108 const url = element . href || ''
106109 let markdown = ''
107110
0 commit comments