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

eleventy-plugin-code-clipboard

v0.2.0

Published

eleventy plugin to copy fence into clipboard

Downloads

237

Readme

eleventy-plugin-code-clipboard

npm version

This plugin adds a clipboard copy button to the code snippets block(codetag) generated by eleventy.

The clipboard copy feature uses clipboard.js library. This Plugin assumes using with eleventy-plugin-syntaxhighlight plugin.

The plugin consists of the following contents.

markdown-it custom renderer

This custom renderer attaches clipboard copy button to code block with language(does nothing for non-language/mermaid block). Of course, it only supports eleventy markdown template(markdown-it).

The clipboard button is provided by Iconify content-copy icon(default).

eleventy shortcode function

This shortcode initializes clipboard.js on window.onload event. If clipboard copy succeeded, it shows tooltips with message(default to Copied!). Tooltips use Primer Tooltips CSS framework.

Usage

Add the following code to project's .eleventy.js(or eleventy.config.js).

const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const codeClipboard = require("eleventy-plugin-code-clipboard");
const markdownIt = require('markdown-it');

module.exports = function (eleventyConfig) {
  eleventyConfig.addPlugin(syntaxHighlight);
  eleventyConfig.addPlugin(codeClipboard);
  
  // others plugins, etc...
  
  const markdownLibrary = markdownIt({
    html: true
  }).use(codeClipboard.markdownItCopyButton);
  
  eleventyConfig.setLibrary("md", markdownLibrary);
  
  return {
    // your settings
  }
}

Add Primer CSS to your CSS or HTML.

@import url("https://cdnjs.cloudflare.com/ajax/libs/Primer/19.1.1/tooltips.min.css");

or

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Primer/19.1.1/tooltips.min.css" crossorigin="anonymous" referrerpolicy="no-referrer" />

Add 11ty shortcode(named initClipboardJS) function your template. This will generate javascript code(scripttag) that initialize clipboard.js and attach onclick eventListener in your HTML.

<html>
  <head>...</head>
  <body>
    ...
    {% initClipboardJS %}
  </body>
</html>

Configuration

plugin options

eleventyConfig.addPlugin(codeClipboard, {
  // Version of clipboard.js to use. default to 2.0.11.
  clipboardJSVersion: '2.0.11',
  // Name of clipboard button css class. default to code-copy.
  // This class is also used to renderer
  // Click event of element with this class is listened by clipboard.js.
  buttonClass: 'code-copy',
  // Message if copy succeeds. default to "Copied!"
  successMessage: 'Copied!',
  // Message if copy failes. default to "Failed..."
  failureMessage: 'Failed...',
});

renderer options(markdown-it)

const markdownLibrary = markdownIt({
  html: true
}).use(codeClipboard.markdownItCopyButton, {
  // Iconfiy icon URL. default to the following. 
  // To make changes, please refer to the Iconify page(https://icon-sets.iconify.design/)
  iconifyUrl: 'https://api.iconify.design/mdi/content-copy.svg',
  // Style attributes of clipboard icon. default to the following.
  iconStyle: 'width: 16px; height: 16px;',
  // Class attributes of clipboard icon. default to empty.
  iconClass: '',
  // Name of HTML tag of clipboard icon. default to span.
  iconTag: 'span',
  // Name of clipboard button css class. default to code-copy.
  // This class should be the same as the plugin options
  buttonClass: 'code-copy',
  // Style attributes of button. default to the following.
  buttonStyle: 'position: absolute; top: 7.5px; right: 6px; padding-top: 3px; cursor: pointer; outline: none; opacity: 0.8;',
  // Additional class attributes in addition to the plugin option(buttonClass). default to empty.
  additionalButtonClass: '',
  // Name of title attribute in Button. default to "Copy".
  title: 'Copy',
});

Example

see here

Migration(v0.1.0)

Prior to version 0.1.0, we used Material Design Icons WebFont. However, due to size issues, we have switched to using Iconify SVG.

There are some breaking changes that you should be aware of:

  • CSS for Material Design Icons(WebFont) is no longer necessary.
  • Iconify URLs have been added to the renderer settings.
  • Default values for iconStyle/iconClass have been changed.

Tips

If you use prismjs's dark theme, the color of the code block and the tooltip will be assimilated, so you will not be able to see the tooltip. To avoid this, overwrite the primer tooltip definition with your favorite color in CSS.

.tooltipped::before {
  color: #fcf !important;
  border-bottom-color: #ffccff !important;
}

.tooltipped::after {
  background-color: #fcf !important;
  color: #303 !important;
}