Skip to content

Commit c00138b

Browse files
authored
Merge pull request #38 from imjohnbo/imjohnbo/fix-edge-link-paste
Fix Microsoft Edge link paste
2 parents 381d9d5 + 9f542de commit c00138b

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/paste-markdown-html.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type MarkdownTransformer = (element: HTMLElement | HTMLAnchorElement, args: stri
1212

1313
function onPaste(event: ClipboardEvent) {
1414
const transfer = event.clipboardData
15+
// if there is no clipboard data, or
16+
// if there is no html content in the clipboard, return
1517
if (!transfer || !hasHTML(transfer)) return
1618

1719
const field = event.currentTarget
@@ -52,8 +54,10 @@ function transform(
5254
for (const element of elements) {
5355
const textContent = element.textContent || ''
5456
const {part, index} = trimAfter(text, textContent)
55-
markdownParts.push(part.replace(textContent, transformer(element, args)))
56-
text = text.slice(index)
57+
if (index >= 0) {
58+
markdownParts.push(part.replace(textContent, transformer(element, args)))
59+
text = text.slice(index)
60+
}
5761
}
5862
markdownParts.push(text)
5963
return markdownParts.join('')

test/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,23 @@ describe('paste-markdown', function () {
147147
paste(textarea, {'text/html': sentence, 'text/plain': plaintextSentence})
148148
assert.equal(textarea.value, markdownSentence)
149149
})
150+
151+
it("doesn't render any markdown for html link without corresponding plaintext", function () {
152+
// eslint-disable-next-line github/unescaped-html-literal
153+
const link = `<meta charset='utf-8'><a href="https://github.com/monalisa/playground/issues/1">
154+
Link pasting · Issue #1 · monalisa/playground (github.com)</a>`
155+
const plaintextLink = 'https://github.com/monalisa/playground/issues/1'
156+
const linkPreviewLink = {
157+
domain: 'github.com',
158+
preferred_format: 'text/html;content=titled-hyperlink',
159+
title: 'Link pasting · Issue #1 · monalisa/playground (github.com)',
160+
type: 'website',
161+
url: 'https://github.com/monalisa/playground/issues/1'
162+
}
163+
164+
paste(textarea, {'text/html': link, 'text/plain': plaintextLink, 'text/link-preview': linkPreviewLink})
165+
assert.equal(textarea.value, '')
166+
})
150167
})
151168
})
152169

0 commit comments

Comments
 (0)