Skip to content

[question] How to have a click to copy feature for code blocks ? #3177

Answered by kgibm
aahnik asked this question in Q&A
Discussion options

You must be logged in to vote

It would be great if mkdocs provides "copy to clipboard" for readthedocs theme, out of the box.

I accomplished this as follows:

  1. Import ClipboardJS and Tippy (tippy-bundle.umd.js and popper.min.js) into a js subfolder.
  2. Create css/extra.css which styles the copy-to-clipboard button:
    .clipboard {
        position: relative;
        display: block;
        float: right;
    }
    
    .btn-clipboard {
        position: relative;
        top: .5rem;
        right: .5rem;
        z-index: 10;
        display: block;
        padding: .25rem .5rem;
        cursor: pointer;
    }
  3. Create js/extra.js which uses jQuery (built into the theme) to wait for the page to load and then find all code blocks, insert a Copy button, and add handlers:
    $(document).r…

Replies: 4 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@niccokunzmann
Comment options

Answer selected by aahnik
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
Needs information Theme-readthedocs Issues specifically involving the readthedocs theme.
5 participants
Converted from issue

This discussion was converted from issue #2238 on April 10, 2023 19:07.