Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/piotrpdev/showdown-copy-code

Showdown extension that adds a button above code blocks to quickly and easily copy code to the clipboard.
https://github.com/piotrpdev/showdown-copy-code

blocks button clipboard code copy extension highlight showdown

Last synced: about 2 months ago
JSON representation

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

Awesome Lists containing this project

README

        

# showdown-copy-code

[![Version](https://img.shields.io/npm/v/showdown-copy-code.svg)](https://www.npmjs.com/package/showdown-copy-code)

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

*~~Yoinked~~ **Heavily inspired** by [this project](https://www.dannyguo.com/blog/how-to-add-copy-to-clipboard-buttons-to-code-blocks-in-hugo/)* (*[go check out the creator](https://github.com/dguo)*).

## How do I install it?

```bash
npm install showdown-copy-code
```

## Usage

*~~Yoinked~~ **Heavily inspired** by the [showdown-highlight example.](https://github.com/Bloggify/showdown-highlight#clipboard-example)*

```js
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:


\`\`\`js
document.querySelector('#master-yoda')
// =>

Yoda

document.querySelector('.class-of-assassins')
// =>

Assassin

document.querySelector('p')
// =>

The three little pigs

document.querySelector('[data-type="rocket"]')
// =>

🚀

\`\`\`
`);

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.](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard#browser_compatibility)

## Q&A

> Why `{ showdownCopyCode }` instead of `showdownCopyCode`?

[Default vs Named exports.](https://github.com/rollup/rollup/issues/1961)

## License

MIT

## Resources Used/Help/Credit

* [showdown-highlight](https://github.com/Bloggify/showdown-highlight/blob/master/lib/index.js)

* [Blog post on clipboard buttons](https://www.dannyguo.com/blog/how-to-add-copy-to-clipboard-buttons-to-code-blocks-in-hugo/)

* [Showdown extensions wiki page](https://github.com/showdownjs/showdown/wiki/Extensions)

* [StackOverflow event listener question](https://stackoverflow.com/questions/12045440/difference-between-document-addeventlistener-and-window-addeventlistener)

* [StackOverflow dynamic event listener question](https://stackoverflow.com/questions/11455515/how-to-check-whether-dynamically-attached-event-listener-exists-or-not)

* [StackOverflow RegExp question](https://stackoverflow.com/questions/43784794/javascript-regex-ignore-anything-in-between)