@@ -37,7 +37,7 @@ function onPaste(event: ClipboardEvent) {
3737 // Generate DOM tree from HTML string
3838 const parser = new DOMParser ( )
3939 const doc = parser . parseFromString ( textHTMLClean , 'text/html' )
40- const walker = doc . createTreeWalker ( doc . body , NodeFilter . SHOW_ELEMENT , node =>
40+ const walker = doc . createTreeWalker ( doc . body , NodeFilter . SHOW_ALL , node =>
4141 node . parentNode && isLink ( node . parentNode ) ? NodeFilter . FILTER_REJECT : NodeFilter . FILTER_ACCEPT ,
4242 )
4343
@@ -64,15 +64,17 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
6464 index ++
6565 const text = isLink ( currentNode )
6666 ? ( currentNode . textContent || '' ) . replace ( / [ \t \n \r ] + / g, ' ' )
67- : ( currentNode . firstChild as Text ) ?. wholeText || ''
67+ : ( currentNode as Text ) ?. wholeText || ''
68+
69+ // No need to transform whitespace
70+ if ( isEmptyString ( text ) ) {
71+ currentNode = walker . nextNode ( )
72+ continue
73+ }
6874
6975 // update value of markdownIgnoreBeforeIndex with current index if the current node is not a link
7076 if ( ! isLink ( currentNode ) ) {
7177 markdownIgnoreBeforeIndex += text . replace ( / [ \t \n \r ] + / g, ' ' ) . trimStart ( ) . length
72- }
73-
74- // No need to transform whitespace
75- if ( isEmptyString ( text ) ) {
7678 currentNode = walker . nextNode ( )
7779 continue
7880 }
@@ -81,14 +83,11 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
8183 const markdownFoundIndex = markdown . indexOf ( text , markdownIgnoreBeforeIndex )
8284
8385 if ( markdownFoundIndex >= 0 ) {
84- if ( isLink ( currentNode ) ) {
85- const markdownLink = linkify ( currentNode , text )
86- // Transform 'example link plus more text' into 'example [link](example link) plus more text'
87- // Method: 'example [link](example link) plus more text' = 'example ' + '[link](example link)' + ' plus more text'
88- markdown =
89- markdown . slice ( 0 , markdownFoundIndex ) + markdownLink + markdown . slice ( markdownFoundIndex + text . length )
90- markdownIgnoreBeforeIndex = markdownFoundIndex + markdownLink . length
91- }
86+ const markdownLink = linkify ( currentNode , text )
87+ // Transform 'example link plus more text' into 'example [link](example link) plus more text'
88+ // Method: 'example [link](example link) plus more text' = 'example ' + '[link](example link)' + ' plus more text'
89+ markdown = markdown . slice ( 0 , markdownFoundIndex ) + markdownLink + markdown . slice ( markdownFoundIndex + text . length )
90+ markdownIgnoreBeforeIndex = markdownFoundIndex + markdownLink . length
9291 }
9392
9493 currentNode = walker . nextNode ( )
0 commit comments