@@ -38,8 +38,8 @@ function onPaste(event: ClipboardEvent) {
38
38
// Get the plaintext and html version of clipboard contents
39
39
let plaintext = transfer . getData ( 'text/plain' )
40
40
const textHTML = transfer . getData ( 'text/html' )
41
- // Replace Unicode equivalent of " " with a space
42
- const textHTMLClean = textHTML . replace ( / \u00A0 / g , ' ' ) . replace ( / \uC2A0 / g , ' ' )
41
+
42
+ const textHTMLClean = normalizeHtmlWhitespace ( textHTML )
43
43
if ( ! textHTML ) return
44
44
45
45
plaintext = plaintext . trim ( )
@@ -161,13 +161,20 @@ function hasHTML(transfer: DataTransfer): boolean {
161
161
return transfer . types . includes ( 'text/html' )
162
162
}
163
163
164
- function stripLineBreaks ( text : string ) : string {
165
- return text . replace ( / [ \t \n \r ] + / g, ' ' )
164
+ /** Collapse whitespace in HTML to normalize it with the plain-text representation. Also convert nbsp into regular space. */
165
+ function normalizeHtmlWhitespace ( text : string ) : string {
166
+ // Collapse regular whitespace characters but preserve non-breaking spaces without collapsing
167
+ return text
168
+ . replace ( / [ \t \n \r ] + / g, ' ' )
169
+ . trim ( )
170
+ . replace ( / [ \u00A0 \uC2A0 ] / g, ' ' )
166
171
}
167
172
168
173
// Makes markdown link from a link element, avoiding special GitHub links
169
174
function linkify ( element : HTMLAnchorElement ) : string {
170
- const label = stripLineBreaks ( element . textContent ?? '' )
175
+ // Inline tags can have hard linebreaks in HTML, but not in Markdown, so we must collapse them to one line
176
+ const label = element . textContent ?? ''
177
+
171
178
const url = element . href || ''
172
179
let markdown = ''
173
180
@@ -186,8 +193,7 @@ function linkify(element: HTMLAnchorElement): string {
186
193
}
187
194
188
195
function simpleInlineTag ( element : Node , markdownBuilder : ( text : string ) => string ) : string {
189
- const text = stripLineBreaks ( element . textContent ?? '' )
190
- return markdownBuilder ( text )
196
+ return markdownBuilder ( element . textContent ?? '' )
191
197
}
192
198
193
199
// Special GitHub links have either a hover card or certain class name
0 commit comments