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

highlightjs-copy

v1.0.6

Published

A simple, accessible highlightjs plugin to add a copy button to your codeblocks.

Downloads

16,795

Readme

highlightjs-copy

Netlify Status

A simple, accessible highlightjs plugin to add a copy button to your codeblocks.

Preview

Demo

Check out the demo

Install

Using a CDN

<script src="https://unpkg.com/highlightjs-copy/dist/highlightjs-copy.min.js"></script>
<link
  rel="stylesheet"
  href="https://unpkg.com/highlightjs-copy/dist/highlightjs-copy.min.css"
/>

NPM

npm install highlightjs-copy

Usage

Basic usage

hljs.addPlugin(new CopyButtonPlugin());

Autohide

By default, the copy button is hidden until a user hovers the code block. Set this to false to have the copy button always visible.

hljs.addPlugin(
  new CopyButtonPlugin({
    autohide: false, // Always show the copy button
  })
);

With a callback

hljs.addPlugin(
  new CopyButtonPlugin({
    callback: (text, el) => console.log("Copied to clipboard", text),
  })
);

Modify copied text with hooks

hljs.addPlugin(
  new CopyButtonPlugin({
    hook: (text, el) => text + "\nCopied from my cool website.",
  })
);

Advanced hook example

<!-- Code block example -->
<pre>
  <code class="language-bash" data-replace="{{YOUR_API_KEY}}" data-replaceWith="grtf32a35bbc...">
    gretel configure --key {{YOUR_API_KEY}}
  </code>
</pre>

<script>
  hljs.addPlugin(
    new CopyButtonPlugin({
      hook: (text, el) => {
        let { replace, replacewith } = el.dataset;
        if (replace && replacewith) {
          return text.replace(replace, replacewith);
        }
        return text;
      },
      callback: (text, el) => {
        /* logs `gretel configure --key grtf32a35bbc...` */
        console.log(text);
      },
    })
  );
  hljs.highlightAll();
</script>

Localization

highlightjs-copy supports multiple locales by providing the correct language for accessibility.

hljs.addPlugin(
  new CopyButtonPlugin({
    lang: "es", // The copy button now says "Copiado!" when selected.
  })
);

This option is unnecessary if you correctly add the lang attribute to your document. You can override this behavior by providing the lang option as described above.

<html lang="es">
  <body>
    <!-- The plugin language defaults to `es` to match the document, so manually setting it is unnecessary. -->
    <script>
      hljs.addPlugin(new CopyButtonPlugin());
    </script>
  </body>
</html>

If the document has no lang set and the lang option is not provided, it will default to en.

Customization

| CSS selector | Details | | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | .hljs-copy-wrapper | Applied to the parent <pre> element that wraps the .hljs code. | | .hljs-copy-container | A parent element to the button, in charge of absolute positioning and animating the button when hovering. | | .hljs-copy-button | The copy button itself.The variables --hljs-theme-background and --hljs-theme-color are automatically applied to the parent element. This allows the button to inherit the code block's text and background colors. | | [data-copied='true'] | This data attribute is applied to the copy button and is set to true for two seconds when the copy action is performed. | | .hljs-copy-alert | A visually hidden status element that announces the copy confirmation to screen readers. |