Added "copy button" to code fences

Hi all,
Just letting you know I was just looking for an option to copy code fences to the clipboard, as this is incredibly useful especially for huge chunks of YAML for pre-made UI widgets.

Turns out the option already existed in Discourse, so naturally I activated it. You’ll get a little button in all code fences and you can click on it to copy the entire thing to the clipboard. Then you can simply paste it wherever you need it. No more selecting a bunch of code around with the mouse.

Note that using GitHub Gists (if you don’t want to create a full-fledged repo) and copying the link is also a very clever alternative for these pieces of code, since it’s overall better presented and the versioning is clearer too.

Cheers,
Yannick

9 Likes

off-topic … but any chance of restoring the visibility of code fence icons to the toolbar of the standard reply box?

It’s a plugin by @ThomDietrich which isn’t maintained anymore it seems. Looks like there’s no alternative available at first sight.

There’s an open issue about it.

1 Like

…and a 2019 comment from the developer saying it needs to be fixed quickly LOL

This fork: GitHub - AGulev/discourse-plugin-code-fences-buttons: Discourse Plugin: Code Fences Buttons (cfbtn) looks like it has a fix.
But I don’t have access to the server so can’t Install Plugins in Discourse - admins - Discourse Meta.
@digitaldan or @kai would have to do it.

1 Like

In the meantime, since it doesn’t work well, I have disabled the plugin, and found another option to use the 3-backticks instead of 4-space indents for the “preformatted text” button.

So you can simply use the </> (7th) button on the toolbar, or the Ctrl-Shift-C shortcut to insert a code fence (or toggle the code fence for the selected text).

3 Likes

Testing with <> (6th button for me)

var Integer Tperim = 15

// Initialize
rule "Light timings init"
when
	System started
then

Yup, works a treat :smiley:
Sorry to hijack your topic - but pleased to get a simple solution in place, people struggle with this, thankyou!

1 Like

But … it looks like code is wrapped now/then for long lines of code while a scrollbar was used before for long code text. Code is not as nice readable as before - or do I miss something ?

But … it looks like code is wrapped now/then for long lines of code while a scrollbar was used before for long code text. Code is not as nice readable as before - or do I miss something ?

I see a horizontal scroll here.
I poked the <> button, which produces prompt -

‘’’
type or paste code here
‘’’
and pasted in as invited.

2 Likes

Yes, you are right a test shows the behavior as you described it. I am sure I used that button, too but the line is wrapped in an other thread.
I will have a look at it next time again.

With todays forum software upgrade, I also switched to the fork of that plugin and enabled it.
While the buttons are there, their icon seems to be missing, though, so it is a bit hidden…:

Screenshot 2021-03-12 at 17.13.38

Shall I leave it activated nonetheless?