1.0.2 • Published 17 days ago

shiki-filename-copy-transformers v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
17 days ago

Shiki Copy Button

A Shiki Transformer that adds a Copy button.

Install

Install the package:

npm install -D shiki-filename-copy-transformers

Add the transformer:

import { codeToHtml } from "shiki/bundle/full";
import { addCopyButton } from "shiki-filename-copy-transformers";

export async function highlight(code, lang, meta) {
  return await codeToHtml(code, {
    lang,
    // Optional parameter
    transformers: [addCopyButton(3000), addFilename("index.ts")],
  });
}

You can get the filename from the third parameters meta. Your markdown code block can look like this:

```js title="file.js"
import path from "path";

import adapter from "@sveltejs/adapter-node";
```

You will be able to get the title string from the third parameter meta.

Button style

Add some basic styling to display the button:

pre:has(code) {
  position: relative;
}

pre button.copy {
  position: absolute;
  right: 16px;
  top: 16px;
  height: 28px;
  width: 28px;
  padding: 0;
  display: flex;

  & span {
    width: 100%;
    aspect-ratio: 1 / 1;
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
  }

  & .ready {
    background-image: url(/icons/copy.svg);
  }

  & .success {
    display: none;
    background-image: url(/icons/copy-success.svg);
  }

  &.copied {
    & .success {
      display: block;
    }

    & .ready {
      display: none;
    }
  }
}

License

MIT

1.0.2

17 days ago

1.0.1

17 days ago

1.0.0

17 days ago