Adding Copy to Clipboard button to minimal-mistakes with clipboard.js #2795
Replies: 7 comments 11 replies
-
Looks like an easy solution to this highly-voted feature. One thing I'm not confident @mmistakes will like is that the library code is about 1k lines unminified. Since we're only using a subset of its features (and quite simple!), I'm sure there's a better way to write some smaller code for the same function. Maybe I could give it a try when I have time, but unfortunately not very soon. |
Beta Was this translation helpful? Give feedback.
-
I've written my own code for this. Check it out on my website (Wayback Machine). And here's my code. I haven't tested it extensively yet, but it looks decent at first glance. There's one specific thing to check before it's good for PR. I don't know if the colors go on well with so many "skins" of the theme because I just set the button to white on my site. Maybe @mmistakes knows how to make this work. I copied quite some code from both zenorocha/clipboard.js and squidfunk/mkdocs-material. Thanks to both projects! P.S. Both projects mentioned above are MIT-licensed so we have nothing to worry about. |
Beta Was this translation helpful? Give feedback.
-
Thank you for the write up! It worked like a champ right out of the gate, compared to the other posts that are out there this works perfectly with MM. And I thank you for the explanation of the background as well. That is helpful. Looking at this, there may be a way to get at the code blocks that are created with the Liquid tags as well? No big deal if it is not. Just thought I'd check before I go revamping previous blog posts. Updating the previous posts are just fine to do, and are probably a little more portable then if I needed to. |
Beta Was this translation helpful? Give feedback.
-
[Updated] In response to #2795 (comment) @jvanderaa about how to make it work with code blocks created with Liquid tags AKA See the full script here and the working demo here. The code now select all the I still think this is not completely safe, but it does the job. |
Beta Was this translation helpful? Give feedback.
-
I wrapped my stuff up at #2812. Anyone interested can help test it out and hopefully it'll ship in the next version of Minimal Mistakes. |
Beta Was this translation helpful? Give feedback.
-
Just wanted to say thank you! This worked perfectly! |
Beta Was this translation helpful? Give feedback.
-
If anyone is looking for a version that doesn't depend on ClipboardJS, check out this gist. Using it is as simple as adding it to the |
Beta Was this translation helpful? Give feedback.
-
Following #2338, #2508 and #2766, I share here how to set up a "Copy to Clipboad" button on code chunks, using the plugin https://clipboardjs.com/.
Note that this solution only works if you use rouge highlighting, see #2795 (comment) for an alternative approach if you use
{% highlight %}
.On your
_config.yml
Add these lines:
On your repo
Copy to
assets/js/clipboardrouge.js
this script link🎉 Build and enjoy
clipboardrouge.js
explainedThese functions would be used to make the button blink for 250 ms after it is activated. The main goal is to make aware the visitor that the action has been succesful (on green) or not (on red).
This is the critical part of the code. It selects all the
<pre>
tags of the page withclass="highlight
, and loops through all of them (for (var i = 0; i < codeChunk.length; i++)
).On each loop, a button is created after the
<pre>
tag (pre.insertBefore(btn, pre.firstChild);
) with the fontawesome iconfar fa-copy
as the single content. After, we add to the button some css classes ("btn", "btn--primary"
), inline styling (style="position: absolute; right: 1em;
) and adata-clipboard-copy
attribute that would be the identifier used by clipboard.js.More attributes, as arias or titles can be set with
btn.setAttribute("attribute", "value");
This is the clipboard.js part (see their docs). When the button is activated, the
nextElementSibling
is copied.nextElementSibling
corresponds to the<code>
part, as we have inserted the buttons after the<pre>
and thus right before the<code>
.The following parts just adds additional actions after success or error. Here we use the function
buttonBlink()
to make the button blink on green (changes tobtn--success
) or red (btn--danger
) for 250ms, as explained before.Additionally, the console of the browser would show what it has been copied.
Example:
def print_hi(name)
puts "Hi, #{name}"
end
print_hi('Tom')
#=> prints 'Hi, Tom' to STDOUT.
Hope this can help. If considered (@mmistakes @iBug ) I could prepare a PR. It is a quite straightforward way to add a new feature to this project.
Beta Was this translation helpful? Give feedback.
All reactions