Other / Markdown / Copy Selection To MD

The Copy Selection to Markdown bookmarklet converts the current text selection to Markdown format and copies it to the clipboard. Supports common HTML elements: headings (h1-h6), paragraphs, line breaks, bold/strong, italic/em, code, pre, links with href, images with alt/src, unordered/ordered lists, blockquotes, horizontal rules, and tables. Displays a green notification when copied successfully, or alerts on failure. Useful for quickly copying web content into Markdown editors.

Run Copy Selection To MD

Test run or drag to
bookmarklets bar to install

javascript:%21function()%7Bvar%20e%3Dwindow.getSelection()%3Bif(e.toString())%7Bvar%20a%3De.getRangeAt(0)%2Ct%3Ddocument.createElement(%22div%22)%3Bt.appendChild(a.cloneContents())%3Bvar%20n%3Dfunction%20e(a)%7Bif(3%3D%3D%3Da.nodeType)return%20a.textContent%3Bif(1%21%3D%3Da.nodeType)return%22%22%3Bfor(var%20t%3D%22%22%2Cn%3Da.tagName.toLowerCase()%2Cr%3D%22%22%2Co%3D0%3Bo%3Ca.childNodes.length%3Bo%2B%2B)r%2B%3De(a.childNodes%5Bo%5D)%3Bswitch(n)%7Bcase%22p%22%3At%3Dr%2B%22%5Cn%5Cn%22%3Bbreak%3Bcase%22br%22%3At%3D%22%5Cn%22%3Bbreak%3Bcase%22h1%22%3At%3D%22%23%20%22%2Br%2B%22%5Cn%5Cn%22%3Bbreak%3Bcase%22h2%22%3At%3D%22%23%23%20%22%2Br%2B%22%5Cn%5Cn%22%3Bbreak%3Bcase%22h3%22%3At%3D%22%23%23%23%20%22%2Br%2B%22%5Cn%5Cn%22%3Bbreak%3Bcase%22h4%22%3At%3D%22%23%23%23%23%20%22%2Br%2B%22%5Cn%5Cn%22%3Bbreak%3Bcase%22h5%22%3At%3D%22%23%23%23%23%23%20%22%2Br%2B%22%5Cn%5Cn%22%3Bbreak%3Bcase%22h6%22%3At%3D%22%23%23%23%23%23%23%20%22%2Br%2B%22%5Cn%5Cn%22%3Bbreak%3Bcase%22strong%22%3Acase%22b%22%3At%3D%22**%22%2Br%2B%22**%22%3Bbreak%3Bcase%22em%22%3Acase%22i%22%3At%3D%22*%22%2Br%2B%22*%22%3Bbreak%3Bcase%22code%22%3At%3D%22%60%22%2Br%2B%22%60%22%3Bbreak%3Bcase%22pre%22%3At%3D%22%60%60%60%5Cn%22%2Br%2B%22%5Cn%60%60%60%5Cn%5Cn%22%3Bbreak%3Bcase%22a%22%3At%3D%22%5B%22%2Br%2B%22%5D(%22%2B(a.getAttribute(%22href%22)%7C%7C%22%22)%2B%22)%22%3Bbreak%3Bcase%22img%22%3Avar%20c%3Da.getAttribute(%22src%22)%3Bt%3D%22%21%5B%22%2B(a.getAttribute(%22alt%22)%7C%7C%22%22)%2B%22%5D(%22%2B(c%7C%7C%22%22)%2B%22)%22%3Bbreak%3Bcase%22ul%22%3Acase%22ol%22%3Acase%22table%22%3At%3Dr%2B%22%5Cn%22%3Bbreak%3Bcase%22li%22%3Avar%20i%3Da.parentNode%3Bif(i%26%26%22ol%22%3D%3D%3Di.tagName.toLowerCase())t%3DArray.from(i.children).indexOf(a)%2B1%2B%22.%20%22%2Br%2B%22%5Cn%22%3Belse%20t%3D%22-%20%22%2Br%2B%22%5Cn%22%3Bbreak%3Bcase%22blockquote%22%3At%3Dr.split(%22%5Cn%22).map(e%3D%3E%22%3E%20%22%2Be).join(%22%5Cn%22)%2B%22%5Cn%5Cn%22%3Bbreak%3Bcase%22hr%22%3At%3D%22---%5Cn%5Cn%22%3Bbreak%3Bcase%22thead%22%3Acase%22tbody%22%3Adefault%3At%3Dr%3Bbreak%3Bcase%22tr%22%3Aif(t%3D%22%7C%22%2Br%2B%22%5Cn%22%2C%22thead%22%3D%3D%3Da.parentNode.tagName.toLowerCase())%7Bvar%20s%3Da.querySelectorAll(%22th%2C%20td%22).length%3Bt%2B%3D%22%7C%22%2BArray(s).fill(%22---%22).join(%22%7C%22)%2B%22%7C%5Cn%22%7Dbreak%3Bcase%22th%22%3Acase%22td%22%3At%3D%22%20%22%2Br.trim()%2B%22%20%7C%22%7Dreturn%20t%7D(t).trim()%3Bnavigator.clipboard.writeText(n).then(function()%7Bvar%20e%3Ddocument.createElement(%22div%22)%3Be.textContent%3D%22Markdown%20copied%20to%20clipboard%21%22%2Ce.style.cssText%3D%22position%3Afixed%3Btop%3A20px%3Bright%3A20px%3Bbackground%3A%234CAF50%3Bcolor%3Awhite%3Bpadding%3A10px%2020px%3Bborder-radius%3A5px%3Bz-index%3A10000%3Bfont-family%3Asans-serif%3B%22%2Cdocument.body.appendChild(e)%2CsetTimeout(function()%7Be.remove()%7D%2C2e3)%7D).catch(function(e)%7Balert(%22Failed%20to%20copy%20to%20clipboard%3A%20%22%2Be)%7D)%7Delse%20alert(%22Please%20select%20some%20text%20first%21%22)%7D()%3B
← Back to all bookmarklets