Skip to content

Commit 12b4bb2

Browse files
authored
Merge pull request #1 from imjohnbo/pr/imjohnbo/38
fix: only markdownify if corresponding plaintext can be found
2 parents 04b3dbf + 22a7266 commit 12b4bb2

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/paste-markdown-html.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ type MarkdownTransformer = (element: HTMLElement | HTMLAnchorElement, args: stri
1313
function onPaste(event: ClipboardEvent) {
1414
const transfer = event.clipboardData
1515
// if there is no clipboard data, or
16-
// if there is no html content in the clipboard, or
17-
// if the browser has made an "improved URL for pasting", return
18-
// See https://support.microsoft.com/en-gb/microsoft-edge/improved-copy-and-paste-of-urls-in-microsoft-edge-d3bd3956-603a-0033-1fbc-9588a30645b4 for more
19-
if (!transfer || !hasHTML(transfer) || hasLinkPreview(transfer)) return
16+
// if there is no html content in the clipboard, return
17+
if (!transfer || !hasHTML(transfer)) return
2018

2119
const field = event.currentTarget
2220
if (!(field instanceof HTMLTextAreaElement)) return
@@ -56,8 +54,10 @@ function transform(
5654
for (const element of elements) {
5755
const textContent = element.textContent || ''
5856
const {part, index} = trimAfter(text, textContent)
59-
markdownParts.push(part.replace(textContent, transformer(element, args)))
60-
text = text.slice(index)
57+
if (index >= 0) {
58+
markdownParts.push(part.replace(textContent, transformer(element, args)))
59+
text = text.slice(index)
60+
}
6161
}
6262
markdownParts.push(text)
6363
return markdownParts.join('')
@@ -83,10 +83,6 @@ function hasHTML(transfer: DataTransfer): boolean {
8383
return transfer.types.includes('text/html')
8484
}
8585

86-
function hasLinkPreview(transfer: DataTransfer): boolean {
87-
return transfer.types.includes('text/link-preview')
88-
}
89-
9086
function linkify(element: HTMLAnchorElement): string {
9187
return `[${element.textContent}](${element.href})`
9288
}

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)