Skip to content

Commit

Permalink
copy-button
Browse files Browse the repository at this point in the history
  • Loading branch information
namachan10777 committed Jun 30, 2024
1 parent 624ba6e commit 0fce887
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 1 deletion.
Binary file modified bun.lockb
Binary file not shown.
175 changes: 175 additions & 0 deletions packages/shiki-copy-button/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
15 changes: 15 additions & 0 deletions packages/shiki-copy-button/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# shiki-copy-button

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run index.ts
```

This project was created using `bun init` in bun v1.1.17. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
50 changes: 50 additions & 0 deletions packages/shiki-copy-button/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { ShikiTransformer } from "shiki";
import { getIconData } from "@iconify/utils";
import { icons, type IconifyJSON } from "@iconify-json/iconoir";
import type { ElementContent, Element } from "hast";
import { parse } from "svg-parser";

function iconSvgHast(icons: IconifyJSON, name: string): Element {
const icon = getIconData(icons, name)!;
const svg = parse(icon.body);
return {
type: "element",
tagName: "svg",
properties: {
width: 24,
height: 24,
viewBox: `0 0 ${icon.width} ${icon.height}`,
fill: "none",
xmlns: "http://www.w3.org/2000/svg",
},
children: svg.children as ElementContent[],
};
}

export function shikiCopyButton(): ShikiTransformer {
return {
pre: (hast) => {
const copy = iconSvgHast(icons, "copy");
const check = iconSvgHast(icons, "check");
copy.properties.class = [
"shiki-copy-button-copy",
"shiki-copy-button-icon",
].join(" ");
check.properties.class = [
"shiki-copy-button-check",
"shiki-copy-button-icon",
].join(" ");
hast.children = [
{
type: "element",
tagName: "button",
properties: {
class: "shiki-copy-button",
},
children: [copy, check],
},
...hast.children,
];
},
};
}
19 changes: 19 additions & 0 deletions packages/shiki-copy-button/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "shiki-copy-button",
"module": "index.ts",
"type": "module",
"devDependencies": {
"@iconify/types": "^2.0.0",
"@iconify/utils": "^2.1.25",
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@iconify-json/iconoir": "^1.1.44",
"@types/hast": "^3.0.4",
"iconify": "^1.4.0",
"shiki": "^1.10.0"
}
}
27 changes: 27 additions & 0 deletions packages/shiki-copy-button/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}
3 changes: 2 additions & 1 deletion packages/web/astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import remarkGemoji from "remark-gemoji";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import remarkSectionize from "remark-sectionize";
import { shikiCopyButton } from "shiki-copy-button";
import tsConfigPaths from "vite-tsconfig-paths";

// https://astro.build/config
Expand All @@ -33,7 +34,7 @@ export default defineConfig({
rehypePrettyCode,
{
theme: { dark: "github-dark", light: "github-light" },
transformers: [],
transformers: [shikiCopyButton()],
},
],
],
Expand Down
3 changes: 3 additions & 0 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@types/bun": "^1.1.6",
"@types/license-checker": "^25.0.6",
"@types/remark-heading-id": "^1.0.0",
"@types/svg-parser": "^2.0.6",
"astro": "^4.11.3",
"astro-icon": "^1.1.0",
"astro-seo": "^0.8.4",
Expand All @@ -56,6 +57,8 @@
"retext-stringify": "^4.0.0",
"sharp": "^0.33.4",
"shiki": "^1.9.0",
"shiki-copy-button": "workspace:*",
"svg-parser": "^2.0.4",
"typescript": "^5.4.5",
"unified": "^11.0.4",
"vite-tsconfig-paths": "^4.3.2"
Expand Down
33 changes: 33 additions & 0 deletions packages/web/src/components/composite/MdArticle.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@
<slot />
</article>

<script>
document.querySelectorAll("button.shiki-copy-button").forEach((element) => {
const button = element as HTMLButtonElement;
console.log(button);
button.addEventListener("click", () => {
const text = button.parentNode?.querySelector("code")?.textContent;
if (text) {
navigator.clipboard.writeText(text);
button.dataset.copied = "true";
setTimeout(() => {
button.dataset.copied = "false";
}, 1500);
}
});
});
</script>

<style>
@layer component {
article {
Expand Down Expand Up @@ -31,6 +48,22 @@
padding: 0.5rem;
border-radius: 0.2rem;
scrollbar-color: var(--fg-thumb) var(--fg-track);
position: relative;
}

article :global(pre button.shiki-copy-button) {
position: absolute;
right: 0.3rem;
top: 0.3rem;
}

article :global(pre button[data-copied="true"] .shiki-copy-button-copy) {
display: none;
}

article
:global(pre button:not([data-copied="true"]) .shiki-copy-button-check) {
display: none;
}

article :global(pre code) {
Expand Down

0 comments on commit 0fce887

Please sign in to comment.