# How do I add a "copy" button to a code snippet?

**URL:** <https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586>\
**Category:** Developer help\
**Created:** [December 3, 2022, 5:18pm UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586 "2022-12-03T17:18:12Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Theo](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/theo/32/2432_2.png) [@Theo](https://forum.ghost.org/u/Theo)\
**Post date:** [December 3, 2022, 5:18pm UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/1 "2022-12-03T17:18:12Z")

</div>

How do I add a “copy” button for code snippets on my Ghost blog?

 ![Screen Shot 2022-12-03 at 12.05.04 PM](https://us1.discourse-cdn.com/flex015/uploads/ghost2/original/3X/5/3/53f7d47e6db6a1299b879e9a2411cf1097c52a5b.png)

The screen capture is from this Ghost [post](https://ghost.org/tutorials/code-snippets-in-ghost/) on adding color to code snippets.

I’m running Ghost-CLI 1.23.1 and Ghost 5.25.0.

Any help is much appreciated!

Ted

---

<div class="post-metadata">

**Author:** ![diliprk](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/diliprk/32/20442_2.png) [@diliprk](https://forum.ghost.org/u/diliprk)\
**Post date:** [December 5, 2022, 4:50pm UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/2 "2022-12-05T16:50:09Z")

</div>

You have to pick that option when you download the JS and CSS files from prismJS website, see the pic below

 ![CopyCode Button](https://us1.discourse-cdn.com/flex015/uploads/ghost2/original/3X/6/a/6ae23b9dee35d8d510335ba7f48b1df50ede99ad.jpeg)  
Download these 2 files and place them as they say in the tutorial.

Here is a link with some pre-selected options:  
[https://prismjs.com/download.html#themes=prism-okaidia&languages=markup+css+clike+javascript+css-extras+python&plugins=autolinker+custom-class+show-language+highlight-keywords+inline-color+autoloader+data-uri-highlight+toolbar+copy-to-clipboard+match-braces](https://prismjs.com/download.html#themes=prism-okaidia&languages=markup+css+clike+javascript+css-extras+python&plugins=autolinker+custom-class+show-language+highlight-keywords+inline-color+autoloader+data-uri-highlight+toolbar+copy-to-clipboard+match-braces)

---

<div class="post-metadata">

**Author:** ![RyanF](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/ryanf/32/17448_2.png) [@RyanF](https://forum.ghost.org/u/RyanF)\
**Post date:** [December 5, 2022, 4:59pm UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/3 "2022-12-05T16:59:32Z")

</div>

As @diliprk said, Prism has a copy to clipboard button plugin. However, the one used on the tutorials site uses custom code to generate the copy button. Here’s the relevant snippet:

```javascript
function initCodeCopy() {
    const codeBlocks = document.querySelectorAll('code[class*="language-"]');

    codeBlocks.forEach((block) => {
        const lang = parseLanguage(block);
        const referenceEl = block.parentElement;
        const parent = block.parentElement.parentElement;
        
        const wrapper = document.createElement('div');
        wrapper.className = 'code-wrapper';
        parent.insertBefore(wrapper, referenceEl);
        wrapper.append(block.parentElement);

        const copyBtn = document.createElement('button');
        copyBtn.setAttribute('class', 'copy-button');
        copyBtn.setAttribute('data-lang', lang);
        copyBtn.innerHTML = `${lang} <svg viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M7 6V3a1 1 0 0 1 1-1h12a1 1 0 0 1 1 1v14a1 1 0 0 1-1 1h-3v3c0 .552-.45 1-1.007 1H4.007A1.001 1.001 0 0 1 3 21l.003-14c0-.552.45-1 1.007-1H7zM5.003 8L5 20h10V8H5.003zM9 6h8v10h2V4H9v2z" fill="currentColor"/></svg>`;

        wrapper.insertAdjacentElement('beforeend', copyBtn);
    });

    function parseLanguage(block) {
        const className = block.className;
        if (className.startsWith('language')) {
            const [prefix, lang] = className.split('-');
            return lang;
        }
    }

    function copy(e) {
        const btn = e.currentTarget;
        const lang = btn.dataset.lang;

        const text = e.currentTarget.previousSibling.children[0].textContent;

        navigator.clipboard.writeText(text).then(
            () => {
                btn.innerHTML = `copied! <svg viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M7 6V3a1 1 0 0 1 1-1h12a1 1 0 0 1 1 1v14a1 1 0 0 1-1 1h-3v3c0 .552-.45 1-1.007 1H4.007A1.001 1.001 0 0 1 3 21l.003-14c0-.552.45-1 1.007-1H7zm2 0h8v10h2V4H9v2z" fill="currentColor"/></svg>`;
                btn.setAttribute('style', 'opacity: 1');
                
            },
            () => alert('failed to copy'),
        );

        setTimeout(() => {
            btn.removeAttribute('style');
            btn.innerHTML = `${lang} <svg viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M7 6V3a1 1 0 0 1 1-1h12a1 1 0 0 1 1 1v14a1 1 0 0 1-1 1h-3v3c0 .552-.45 1-1.007 1H4.007A1.001 1.001 0 0 1 3 21l.003-14c0-.552.45-1 1.007-1H7zM5.003 8L5 20h10V8H5.003zM9 6h8v10h2V4H9v2z" fill="currentColor"/></svg>`;
        }, 3000);
    }

    const copyButtons = document.querySelectorAll('.copy-button');

    copyButtons.forEach((btn) => {
        btn.addEventListener('click', copy);
    });
}

initCodeCopy()

```

---

<div class="post-metadata">

**Author:** ![Theo](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/theo/32/2432_2.png) [@Theo](https://forum.ghost.org/u/Theo)\
**Post date:** [December 5, 2022, 5:40pm UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/4 "2022-12-05T17:40:44Z")

</div>

That’s awesome Ryan!  
Thanks!  
I hope, at some point it will become available through an editing menu or something. Maybe I’ll be able to try and tackle that at some point and request that the code be added to Ghost?  
Thanks so much for replying.  
Kind regards,  
Ted

---

<div class="post-metadata">

**Author:** ![Theo](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/theo/32/2432_2.png) [@Theo](https://forum.ghost.org/u/Theo)\
**Post date:** [December 5, 2022, 7:00pm UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/5 "2022-12-05T19:00:43Z")

</div>

Thank you to diliprk as well!

---

<div class="post-metadata">

**Author:** ![Theo](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/theo/32/2432_2.png) [@Theo](https://forum.ghost.org/u/Theo)\
**Post date:** [December 5, 2022, 7:43pm UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/6 "2022-12-05T19:43:12Z")

</div>

@RyanF, does that code have to be copied into a particular file, or can it be put in as a code injection?

If it has to be in a file, would you possibly be able to direct me to the right file?

Kind regards,

Ted

---

<div class="post-metadata">

**Author:** ![RyanF](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/ryanf/32/17448_2.png) [@RyanF](https://forum.ghost.org/u/RyanF)\
**Post date:** [December 5, 2022, 7:54pm UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/7 "2022-12-05T19:54:18Z")

</div>

You should be able to add the code to the **Site Footer** in **Code Injection**.

---

<div class="post-metadata">

**Author:** ![Theo](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/theo/32/2432_2.png) [@Theo](https://forum.ghost.org/u/Theo)\
**Post date:** [December 5, 2022, 8:34pm UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/8 "2022-12-05T20:34:51Z")

</div>

Thanks again @RyanF!  
Sorry for the noob question, but does the calling of the `initCodeCopy()` function have to be done within a file, or can it also be added to the Site Footer through Code Injection?  
My complete lack of understanding of node.js is evident. I’m also seeking to find the answer myself, but it could be a while before I know enough to understand.  
Kind regards,  
Ted

---

<div class="post-metadata">

**Author:** ![Theo](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/theo/32/2432_2.png) [@Theo](https://forum.ghost.org/u/Theo)\
**Post date:** [December 6, 2022, 12:38am UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/9 "2022-12-06T00:38:14Z")

</div>

Tried this as an experiment:

```auto
<script>

function initCodeCopy() {
    const codeBlocks = document.querySelectorAll('code[class*="language-"]');

    codeBlocks.forEach((block) => {
        const lang = parseLanguage(block);
        const referenceEl = block.parentElement;
        const parent = block.parentElement.parentElement;

        const wrapper = document.createElement('div');
        wrapper.className = 'code-wrapper';
        parent.insertBefore(wrapper, referenceEl);
        wrapper.append(block.parentElement);

        const copyBtn = document.createElement('button');
        copyBtn.setAttribute('class', 'copy-button');
        copyBtn.setAttribute('data-lang', lang);
        copyBtn.innerHTML = `${lang} <svg viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M7 6V3a1 1 0 0 1 1-1h12a1 1 0 0 1 1 1v14a1 1 0 0 1-1 1h-3v3c0 .552-.45 1-1.007 1H4.007A1.001 1.001 0 0 1 3 21l.003-14c0-.552.45-1 1.007-1H7zM5.003 8L5 20h10V8H5.003zM9 6h8v10h2V4H9v2z" fill="currentColor"/></svg>`;

        wrapper.insertAdjacentElement('beforeend', copyBtn);
    });

    function parseLanguage(block) {
        const className = block.className;
        if (className.startsWith('language')) {
            const [prefix, lang] = className.split('-');
            return lang;
        }
    }

    function copy(e) {
        const btn = e.currentTarget;
        const lang = btn.dataset.lang;

        const text = e.currentTarget.previousSibling.children[0].textContent;

        navigator.clipboard.writeText(text).then(
            () => {
                btn.innerHTML = `copied! <svg viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M7 6V3a1 1 0 0 1 1-1h12a1 1 0 0 1 1 1v14a1 1 0 0 1-1 1h-3v3c0 .552-.45 1-1.007 1H4.007A1.001 1.001 0 0 1 3 21l.003-14c0-.552.45-1 1.007-1H7zm2 0h8v10h2V4H9v2z" fill="currentColor"/></svg>`;
                btn.setAttribute('style', 'opacity: 1');

            },
            () => alert('failed to copy'),
        );

        setTimeout(() => {
            btn.removeAttribute('style');
            btn.innerHTML = `${lang} <svg viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M7 6V3a1 1 0 0 1 1-1h12a1 1 0 0 1 1 1v14a1 1 0 0 1-1 1h-3v3c0 .552-.45 1-1.007 1H4.007A1.001 1.001 0 0 1 3 21l.003-14c0-.552.45-1 1.007-1H7zM5.003 8L5 20h10V8H5.003zM9 6h8v10h2V4H9v2z" fill="currentColor"/></svg>`;
        }, 3000);
    }

    const copyButtons = document.querySelectorAll('.copy-button');

    copyButtons.forEach((btn) => {
        btn.addEventListener('click', copy);
    }('initCodeCopy'));

</script>

No luck so far. Seems like the initialization would have to be related to the snippet box.
```

---

<div class="post-metadata">

**Author:** ![diliprk](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/diliprk/32/20442_2.png) [@diliprk](https://forum.ghost.org/u/diliprk)\
**Post date:** [December 6, 2022, 2:48pm UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/10 "2022-12-06T14:48:05Z")

</div>

Hi Ryan, I added the code you sent in a `<script>` tag in code injection, and also after the end of the `</script>` tag, I also called the `initCodeCopy()` function but all it did was add the text in the home page header.

 ![Screenshot 2022-12-06 at 8.19.34 PM](https://us1.discourse-cdn.com/flex015/uploads/ghost2/original/3X/f/8/f83de532507dc0dea13ba58ecdceaf24ace4e0a0.jpeg)

I also did not see any change in my Copy button from the code block that I originally got from the prismJS website CSS/JS download.

 ![Screenshot 2022-12-06 at 8.11.22 PM](https://us1.discourse-cdn.com/flex015/uploads/ghost2/original/3X/2/c/2c6faa409da2ba60d64ab628fca1095e13341cc7.jpeg)

It would thus be great if you could send us the `prism.css` and `prism.JS` files, that are being used in the [ghost tutorial post](https://ghost.org/tutorials/code-snippets-in-ghost) and clear instructions on where/how to call this `initCodeCopy()` function ?

Best Regards

---

<div class="post-metadata">

**Author:** ![RyanF](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/ryanf/32/17448_2.png) [@RyanF](https://forum.ghost.org/u/RyanF)\
**Post date:** [December 6, 2022, 4:36pm UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/11 "2022-12-06T16:36:30Z")

</div>

This isn’t quite right. I updated the code snippet to be exactly what you’d paste into the `<script>` tag.

---

<div class="post-metadata">

**Author:** ![RyanF](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/ryanf/32/17448_2.png) [@RyanF](https://forum.ghost.org/u/RyanF)\
**Post date:** [December 6, 2022, 4:40pm UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/12 "2022-12-06T16:40:00Z")

</div>

This code runs independently of Prism—I’m using Prism without any plugins, except the autoimporter.

You may have to write some of your own code to get it to work with your site.

If you want something that will work out of the box, I’d just reach for Prism’s own plugins.

---

<div class="post-metadata">

**Author:** ![diliprk](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/diliprk/32/20442_2.png) [@diliprk](https://forum.ghost.org/u/diliprk)\
**Post date:** [December 6, 2022, 4:56pm UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/13 "2022-12-06T16:56:48Z")

</div>

I downloaded the new prism.css and prism.js file with just `Autolinker` and `Autoimporter` options, checked in the plugins and I only saw the `Autoloader` and `Autolinker` check box on the prism download page not sure where was `Autoimporter`checbox. Your revised code injection script worked (functionally) and I get the **copy** button alright, but alignment is not coming properly. I guess one has to tinker with the .css to get this alignment working perfectly.

 ![Screenshot 2022-12-06 at 10.19.44 PM](https://us1.discourse-cdn.com/flex015/uploads/ghost2/original/3X/8/2/8280e67fb7d91427e8b5bee522ea08bfa8f5e835.jpeg)

Since I don’t have much time to figure this css out, I will stick with the default layouts I get with the prism JS downloads, for now

Thanks and Regards

---

<div class="post-metadata">

**Author:** ![RyanF](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/ryanf/32/17448_2.png) [@RyanF](https://forum.ghost.org/u/RyanF)\
**Post date:** [December 8, 2022, 2:40pm UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/14 "2022-12-08T14:40:07Z")

</div>

This gets you pretty close.

If you have a live version of this up and can share the link, I can give you some guidance on the CSS, as it’ll differ slightly depending on your theme, etc.

---

<div class="post-metadata">

**Author:** ![scottmckendry](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/scottmckendry/32/22782_2.png) [@scottmckendry](https://forum.ghost.org/u/scottmckendry)\
**Post date:** [January 29, 2023, 5:24am UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/15 "2023-01-29T05:24:08Z")

</div>

For anyone looking for an easier way to implement this, I’ve got it working on my Ghost instance by adding the following to the Header:

```auto
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/toolbar/prism-toolbar.min.css" integrity="sha512-Dqf5696xtofgH089BgZJo2lSWTvev4GFo+gA2o4GullFY65rzQVQLQVlzLvYwTo0Bb2Gpb6IqwxYWtoMonfdhQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />

```

And the following to the Footer:

```auto
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/toolbar/prism-toolbar.min.js" integrity="sha512-st608h+ZqzliahyzEpETxzU0f7z7a9acN6AFvYmHvpFhmcFuKT8a22TT5TpKpjDa3pt3Wv7Z3SdQBCBdDPhyWA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js" integrity="sha512-/kVH1uXuObC0iYgxxCKY41JdWOkKOxorFVmip+YVifKsJ4Au/87EisD1wty7vxN2kAhnWh6Yc8o/dSAXj6Oz7A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

```

Which looks like this:  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/ghost2/original/3X/4/b/4b8f6811f2af33a852713ca25c2e09f9882e871e.png)

---

<div class="post-metadata">

**Author:** ![mitya.info](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/mitya.info/32/37552_2.png) [@mitya.info](https://forum.ghost.org/u/mitya.info)\
**Post date:** [August 24, 2025, 7:33pm UTC](https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586/16 "2025-08-24T19:33:24Z")

</div>

![image](https://us1.discourse-cdn.com/flex015/uploads/ghost2/original/3X/3/f/3f74ab90bf68db12c94733dabebf6cfc11bd3fa4.png)

😭 i used this code but style not good ([Customize your download ▲ Prism](https://prismjs.com/download#themes=prism-dark&plugins=toolbar+copy-to-clipboard))
