@@ -37,7 +37,7 @@ function onPaste(event: ClipboardEvent) {
37
37
// Generate DOM tree from HTML string
38
38
const parser = new DOMParser ( )
39
39
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 =>
41
41
node . parentNode && isLink ( node . parentNode ) ? NodeFilter . FILTER_REJECT : NodeFilter . FILTER_ACCEPT ,
42
42
)
43
43
@@ -64,15 +64,17 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
64
64
index ++
65
65
const text = isLink ( currentNode )
66
66
? ( 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
+ }
68
74
69
75
// update value of markdownIgnoreBeforeIndex with current index if the current node is not a link
70
76
if ( ! isLink ( currentNode ) ) {
71
77
markdownIgnoreBeforeIndex += text . replace ( / [ \t \n \r ] + / g, ' ' ) . trimStart ( ) . length
72
- }
73
-
74
- // No need to transform whitespace
75
- if ( isEmptyString ( text ) ) {
76
78
currentNode = walker . nextNode ( )
77
79
continue
78
80
}
@@ -81,14 +83,11 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
81
83
const markdownFoundIndex = markdown . indexOf ( text , markdownIgnoreBeforeIndex )
82
84
83
85
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
92
91
}
93
92
94
93
currentNode = walker . nextNode ( )
0 commit comments