Skip to content

Commit

Permalink
do not format as markdown when pasting after at mention (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
akenneth authored Jul 27, 2022
1 parent 95ee1ef commit 6090af4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/paste-markdown-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ function onPaste(event: ClipboardEvent) {
const field = event.currentTarget
if (!(field instanceof HTMLTextAreaElement)) return

if (isWithinUserMention(field)) {
return
}

// Get the plaintext and html version of clipboard contents
let plaintext = transfer.getData('text/plain')
const textHTML = transfer.getData('text/html')
Expand Down Expand Up @@ -91,6 +95,16 @@ function convertToMarkdown(plaintext: string, walker: TreeWalker): string {
return index === NODE_LIMIT ? plaintext : markdown
}

function isWithinUserMention(textarea: HTMLTextAreaElement): boolean {
const selectionStart = textarea.selectionStart || 0
if (selectionStart === 0) {
return false
}

const previousChar = textarea.value.substring(selectionStart - 1, selectionStart)
return previousChar === '@'
}

function isEmptyString(text: string): boolean {
return !text || text?.trim().length === 0
}
Expand Down
13 changes: 13 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ describe('paste-markdown', function () {
assert.equal(textarea.value, 'The examples can be found here: https://docs.github.com')
})

it("doesn't paste markdown URL when pasting after user at mentions", function () {
textarea.value = '@'
textarea.setSelectionRange(1, 1)
const html = `
<a href="http://github.localhost/monalisa">monalisa</a>
`
paste(textarea, {'text/plain': 'monalisa', 'text/html': html})

// No change in textarea value here means no custom paste event handler was fired.
// So the browser default paste handler will be used.
assert.equal(textarea.value, '@')
})

it('turns html tables into markdown', function () {
const data = {
'text/html': tableHtml
Expand Down

0 comments on commit 6090af4

Please sign in to comment.