Created
March 18, 2013 00:49
-
-
Save aprescott/5184336 to your computer and use it in GitHub Desktop.
Take a selection in the page and turn it into a blockquote
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Take a user's selected content in the page, as HTML, and | |
// place it inside a blockquote. | |
// | |
// Allows the possibility of automatically converting it | |
// to Markdown with toMarkdown. | |
// | |
// the selection in the document -- Selection object | |
var sel = document.getSelection(); | |
// the Range object for the selection | |
var range = sel.getRangeAt(0); | |
// the Range object's contents as a DocumentFragment. | |
// | |
// if the selection is <p>foo [bar ba]z</p> then the fragment will | |
// automatically be contained in <p/> without doing anything. | |
var docFragment = range.cloneContents(); | |
// create a <blockquote> to store the document fragment | |
var quote = document.createElement("blockquote"); | |
quote.appendChild(docFragment); | |
// put the <blockquote> in a container to see its contents | |
var container = document.createElement("div"); | |
container.appendChild(quote); | |
container.innerHTML | |
//-> <blockquote><p>bar ba</p></blockquote> | |
// with toMarkdown you can then convert this HTML into Markdown syntax | |
// | |
// https://github.com/domchristie/to-markdown | |
toMarkdown(container.innerHTML) | |
//-> > bar ba | |
// This will also give you <em> and <strong> accordingly. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment