@mirrordown/sat-copy-code 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +176 -2
- package/dist/client.d.mts +1 -0
- package/dist/client.mjs +45 -0
- package/dist/client.mjs.map +1 -0
- package/dist/copy-code.css +171 -0
- package/dist/index.d.mts +21 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +31 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +45 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
<sub>2026-11-02</sub>
|
|
5
|
+
|
|
6
|
+
- *(minor)*
|
|
7
|
+
Add the copy-code plugin set: every fenced and indented code block gets a copy button, in its `<pre>` after the `<code>`, with no wrapper element. The button is an invoker command (`command="--copy"`) aimed at the `<pre>`, which takes an id from a hash of its code (`code-<hash>`, with `-2`, `-3`… for repeats). Each package ships a small client script (`./client`) that copies the block's code, without the newline markdown ends it with, shows a check with "Copied!" in a pill beside it, as GitHub does, and announces it to screen readers; the buttons render `hidden` until it runs, so a page without it shows none. The `icons` and `text` options set the button's icon and text in each state. Write `nocopy` after the fence's language to leave a block without one; a host that highlights first can keep the meta in a `data-meta` attribute on the `<pre>`. Where CSS anchor positioning with `anchor-scope` is supported, the button sits in the block's top corner and shows on hover or focus. The markdown-it, remark and Sätteri plugins emit identical HTML.
|
package/README.md
CHANGED
|
@@ -1,3 +1,177 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mirrordown/sat-copy-code
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Part of [Mirrordown](https://github.com/mirrordown/mirrordown) — a suite of markdown syntax extensions for the unified, markdown-it and Sätteri ecosystems.
|
|
4
|
+
|
|
5
|
+
A Sätteri plugin for the `copy-code` syntax extension.
|
|
6
|
+
|
|
7
|
+
## Overview
|
|
8
|
+
|
|
9
|
+
The `copy-code` plugin adds a copy button to every code block. The button goes in the block's `<pre>`, after its `<code>`, with no wrapper element. It's an [invoker command](https://developer.mozilla.org/docs/Web/API/Invoker_Commands_API) aimed at the `<pre>`, which takes an id from a hash of the code:
|
|
10
|
+
|
|
11
|
+
```html
|
|
12
|
+
<pre id="code-mxxtmr"><code class="language-sh">npm install @mirrordown/remd-copy-code
|
|
13
|
+
</code><button type="button" class="markdown-copy-code" command="--copy" commandfor="code-mxxtmr" aria-label="Copy code" hidden>
|
|
14
|
+
<span class="markdown-copy-code-icon" data-state="copy" aria-hidden="true"><svg>...</svg></span>
|
|
15
|
+
<span class="markdown-copy-code-icon" data-state="copied" aria-hidden="true"><svg>...</svg></span>
|
|
16
|
+
<span class="markdown-copy-code-text" data-state="copied" aria-hidden="true">Copied!</span>
|
|
17
|
+
</button></pre>
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Copying needs JavaScript: no browser copies to the clipboard from markup alone. So each package ships a small client script that handles the `--copy` command, and the buttons render `hidden` until it runs. A page without the script, or a browser without invoker commands or the clipboard, shows no button rather than one that does nothing.
|
|
21
|
+
|
|
22
|
+
## Syntax
|
|
23
|
+
|
|
24
|
+
There's nothing to write: every fenced and indented code block gets a button. Hover a block to see it, or tab to it.
|
|
25
|
+
|
|
26
|
+
````markdown
|
|
27
|
+
```sh
|
|
28
|
+
npm install @mirrordown/remd-copy-code
|
|
29
|
+
```
|
|
30
|
+
````
|
|
31
|
+
|
|
32
|
+
A block's id comes from a hash of its code, so it doesn't change when other blocks are edited. Repeated blocks get `-2`, `-3` and so on, in document order.
|
|
33
|
+
|
|
34
|
+
### Turning it off for a block
|
|
35
|
+
|
|
36
|
+
Write `nocopy` after the fence's language to leave a block without a button, for output or code that isn't meant to be run as is:
|
|
37
|
+
|
|
38
|
+
````markdown
|
|
39
|
+
```sh nocopy
|
|
40
|
+
$ npm test
|
|
41
|
+
✓ 42 tests passed
|
|
42
|
+
```
|
|
43
|
+
````
|
|
44
|
+
|
|
45
|
+
A block that another plugin renders as something else, such as a [mermaid diagram](/guide/plugins/mermaid-pele), gets no button either.
|
|
46
|
+
|
|
47
|
+
A host that highlights code before your plugins run, such as Astro with Shiki, rebuilds each block from its code and language and drops the fence's meta. The plugin also reads the meta from a `data-meta` attribute on the `<pre>`, so keep it there with a Shiki transformer:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
// astro.config.ts
|
|
51
|
+
export default defineConfig({
|
|
52
|
+
markdown: {
|
|
53
|
+
shikiConfig: {
|
|
54
|
+
transformers: [
|
|
55
|
+
{
|
|
56
|
+
pre(node) {
|
|
57
|
+
const meta = this.options.meta?.__raw;
|
|
58
|
+
if (meta) node.properties.dataMeta = meta;
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## The client script
|
|
68
|
+
|
|
69
|
+
Import the script once per page, from the package for your engine. It does three things:
|
|
70
|
+
|
|
71
|
+
- **Shows the buttons.** It adopts a small stylesheet into the document, so buttons on pages that client-side navigation swaps in, such as Astro's view transitions, show too.
|
|
72
|
+
- **Copies.** It handles the `--copy` command by copying the text of the block's `<code>`, without the newline markdown ends a block with.
|
|
73
|
+
- **Says so.** For two seconds, as GitHub's button does, it shows a check with its copied text beside it, "Copied!" by default, and takes that text as its label. A polite live region reads the text to screen readers.
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import "@mirrordown/remd-copy-code/client";
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Configuration
|
|
80
|
+
|
|
81
|
+
All options are passed when registering the plugin, or as the presets' `copyCode` option.
|
|
82
|
+
|
|
83
|
+
### Icons
|
|
84
|
+
|
|
85
|
+
`icons` replaces the button's icon in either state with your own SVG markup: `copy` before the block is copied, `copied` after. The stylesheet sizes it to `1rem` and colors it with `currentColor`. An icon can be replaced but not removed, since it's all the button holds, so an empty one keeps the default. The defaults are Lucide's copy and check icons, exported as `COPY_CODE_ICONS`.
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
satteriCopyCode({ icons: { copied: `<svg viewBox="0 0 16 16">...</svg>` } })
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Text
|
|
92
|
+
|
|
93
|
+
`text` sets the text shown beside the button in each state. The `copied` text, "Copied!" by default, shows once the block is copied. A `copy` text, none by default, shows while the button is hovered or focused. Either one also names the button while it shows, so set them in your page's language. `null` or an empty string shows no text in that state.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
satteriCopyCode({ text: { copy: "Copy", copied: "Copied" } })
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Accessibility
|
|
100
|
+
|
|
101
|
+
The button is a real `<button>`, so it's reachable by keyboard and named for screen readers. It's labeled "Copy code", or by its copy text when it has one, so the text a reader sees is the button's name. Its icons and text are `aria-hidden`, since the label already says what they show. The button is hidden until the script can copy, so assistive technology never meets a control that does nothing.
|
|
102
|
+
|
|
103
|
+
## Customization
|
|
104
|
+
|
|
105
|
+
The stylesheet keeps its rules in `@layer markdown-copy-code`, so any unlayered CSS you write overrides them. Where [CSS anchor positioning](https://developer.mozilla.org/docs/Web/CSS/CSS_anchor_positioning) with `anchor-scope` is supported, the button sits in the block's top corner, anchored to the `<pre>` itself, so it stays put while a long line scrolls. On devices that hover, it shows while the block is hovered or the button focused. Elsewhere it sits at the end of the block.
|
|
106
|
+
|
|
107
|
+
Its states follow GitHub's. At rest it's a muted icon on the block's background. Hovered, a tint of the block's text color fades in. Once copied, the icon turns into a green check and the text shows in a pill beside it. The pill is dark with white text, as GitHub's is, in a lighter gray where the page's [`color-scheme`](https://developer.mozilla.org/docs/Web/CSS/color-scheme) is dark, with a faint ring that sets it off a block of the same shade. The button itself takes its colors from the block, so it suits any code theme. Change the hover tint and the green with custom properties on the button or any ancestor:
|
|
108
|
+
|
|
109
|
+
```css
|
|
110
|
+
.content {
|
|
111
|
+
--markdown-copy-code-tint: oklch(0.6 0.15 250 / 0.2);
|
|
112
|
+
--markdown-copy-code-success: oklch(0.6 0.15 250);
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
To color the pill yourself, set `background-color` and `color` on `.markdown-copy-code-text`. A page that sets no `color-scheme` gets the light page's pill.
|
|
117
|
+
|
|
118
|
+
## VS Code
|
|
119
|
+
|
|
120
|
+
There's no VS Code extension for this plugin: VS Code's Markdown preview has had its own copy button on code blocks since version 1.132.
|
|
121
|
+
|
|
122
|
+
## Install
|
|
123
|
+
|
|
124
|
+
```sh
|
|
125
|
+
npm install @mirrordown/sat-copy-code
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
List it after plugins that replace code blocks, such as mermaid-pele.
|
|
129
|
+
|
|
130
|
+
### Standalone
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
import { markdownToHtml } from "satteri";
|
|
134
|
+
import { satteriCopyCode } from "@mirrordown/sat-copy-code";
|
|
135
|
+
|
|
136
|
+
const { html } = markdownToHtml(source, {
|
|
137
|
+
hastPlugins: [satteriCopyCode()],
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// In the page:
|
|
141
|
+
import "@mirrordown/sat-copy-code/copy-code.css";
|
|
142
|
+
import "@mirrordown/sat-copy-code/client";
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Astro
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
// astro.config.ts
|
|
149
|
+
import { defineConfig } from "astro/config";
|
|
150
|
+
import { satteri } from "@astrojs/markdown-satteri";
|
|
151
|
+
import { satteriCopyCode } from "@mirrordown/sat-copy-code";
|
|
152
|
+
|
|
153
|
+
export default defineConfig({
|
|
154
|
+
markdown: {
|
|
155
|
+
processor: satteri({ hastPlugins: [satteriCopyCode()] }),
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
```astro
|
|
161
|
+
---
|
|
162
|
+
// In your layout:
|
|
163
|
+
import "@mirrordown/sat-copy-code/copy-code.css";
|
|
164
|
+
---
|
|
165
|
+
|
|
166
|
+
<script>
|
|
167
|
+
import "@mirrordown/sat-copy-code/client";
|
|
168
|
+
</script>
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Documentation
|
|
172
|
+
|
|
173
|
+
Full documentation, more examples, and configuration options: [github.com/mirrordown/mirrordown](https://github.com/mirrordown/mirrordown) (dedicated docs site coming soon).
|
|
174
|
+
|
|
175
|
+
## License
|
|
176
|
+
|
|
177
|
+
MIT © [Drake Costa](https://saeris.gg)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
package/dist/client.mjs
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
const COMMAND = `--copy`;
|
|
3
|
+
const COPIED_FOR = 2e3;
|
|
4
|
+
const timers = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
const labels = /* @__PURE__ */ new WeakMap();
|
|
6
|
+
const status = document.createElement(`div`);
|
|
7
|
+
status.setAttribute(`aria-live`, `polite`);
|
|
8
|
+
status.style.cssText = `position:absolute;inline-size:1px;block-size:1px;overflow:clip;clip-path:inset(50%);white-space:nowrap`;
|
|
9
|
+
const announce = (message) => {
|
|
10
|
+
if (!status.isConnected) document.body.append(status);
|
|
11
|
+
status.textContent = message;
|
|
12
|
+
};
|
|
13
|
+
const copy = async (button, pre) => {
|
|
14
|
+
const code = pre.querySelector(`:scope > code`) ?? pre;
|
|
15
|
+
try {
|
|
16
|
+
await navigator.clipboard.writeText(code.textContent.replace(/\n$/, ``));
|
|
17
|
+
} catch {
|
|
18
|
+
announce(`Couldn't copy the code`);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const copied = button.querySelector(`.markdown-copy-code-text[data-state="copied"]`)?.textContent ?? `Copied`;
|
|
22
|
+
if (!labels.has(button)) labels.set(button, button.getAttribute(`aria-label`) ?? ``);
|
|
23
|
+
announce(copied);
|
|
24
|
+
button.dataset.copied = ``;
|
|
25
|
+
button.setAttribute(`aria-label`, copied);
|
|
26
|
+
clearTimeout(timers.get(button));
|
|
27
|
+
timers.set(button, window.setTimeout(() => {
|
|
28
|
+
status.textContent = ``;
|
|
29
|
+
delete button.dataset.copied;
|
|
30
|
+
button.setAttribute(`aria-label`, labels.get(button) ?? ``);
|
|
31
|
+
}, COPIED_FOR));
|
|
32
|
+
};
|
|
33
|
+
if (`CommandEvent` in window && `clipboard` in navigator) {
|
|
34
|
+
document.addEventListener(`command`, (event) => {
|
|
35
|
+
if (!(event instanceof CommandEvent) || event.command !== COMMAND) return;
|
|
36
|
+
const { source, target } = event;
|
|
37
|
+
if (source instanceof HTMLElement && target instanceof Element) copy(source, target);
|
|
38
|
+
}, { capture: true });
|
|
39
|
+
const reveal = new CSSStyleSheet();
|
|
40
|
+
reveal.replaceSync(`@layer markdown-copy-code { .markdown-copy-code[hidden] { display: flex; } }`);
|
|
41
|
+
document.adoptedStyleSheets = [...document.adoptedStyleSheets, reveal];
|
|
42
|
+
}
|
|
43
|
+
export {};
|
|
44
|
+
|
|
45
|
+
//# sourceMappingURL=client.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.mjs","names":[],"sources":["../src/client.ts"],"sourcesContent":["// The copy buttons' client script: import it once per page. A button is an invoker command (`command=\"--copy\"`) aimed at its code block's `<pre>`, which this script handles by copying the `<code>` in it. The buttons render `hidden`, and this script shows them, so a page without it, or a browser without invoker commands or the clipboard, has no button that does nothing. It shows them with a stylesheet adopted into the document, and listens on the document, so buttons on pages that client-side navigation swaps in work too.\n\nconst COMMAND = `--copy`;\n// How long a button shows that its block was copied.\nconst COPIED_FOR = 2000;\n\nconst timers = new WeakMap<HTMLElement, number>();\n// Each button's own name, given back once its copied state ends.\nconst labels = new WeakMap<HTMLElement, string>();\n\n// A polite live region, so screen readers hear that the code was copied; attached again after navigation replaces the body it was in.\nconst status = document.createElement(`div`);\nstatus.setAttribute(`aria-live`, `polite`);\nstatus.style.cssText = `position:absolute;inline-size:1px;block-size:1px;overflow:clip;clip-path:inset(50%);white-space:nowrap`;\nconst announce = (message: string): void => {\n if (!status.isConnected) document.body.append(status);\n status.textContent = message;\n};\n\nconst copy = async (button: HTMLElement, pre: Element): Promise<void> => {\n const code = pre.querySelector(`:scope > code`) ?? pre;\n try {\n await navigator.clipboard.writeText(code.textContent.replace(/\\n$/, ``));\n } catch {\n announce(`Couldn't copy the code`);\n return;\n }\n // The button's copied text, as the plugin's options set it, names it while it shows.\n const copied =\n button.querySelector(`.markdown-copy-code-text[data-state=\"copied\"]`)\n ?.textContent ?? `Copied`;\n if (!labels.has(button))\n labels.set(button, button.getAttribute(`aria-label`) ?? ``);\n announce(copied);\n button.dataset.copied = ``;\n button.setAttribute(`aria-label`, copied);\n clearTimeout(timers.get(button));\n timers.set(\n button,\n window.setTimeout(() => {\n // Cleared, so copying again is announced again.\n status.textContent = ``;\n delete button.dataset.copied;\n button.setAttribute(`aria-label`, labels.get(button) ?? ``);\n }, COPIED_FOR)\n );\n};\n\n// The clipboard is only there in secure contexts.\nif (`CommandEvent` in window && `clipboard` in navigator) {\n document.addEventListener(\n `command`,\n (event) => {\n if (!(event instanceof CommandEvent) || event.command !== COMMAND) return;\n const { source, target } = event;\n if (source instanceof HTMLElement && target instanceof Element)\n void copy(source, target);\n },\n // The command event doesn't bubble: catch it on its way down to the <pre>.\n { capture: true }\n );\n\n // In the stylesheet's layer, so the page's own CSS can still hide the buttons.\n const reveal = new CSSStyleSheet();\n reveal.replaceSync(\n `@layer markdown-copy-code { .markdown-copy-code[hidden] { display: flex; } }`\n );\n document.adoptedStyleSheets = [...document.adoptedStyleSheets, reveal];\n}\n\n// A module, so its names stay out of the global scope.\nexport {};\n"],"mappings":";AAEA,MAAM,UAAU;AAEhB,MAAM,aAAa;AAEnB,MAAM,yBAAS,IAAI,QAA6B;AAEhD,MAAM,yBAAS,IAAI,QAA6B;AAGhD,MAAM,SAAS,SAAS,cAAc,KAAK;AAC3C,OAAO,aAAa,aAAa,QAAQ;AACzC,OAAO,MAAM,UAAU;AACvB,MAAM,YAAY,YAA0B;CAC1C,IAAI,CAAC,OAAO,aAAa,SAAS,KAAK,OAAO,MAAM;CACpD,OAAO,cAAc;AACvB;AAEA,MAAM,OAAO,OAAO,QAAqB,QAAgC;CACvE,MAAM,OAAO,IAAI,cAAc,eAAe,KAAK;CACnD,IAAI;EACF,MAAM,UAAU,UAAU,UAAU,KAAK,YAAY,QAAQ,OAAO,EAAE,CAAC;CACzE,QAAQ;EACN,SAAS,wBAAwB;EACjC;CACF;CAEA,MAAM,SACJ,OAAO,cAAc,+CAA+C,CAAC,EACjE,eAAe;CACrB,IAAI,CAAC,OAAO,IAAI,MAAM,GACpB,OAAO,IAAI,QAAQ,OAAO,aAAa,YAAY,KAAK,EAAE;CAC5D,SAAS,MAAM;CACf,OAAO,QAAQ,SAAS;CACxB,OAAO,aAAa,cAAc,MAAM;CACxC,aAAa,OAAO,IAAI,MAAM,CAAC;CAC/B,OAAO,IACL,QACA,OAAO,iBAAiB;EAEtB,OAAO,cAAc;EACrB,OAAO,OAAO,QAAQ;EACtB,OAAO,aAAa,cAAc,OAAO,IAAI,MAAM,KAAK,EAAE;CAC5D,GAAG,UAAU,CACf;AACF;AAGA,IAAI,kBAAkB,UAAU,eAAe,WAAW;CACxD,SAAS,iBACP,YACC,UAAU;EACT,IAAI,EAAE,iBAAiB,iBAAiB,MAAM,YAAY,SAAS;EACnE,MAAM,EAAE,QAAQ,WAAW;EAC3B,IAAI,kBAAkB,eAAe,kBAAkB,SACrD,KAAU,QAAQ,MAAM;CAC5B,GAEA,EAAE,SAAS,KAAK,CAClB;CAGA,MAAM,SAAS,IAAI,cAAc;CACjC,OAAO,YACL,8EACF;CACA,SAAS,qBAAqB,CAAC,GAAG,SAAS,oBAAoB,MAAM;AACvE"}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copy code — default stylesheet
|
|
3
|
+
------------------------------
|
|
4
|
+
All rules live inside @layer markdown-copy-code so any unlayered consumer
|
|
5
|
+
CSS overrides them without needing !important.
|
|
6
|
+
|
|
7
|
+
A code block's copy button sits in its <pre>, after the <code>. It renders
|
|
8
|
+
`hidden`, and the client script shows it where it can copy. Where CSS anchor positioning with
|
|
9
|
+
anchor-scope is supported, the button sits in the block's top corner,
|
|
10
|
+
anchored to the <pre> rather than its scrolling content, and on devices that
|
|
11
|
+
hover it shows while the block is hovered or the button focused; elsewhere
|
|
12
|
+
it sits at the end of the block.
|
|
13
|
+
|
|
14
|
+
Its states follow GitHub's: a muted icon with no fill at rest, a tint on
|
|
15
|
+
hover, and once copied a green check with its text in a pill beside it.
|
|
16
|
+
|
|
17
|
+
To override public tokens, set them on the button or any ancestor:
|
|
18
|
+
|
|
19
|
+
.content { --markdown-copy-code-tint: oklch(0.6 0.15 250 / 0.2); }
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
@layer markdown-copy-code {
|
|
23
|
+
.markdown-copy-code {
|
|
24
|
+
/* The block's own colors, so the button suits any code theme: its background, and a tint of its text color on hover. */
|
|
25
|
+
--_copy-code-tint: var(
|
|
26
|
+
--markdown-copy-code-tint,
|
|
27
|
+
color-mix(in oklch, currentColor 10%, transparent)
|
|
28
|
+
);
|
|
29
|
+
/* Green, drawn a quarter of the way toward the text color, so it reads on light and dark blocks alike. */
|
|
30
|
+
--_copy-code-success: var(
|
|
31
|
+
--markdown-copy-code-success,
|
|
32
|
+
color-mix(in oklch, oklch(0.62 0.17 150) 75%, currentColor)
|
|
33
|
+
);
|
|
34
|
+
--_copy-code-fill: transparent;
|
|
35
|
+
|
|
36
|
+
position: relative;
|
|
37
|
+
align-items: center;
|
|
38
|
+
justify-content: center;
|
|
39
|
+
inline-size: 2rem;
|
|
40
|
+
block-size: 2rem;
|
|
41
|
+
margin-inline-start: auto;
|
|
42
|
+
padding: 0;
|
|
43
|
+
border: 0;
|
|
44
|
+
border-radius: 0.375rem;
|
|
45
|
+
background-color: inherit;
|
|
46
|
+
/* The tint as an inset shadow over the block's background, so it can fade. */
|
|
47
|
+
box-shadow: inset 0 0 0 1rem var(--_copy-code-fill);
|
|
48
|
+
color: inherit;
|
|
49
|
+
cursor: pointer;
|
|
50
|
+
/* GitHub's button timing. */
|
|
51
|
+
transition: box-shadow 80ms cubic-bezier(0.33, 1, 0.68, 1);
|
|
52
|
+
|
|
53
|
+
@media (hover: hover) and (pointer: fine) {
|
|
54
|
+
&:hover {
|
|
55
|
+
--_copy-code-fill: var(--_copy-code-tint);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
&:active {
|
|
60
|
+
--_copy-code-fill: color-mix(
|
|
61
|
+
in oklch,
|
|
62
|
+
var(--_copy-code-tint),
|
|
63
|
+
currentColor 10%
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
&:focus-visible {
|
|
68
|
+
outline: 2px solid currentColor;
|
|
69
|
+
outline-offset: 2px;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
.markdown-copy-code-icon {
|
|
74
|
+
display: flex;
|
|
75
|
+
inline-size: 1rem;
|
|
76
|
+
block-size: 1rem;
|
|
77
|
+
color: color-mix(in oklch, currentColor 75%, transparent);
|
|
78
|
+
|
|
79
|
+
> svg {
|
|
80
|
+
inline-size: 100%;
|
|
81
|
+
block-size: 100%;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
&[data-state="copied"] {
|
|
85
|
+
color: var(--_copy-code-success);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
.markdown-copy-code:not([data-copied]) > [data-state="copied"],
|
|
90
|
+
.markdown-copy-code[data-copied] > [data-state="copy"],
|
|
91
|
+
.markdown-copy-code-text {
|
|
92
|
+
display: none;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/* The text: a pill beside the button, toward the block's start, dark with white text as GitHub's tooltip is, and a lighter gray where the page's color-scheme is dark, so it stands off a dark page. */
|
|
96
|
+
.markdown-copy-code-text {
|
|
97
|
+
position: absolute;
|
|
98
|
+
inset-inline-end: calc(100% + 0.375rem);
|
|
99
|
+
inset-block-start: 50%;
|
|
100
|
+
translate: 0 -50%;
|
|
101
|
+
padding: 0.25rem 0.5rem;
|
|
102
|
+
border-radius: 0.375rem;
|
|
103
|
+
background-color: light-dark(oklch(0.27 0 none), oklch(0.4 0 none));
|
|
104
|
+
/* A faint ring in its text color, which shows only where the block is as dark as the pill. */
|
|
105
|
+
box-shadow: 0 0 0 1px color-mix(in oklch, currentColor 25%, transparent);
|
|
106
|
+
color: oklch(0.98 0 none);
|
|
107
|
+
font:
|
|
108
|
+
0.75rem/1.5 system-ui,
|
|
109
|
+
sans-serif;
|
|
110
|
+
white-space: nowrap;
|
|
111
|
+
pointer-events: none;
|
|
112
|
+
animation: markdown-copy-code-appear 0.1s ease-out;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
.markdown-copy-code[data-copied]
|
|
116
|
+
> .markdown-copy-code-text[data-state="copied"],
|
|
117
|
+
.markdown-copy-code:not([data-copied]):focus-visible
|
|
118
|
+
> .markdown-copy-code-text[data-state="copy"] {
|
|
119
|
+
display: block;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
@media (hover: hover) and (pointer: fine) {
|
|
123
|
+
.markdown-copy-code:not([data-copied]):hover
|
|
124
|
+
> .markdown-copy-code-text[data-state="copy"] {
|
|
125
|
+
display: block;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
@keyframes markdown-copy-code-appear {
|
|
130
|
+
from {
|
|
131
|
+
opacity: 0;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
@supports (anchor-scope: --x) {
|
|
136
|
+
/* Each <pre> scopes the name, so its button anchors to it rather than the page's last block. */
|
|
137
|
+
pre:has(> .markdown-copy-code) {
|
|
138
|
+
anchor-name: --markdown-copy-code;
|
|
139
|
+
anchor-scope: --markdown-copy-code;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
.markdown-copy-code {
|
|
143
|
+
position: absolute;
|
|
144
|
+
position-anchor: --markdown-copy-code;
|
|
145
|
+
position-visibility: anchors-visible;
|
|
146
|
+
inset-block-start: anchor(start);
|
|
147
|
+
inset-inline-end: anchor(end);
|
|
148
|
+
margin: 0.5rem;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
@media (hover: hover) and (pointer: fine) {
|
|
152
|
+
.markdown-copy-code {
|
|
153
|
+
opacity: 0;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
pre:hover > .markdown-copy-code,
|
|
157
|
+
.markdown-copy-code:focus-visible,
|
|
158
|
+
.markdown-copy-code[data-copied] {
|
|
159
|
+
opacity: 1;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
163
|
+
.markdown-copy-code {
|
|
164
|
+
transition:
|
|
165
|
+
box-shadow 80ms cubic-bezier(0.33, 1, 0.68, 1),
|
|
166
|
+
opacity 0.15s ease-out;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { COPY_CODE_ICONS, COPY_CODE_TEXT, CopyCodeOptions, CopyCodeState } from "@mirrordown/core";
|
|
2
|
+
import { HastVisitorInstance, PluginFactoryContext } from "satteri";
|
|
3
|
+
type ElementVisitor = Extract<NonNullable<HastVisitorInstance["element"]>, {
|
|
4
|
+
filter: string[];
|
|
5
|
+
}>;
|
|
6
|
+
/**
|
|
7
|
+
* The plugin's definition: only the keys it defines, with a synchronous visitor, so `markdownToHtml` returns its result directly.
|
|
8
|
+
*/
|
|
9
|
+
export interface CopyCodePlugin {
|
|
10
|
+
name: string;
|
|
11
|
+
element: {
|
|
12
|
+
filter: string[];
|
|
13
|
+
visit: (...args: Parameters<ElementVisitor["visit"]>) => void;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Sätteri plugin that adds a copy button to each code block, in its `<pre>` after the `<code>`: a `<button class="markdown-copy-code">` sending the `--copy` invoker command to the block's `<pre>`, which takes an id from a hash of its text. The button is a `raw` node, so it reaches the HTML exactly as the markdown-it and rehype plugins write it. The buttons render `hidden`; the client script (`@mirrordown/sat-copy-code/client`) copies and shows them. ` ```sh nocopy ` leaves a block without one. Options set the button's icons and text. List it after plugins that replace code blocks, such as mermaid-pele. A factory, so each document's ids start fresh. The ids and the button are shared with the markdown-it and rehype plugins through `@mirrordown/core`.
|
|
18
|
+
*/
|
|
19
|
+
export declare const satteriCopyCode: (options?: CopyCodeOptions) => ((ctx: PluginFactoryContext) => CopyCodePlugin);
|
|
20
|
+
export { COPY_CODE_ICONS, COPY_CODE_TEXT, type CopyCodeOptions, type CopyCodeState };
|
|
21
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;KAmBK,iBAAiB,QACpB,YAAY;EACV;;;;;iBAMa;EACf;EACA;IACE;IACA,WAAW,MAAM,WAAW;;;;;;qBAOnB,kBACX,UAAS,sBACN,KAAK,yBAAyB"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { COPY_CODE_ICONS, COPY_CODE_TEXT, copyCodeBlock, copyCodeIds, copyCodeOptedOut, createCopyCodeButton } from "@mirrordown/core";
|
|
2
|
+
import { defineHastPlugin } from "satteri";
|
|
3
|
+
// src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* Sätteri plugin that adds a copy button to each code block, in its `<pre>` after the `<code>`: a `<button class="markdown-copy-code">` sending the `--copy` invoker command to the block's `<pre>`, which takes an id from a hash of its text. The button is a `raw` node, so it reaches the HTML exactly as the markdown-it and rehype plugins write it. The buttons render `hidden`; the client script (`@mirrordown/sat-copy-code/client`) copies and shows them. ` ```sh nocopy ` leaves a block without one. Options set the button's icons and text. List it after plugins that replace code blocks, such as mermaid-pele. A factory, so each document's ids start fresh. The ids and the button are shared with the markdown-it and rehype plugins through `@mirrordown/core`.
|
|
6
|
+
*/
|
|
7
|
+
const satteriCopyCode = (options = {}) => {
|
|
8
|
+
const copyCodeButton = createCopyCodeButton(options);
|
|
9
|
+
return () => {
|
|
10
|
+
const ids = copyCodeIds();
|
|
11
|
+
return defineHastPlugin({
|
|
12
|
+
name: `copy-code`,
|
|
13
|
+
element: {
|
|
14
|
+
filter: [`pre`],
|
|
15
|
+
visit(node, ctx) {
|
|
16
|
+
const block = copyCodeBlock(node);
|
|
17
|
+
if (!block || copyCodeOptedOut(block.meta)) return;
|
|
18
|
+
const id = ids(block.text);
|
|
19
|
+
ctx.setProperty(node, `id`, id);
|
|
20
|
+
ctx.appendChild(node, {
|
|
21
|
+
type: `raw`,
|
|
22
|
+
value: copyCodeButton(id)
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
export { COPY_CODE_ICONS, COPY_CODE_TEXT, satteriCopyCode };
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import {\n COPY_CODE_ICONS,\n COPY_CODE_TEXT,\n copyCodeBlock,\n copyCodeIds,\n copyCodeOptedOut,\n createCopyCodeButton,\n type CopyCodeOptions,\n type CopyCodeState\n} from \"@mirrordown/core\";\nimport {\n defineHastPlugin,\n type HastVisitorInstance,\n type PluginFactoryContext\n} from \"satteri\";\n\nexport type { CopyCodeOptions, CopyCodeState };\nexport { COPY_CODE_ICONS, COPY_CODE_TEXT };\n\ntype ElementVisitor = Extract<\n NonNullable<HastVisitorInstance[\"element\"]>,\n { filter: string[] }\n>;\n\n/**\n * The plugin's definition: only the keys it defines, with a synchronous visitor, so `markdownToHtml` returns its result directly.\n */\nexport interface CopyCodePlugin {\n name: string;\n element: {\n filter: string[];\n visit: (...args: Parameters<ElementVisitor[\"visit\"]>) => void;\n };\n}\n\n/**\n * Sätteri plugin that adds a copy button to each code block, in its `<pre>` after the `<code>`: a `<button class=\"markdown-copy-code\">` sending the `--copy` invoker command to the block's `<pre>`, which takes an id from a hash of its text. The button is a `raw` node, so it reaches the HTML exactly as the markdown-it and rehype plugins write it. The buttons render `hidden`; the client script (`@mirrordown/sat-copy-code/client`) copies and shows them. ` ```sh nocopy ` leaves a block without one. Options set the button's icons and text. List it after plugins that replace code blocks, such as mermaid-pele. A factory, so each document's ids start fresh. The ids and the button are shared with the markdown-it and rehype plugins through `@mirrordown/core`.\n */\nexport const satteriCopyCode = (\n options: CopyCodeOptions = {}\n): ((ctx: PluginFactoryContext) => CopyCodePlugin) => {\n const copyCodeButton = createCopyCodeButton(options);\n return () => {\n const ids = copyCodeIds();\n return defineHastPlugin({\n name: `copy-code`,\n element: {\n filter: [`pre`],\n visit(node, ctx) {\n const block = copyCodeBlock(node);\n if (!block || copyCodeOptedOut(block.meta)) return;\n const id = ids(block.text);\n ctx.setProperty(node, `id`, id);\n ctx.appendChild(node, { type: `raw`, value: copyCodeButton(id) });\n }\n }\n });\n };\n};\n"],"mappings":";;;;;;AAsCA,MAAa,mBACX,UAA2B,CAAC,MACwB;CACpD,MAAM,iBAAiB,qBAAqB,OAAO;CACnD,aAAa;EACX,MAAM,MAAM,YAAY;EACxB,OAAO,iBAAiB;GACtB,MAAM;GACN,SAAS;IACP,QAAQ,CAAC,KAAK;IACd,MAAM,MAAM,KAAK;KACf,MAAM,QAAQ,cAAc,IAAI;KAChC,IAAI,CAAC,SAAS,iBAAiB,MAAM,IAAI,GAAG;KAC5C,MAAM,KAAK,IAAI,MAAM,IAAI;KACzB,IAAI,YAAY,MAAM,MAAM,EAAE;KAC9B,IAAI,YAAY,MAAM;MAAE,MAAM;MAAO,OAAO,eAAe,EAAE;KAAE,CAAC;IAClE;GACF;EACF,CAAC;CACH;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mirrordown/sat-copy-code",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "A Sätteri plugin to add a copy button to code blocks, as an invoker command, with a small client script that copies.",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Drake Costa <drake@saeris.io> (https://saeris.gg)",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/mirrordown/mirrordown.git"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"development": "./src/index.ts",
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"import": "./dist/index.mjs"
|
|
20
|
+
},
|
|
21
|
+
"./client": {
|
|
22
|
+
"development": "./src/client.ts",
|
|
23
|
+
"types": "./dist/client.d.mts",
|
|
24
|
+
"import": "./dist/client.mjs"
|
|
25
|
+
},
|
|
26
|
+
"./*.css": "./dist/*.css"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@mirrordown/core": "^0.1.3"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"satteri": "^0.11.5"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"satteri": "^0.10.5 || ^0.11.0"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=24.0.0"
|
|
39
|
+
},
|
|
40
|
+
"bumpy": {
|
|
41
|
+
"publishCommand": [
|
|
42
|
+
"node ../../scripts/sync-jsr-version.mjs",
|
|
43
|
+
"node ../../scripts/npm-publish.mjs",
|
|
44
|
+
"node ../../scripts/jsr-resolve-deps.mjs",
|
|
45
|
+
"node ../../scripts/jsr-publish.mjs"
|
|
46
|
+
]
|
|
47
|
+
}
|
|
6
48
|
}
|