npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jpfulton/gatsby-remark-copy-button

v1.0.10

Published

A Gatsby remark plugin to support copy buttons on code regions with support for MDX.

Downloads

253

Readme

gatsby-remark-copy-button

ci npm version License Visitors

Add a copy button to markdown code snippets in Gatsby sites with compatibility for MDX and use of the browser Clipboard API.

This package and implementation is discussed in this blog post.

Install

yarn add @jpfulton/gatsby-remark-copy-button

How to Use

Add the configuration entry to your gatsby-config.js file. This plugin must be added before other plugins that operate on code nodes and markdown code snippets to operate correctly.

The following listing assumes you are using the gatsby-plugin-mdx plugin. However, this plugin may also be used with the gatsby-transformer-remark plugin.

plugins: [
  {
    resolve: `gatsby-plugin-mdx`,
    options: {
      extensions: [`.mdx`, `.md`],
      gatsbyRemarkPlugins: [
        {
          resolve: `@jpfulton/gatsby-remark-copy-button`,
        },
        {
          resolve: `gatsby-remark-code-titles`,
        },
        {
          resolve: `gatsby-remark-prismjs`,
        },
      ],
    },
  },
],

Options

All plugin options are optional. However, it is strongly suggested that you customize them to override styling to fit your site's look, feel and layout.

{
  resolve: `@jpfulton/gatsby-remark-copy-button`,
  options: {
    // Provide a text label for the copy button.
    // Default: null
    buttonText: null,
    // Provide a complete SVG tag string to replace the default
    // copy icon. Be sure to include a class of "copy-icon" on your custom
    // SVG tag when using this option.
    copySvg: null,
    // Provide a complete SVG tag string to replace the default
    // success icon.  Be sure to include a class of "success-icon" on your custom
    // SVG tag when using this option.
    successSvg: null,
    // Provide a custom container class for the <div> tag that contains
    // the copy button to apply custom styling.
    // Default: "gatsby-remark-copy-button-container"
    customButtonContainerClass: null,
    // Provide a custom button class for the copy button to apply
    // custom styling.
    // Default: "gatsby-remark-copy-button"
    customButtonClass: null,
  },
},

Custom Styling

Custom styling may be applied to the default classes or using the options above custom classes may be applied to the injected markup.

.gatsby-remark-copy-button-container {
}
.gatsby-remark-copy-button {
}

Apply custom styles by adding a style sheet to your gatsby-browser.js file.

// gatsby-browser.js
import "./src/styles/copy-button.scss";

Structure of the Injected Markup

When enabled on code snippet, the following HTML will be injected into the output of the page after parsing the Markdown AST using the default plugin options. It will be injected above the code snippet in the generated HTML.

<div class="gatsby-remark-copy-button-container">
  <button
    class="gatsby-remark-copy-button"
    onclick="copyToClipboard(`CLEANED CODE CONTENT TO COPY HERE`, this)"
  >
    <svg
      class="copy-icon"
      xmlns="http://www.w3.org/2000/svg"
      width="24"
      height="24"
      viewBox="0 0 24 24"
    >
      ...
    </svg>
    <svg
      class="success-icon"
      xmlns="http://www.w3.org/2000/svg"
      width="24"
      height="24"
      viewBox="0 0 24 24"
    >
      ...
    </svg>
  </button>
</div>

Usage in Markdown

Once installed, the copy button may optionally be enabled by adding to the code snippet declaration within markdown files. When this plugin is used in conjunction with the gatsby-remark-prismjs plugin, the {clipboardButton: true} option may be provided in any order with other prismjs options.

```js {clipboardButton: true}
const example = "This content will end up on the user's clipboard";
```

Further Information

For details on contributing to this project, reference both the CONTRIBUTING.md and CODE_OF_CONDUCT.md documentation.

To submit a bug or feature request, please open an issue here.

To report create a security advisory, please reference the SECURITY.md policy document.