Skip to content

Showdown extension that adds a button above code blocks to quickly and easily copy code to the clipboard.

License

Notifications You must be signed in to change notification settings

piotrpdev/showdown-copy-code

Repository files navigation

showdown-copy-code

Version

Showdown extension that adds a button above code blocks to quickly and easily copy code to the clipboard.

Yoinked Heavily inspired by this project (go check out the creator).

How do I install it?

npm install showdown-copy-code

Usage

Yoinked Heavily inspired by the showdown-highlight example.

const showdown = require("showdown");
const showdownHighlight = require("showdown-highlight");
const { showdownCopyCode } = require("showdown-copy-code");

let converter = new showdown.Converter({
  extensions: [showdownHighlight, showdownCopyCode],
});

const html = converter.makeHtml(`
## Highlighting Code with Showdown

Below we have a piece of JavaScript code:

<br>

\`\`\`js
document.querySelector('#master-yoda')
// => <div id="master-yoda">Yoda</div>

document.querySelector('.class-of-assassins')
// => <div class="class-of-assassins">Assassin</div>

document.querySelector('p')
// => <p>The three little pigs</p>

document.querySelector('[data-type="rocket"]')
// => <div data-type="rocket">🚀</div>
\`\`\`
`);

console.log(html);

Warning

  • ONLY FULLY WORKS IN BROWSER (Adds an event listener to the window to detect button clicks). If you load the module serverside AND on the browser, you may be able to call showdownCopyCode.addListener() on the browser. Otherwise, you need to do your own implementation.

  • Styles not included out of the box.

  • Needs testing.

  • Need to make sure the fix for the showdown-highlight pre option doesn't mess up.

  • Uses navigator.clipboard, which might need a polyfill on older browsers.

Q&A

Why { showdownCopyCode } instead of showdownCopyCode?

Default vs Named exports.

License

MIT

Resources Used/Help/Credit

About

Showdown extension that adds a button above code blocks to quickly and easily copy code to the clipboard.

Topics

Resources

License

Stars

Watchers

Forks