@@ -33,7 +33,9 @@ function onPaste(event: ClipboardEvent) {
33
33
// Generate DOM tree from HTML string
34
34
const parser = new DOMParser ( )
35
35
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
+ )
37
39
38
40
const markdown = convertToMarkdown ( plaintext , walker )
39
41
@@ -56,7 +58,9 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
56
58
// Walk through the DOM tree
57
59
while ( currentNode && index < NODE_LIMIT ) {
58
60
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 || ''
60
64
61
65
// No need to transform whitespace
62
66
if ( isEmptyString ( text ) ) {
@@ -69,7 +73,7 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
69
73
70
74
if ( markdownFoundIndex >= 0 ) {
71
75
if ( isLink ( currentNode ) ) {
72
- const markdownLink = linkify ( currentNode )
76
+ const markdownLink = linkify ( currentNode , text )
73
77
// Transform 'example link plus more text' into 'example [link](example link) plus more text'
74
78
// Method: 'example [link](example link) plus more text' = 'example ' + '[link](example link)' + ' plus more text'
75
79
markdown =
@@ -100,8 +104,7 @@ function hasHTML(transfer: DataTransfer): boolean {
100
104
}
101
105
102
106
// 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 {
105
108
const url = element . href || ''
106
109
let markdown = ''
107
110
0 commit comments