Copy Codeblocks Theme Component

:warning: This theme component is now deprecated in favor of Copy codeblock contents to clipboard, a core Discourse feature.

:sun_with_face: Preview on Theme Creator
:computer: Git Repository:
:bulb: How do I install a Theme or Theme Component?

This theme component adds a copy button to codeblocks inside of Discourse posts – perfect for communities sharing code samples or knowledge base articles that require copying text.

On desktop, the copy button shows up on hover over the block.

On mobile, the code button is shown always but pushes the code in the block down to avoid obfuscating the first line of code if too long.

Do note that this only works for codeblocks generated by ``` ```, and not inline codeblocks.

This is a codeblock.
Copy this text.

This is an inline codeblock. Copy does not work here.

Big thanks to @joffreyjaffeux who did a major chunk of the initial development on this one.


What is the difference between this and COPY option for code blocks in Discourse ?


Multiple things:

  • doesn’t vendor a full clipboard lib
  • doesn’t rely on jQuery
  • doesn’t leak event listeners
  • doesn’t rely on multiple events
  • uses up to date coding standards
  • doesn’t have the quoting bug

Not sure if this is working correctly on mobile too.



Curious if we are open to replacing the localized “copy”/“copied” text with icons? Maybe the copy icon for “copy”, and the checkmark icon for “copied”?


I’m open to adding a setting to the component. The reason I decided to go with localized text is it is clear what is happening, plus text can have a shorter height and still be readable. The aesthetic of it seemed to fit the use case better.


It sounds like we should remove / deprecate one or the other and consolidate effort? :thinking:


Minor, the copied delay is too fast on my iPhone, text should say copied for around 3 seconds prior to flipping back

It does look good :+1:


Modified here:

Also fixed a couple of bugs with older versions of Discourse that don’t have @ember/runloop and Promise from the rsvp library available. (Thanks to @jomaxro for the reports on those)


Sorry, but a :heart: isn’t enough this time.

Sweet! This is awesome. This combined with the Discourse-placeholder-theme-component is really cool.

what I just did with it

sfdisk -d /dev/=gooddev= | sfdisk /dev/=newdev=
mdadm --manage /dev/md1 --add /dev/=newdev=
mdadm --detail /dev/md1

Watch it work!

while true; do mdadm --detail /dev/md1|grep "Rebuild Status" ; sleep 10;done

Get Power On time for all drives

for x in a b c d ; do smartctl -a /dev/sd$x|grep Power_On; done

I like this a lot! :heart_eyes: But can the copy button be a visible-on-tap-only thing for mobile? I’m a little bummed by the consistent loss of screen space on mobile, an extra line for each code block:

But then again I realize

  • code blocks aren’t that common
  • since it’s opt-in you’d only enable it on sites where actually copying code blocks is a common activity as well

… that being said… the odds of any given code block being copied on mobile are vanishingly small, so I feel the ratio of UI to usage is off here, and would prefer an on-tap thing for mobile?

Not urgent.


Yes totally fair request :+1: I share this concern too, we will find a solution.

1 Like

Even this would be preferable on mobile, I think. Alignment is off but you get the concept:

(Even if it overlaps text, etc, not sure if that makes it harder or easier than on-tap display? :thinking:)

We had that originally but it overlapped text without being able to see it. I like the idea of it becoming visible on tap. Will look into that.

1 Like

On mobile that is a better tradeoff, big time. On laptop/desktop/tablet, the additional space being consumed is 100% fine.

1 Like


I would love to use this but as soon as I enabled it for my Grey Amber theme, my site became sluggish and unresponsive at times until I turn it off.

Has anyone experience this behavior, or could it tax the VPS that much when this is enabled?

The site is like 99.9% single code lines and I was hoping to switch into code blocks for this, so is not like is trying to fetch to convert any existing code. Or is it?

Do I have to give it some amount of time to normalize? It just kept bringing the site down temporarily and when sometimes a page would load it would say error and the try again message from discourse would show up.

Thanks in advance.


Oh wow that sounds like a serious bug actually!

1 Like

Minor Bug: the “copy” text can show up in a quote. (Much more minor than the previous post.)

The button.copy-cmd needs to be excluded from the quote. Applying user-select: none in the CSS fixes Ctrl-C but not the quote functionality.


Do you have a link to your site? It seems very weird than a theme could cause this. How many codeblocks do you have on a page?

So I made multiple changes here. Given our problems were:

  • screen space on mobile
  • copy text bug on quote
  • and we have had request of having an icon instead of text

The obvious solution solving all of this… was to use an icon.

On mobile it’s now acceptable to have it on the right, given it takes less space and we can just have some padding for text:

Only minor annoyance, on long FIRST lines it will be hidden behind button, I think it’s acceptable given you can scroll:

And in Desktop it will work as before without the quoting bug:

Concerning, the possible perf bug, I tried a page with dozens of code blocks and got 0 issue, so I will need a reproduction before I can do anything @lcestou please

Try the new codeblock here:

rsync -a file.txt file_backup.txt
rsync -a /var/www/public_html/ /var/www/public_html_backup/

This feature is now in core Discourse :tada:

As a result, this theme component is now DEPRECATED. It will still work for older Discourse instances but will no longer receive any updates.

I’ve updated the OP to reflect this information.