Skip to content

Commit 6090af4

Browse files
authored
do not format as markdown when pasting after at mention (#64)
1 parent 95ee1ef commit 6090af4

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/paste-markdown-html.ts

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ function onPaste(event: ClipboardEvent) {
2020
const field = event.currentTarget
2121
if (!(field instanceof HTMLTextAreaElement)) return
2222

23+
if (isWithinUserMention(field)) {
24+
return
25+
}
26+
2327
// Get the plaintext and html version of clipboard contents
2428
let plaintext = transfer.getData('text/plain')
2529
const textHTML = transfer.getData('text/html')
@@ -91,6 +95,16 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
9195
return index === NODE_LIMIT ? plaintext : markdown
9296
}
9397

98+
function isWithinUserMention(textarea: HTMLTextAreaElement): boolean {
99+
const selectionStart = textarea.selectionStart || 0
100+
if (selectionStart === 0) {
101+
return false
102+
}
103+
104+
const previousChar = textarea.value.substring(selectionStart - 1, selectionStart)
105+
return previousChar === '@'
106+
}
107+
94108
function isEmptyString(text: string): boolean {
95109
return !text || text?.trim().length === 0
96110
}

test/test.js

+13
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ describe('paste-markdown', function () {
5353
assert.equal(textarea.value, 'The examples can be found here: https://docs.github.com')
5454
})
5555

56+
it("doesn't paste markdown URL when pasting after user at mentions", function () {
57+
textarea.value = '@'
58+
textarea.setSelectionRange(1, 1)
59+
const html = `
60+
<a href="http://github.localhost/monalisa">monalisa</a>
61+
`
62+
paste(textarea, {'text/plain': 'monalisa', 'text/html': html})
63+
64+
// No change in textarea value here means no custom paste event handler was fired.
65+
// So the browser default paste handler will be used.
66+
assert.equal(textarea.value, '@')
67+
})
68+
5669
it('turns html tables into markdown', function () {
5770
const data = {
5871
'text/html': tableHtml

0 commit comments

Comments
 (0)