Skip to content
Please note that GitHub no longer supports your web browser.

We recommend upgrading to the latest Google Chrome or Firefox.

Learn more
Install a shortcut to append selected text to a <textarea> as a Markdown quote.
JavaScript
Branch: master
Clone or download
Cannot retrieve the latest commit at this time.
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
test
.eslintrc.json quoteSelection Jul 6, 2018
.flowconfig quoteSelection Jul 6, 2018
.gitignore quoteSelection Jul 6, 2018
.travis.yml quoteSelection Jul 6, 2018
LICENSE quoteSelection Jul 6, 2018
README.md Bring back copy Markdown to clipboard functionality Apr 16, 2019
markdown-parsing.js Remove GitHub-specific syntax, fire `quote-selection-markdown` event Apr 11, 2019
package-lock.json 0.8.1 Aug 23, 2019
package.json 0.8.1 Aug 23, 2019
prettier.config.js
quote-selection.js Bring back copy Markdown to clipboard functionality Apr 16, 2019
quote-selection.js.flow Bring back copy Markdown to clipboard functionality Apr 16, 2019
rollup.config.js Update rollup and babel toolchain Feb 13, 2019

README.md

Quote selection

Install a keyboard shortcut r to append selected text to a <textarea> as a Markdown quote.

Installation

$ npm install @github/quote-selection

Usage

<div class="my-quote-region">
  <p>Text to quote</p>
  <textarea></textarea>
</div>
import {install} from '@github/quote-selection'

install(document.querySelector('.my-quote-region'))

This sets up a keyboard event handler so that selecting any text within .my-quote-region and pressing r appends the quoted representation of the selected text into the first applicable <textarea> element.

Preserving Markdown syntax

install(element, {
  quoteMarkdown: true,
  copyMarkdown: false,
  scopeSelector: '.comment-body'
})

The optional scopeSelector parameter ensures that even if the user selection bleeds outside of the scoped element, the quoted portion will always be contained inside the scope. This is useful to avoid accidentally quoting parts of the UI that might be interspersed between quotable content.

In copyMarkdown: true mode, the browser clipboard copy action is intercepted for user selections within element and the Markdown representation of the content is placed on clipboard under the text/x-gfm MIME-type.

Events

  • quote-selection-markdown (bubbles: true, cancelable: false) - fired on the quote region to optionally inject custom syntax into the fragment element in quoteMarkdown: true mode
  • quote-selection (bubbles: true, cancelable: true) - fired on the quote region before text is appended to a textarea

For example, reveal a textarea so it can be found:

region.addEventListener('quote-selection', function(event) {
  const {selection, selectionText} = event.detail
  console.log('Quoted text', selection, selectionText)

  const textarea = event.target.querySelector('textarea')
  textarea.hidden = false

  // Cancel the quote behavior.
  // event.preventDefault()
})

Development

npm install
npm test

License

Distributed under the MIT license. See LICENSE for details.

You can’t perform that action at this time.