@mirrordown/sat-copy-code 0.2.0 → 0.2.1

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 CHANGED
@@ -2,6 +2,23 @@
2
2
 
3
3
 
4
4
 
5
+
6
+ ## 0.2.1
7
+ <sub>2026-11-17</sub>
8
+
9
+ - *(patch)*
10
+ Fixes for the markdown-it plugin's handling of option text and of split parse and render calls, and for the client script; the markup for a code block with default options is unchanged.
11
+
12
+ - **Option text holding a `$` is written as it is** (markdown-it). With `text: { copy: "Copy $& all" }` the label pulled part of the code block's own HTML into the button (`aria-label="Copy </pre>…`), and `"Costs $$5"` came out as `Costs $5`. Both are now written as given, as in remark and Sätteri. The same goes for the `icons` option.
13
+
14
+ - **Buttons are added when tokens are rendered with a different `env` than they were parsed with** (markdown-it). `md.renderer.render(md.parse(src, {}), md.options, {})` rendered the code blocks without ids and without buttons; it now renders what `md.render(src)` does.
15
+
16
+ - **The client script does nothing when it is loaded a second time** (all engines). Loaded twice (from two of the packages, or again by a dev server), one press wrote the clipboard twice and the button kept the label "Copied!".
17
+
18
+ - **A button copies its own block when another element on the page has the block's id** (all engines). A heading whose slug equals a block's id (`code-…`), or the same block in a second document on the page, came first, and the button copied that element's text.
19
+
20
+ The guide now says that a block's id is unique per document rather than per page, and names file-tree beside mermaid-pele as a plugin to list before this one in remark and Sätteri.
21
+
5
22
  ## 0.2.0
6
23
  <sub>2026-11-11</sub>
7
24
 
package/README.md CHANGED
@@ -30,7 +30,7 @@ npm install @mirrordown/remd-copy-code
30
30
  ```
31
31
  ````
32
32
 
33
- 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
+ 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. Ids are unique per document, not per page: the same block in two documents rendered into one page has the same id in both, and so can a heading whose slug happens to match. The script then copies the block its button is in.
34
34
 
35
35
  ### Turning it off for a block
36
36
 
@@ -67,7 +67,7 @@ export default defineConfig({
67
67
 
68
68
  ## The client script
69
69
 
70
- Import the script once per page, from the package for your engine. It does three things:
70
+ Import the script once per page, from the package for your engine; a second copy on the page does nothing. It does three things:
71
71
 
72
72
  - **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.
73
73
  - **Copies.** It handles the `--copy` command by copying the text of the block's `<code>`, without the newline markdown ends a block with.
@@ -126,7 +126,7 @@ There's no VS Code extension for this plugin: VS Code's Markdown preview has had
126
126
  npm install @mirrordown/sat-copy-code
127
127
  ```
128
128
 
129
- List it after plugins that replace code blocks, such as mermaid-pele.
129
+ List it after plugins that replace code blocks, such as mermaid-pele and file-tree: listed before them, it gives their fences a button and they stay code blocks.
130
130
 
131
131
  ### Standalone
132
132
 
package/dist/client.mjs CHANGED
@@ -30,11 +30,16 @@ const copy = async (button, pre) => {
30
30
  button.setAttribute(`aria-label`, labels.get(button) ?? ``);
31
31
  }, COPIED_FOR));
32
32
  };
33
- if (`CommandEvent` in window && `clipboard` in navigator) {
33
+ const LOADED = Symbol.for(`mirrordown.copy-code`);
34
+ const page = document;
35
+ if (`CommandEvent` in window && `clipboard` in navigator && !page[LOADED]) {
36
+ page[LOADED] = true;
34
37
  document.addEventListener(`command`, (event) => {
35
38
  if (!(event instanceof CommandEvent) || event.command !== COMMAND) return;
36
39
  const { source, target } = event;
37
- if (source instanceof HTMLElement && target instanceof Element) copy(source, target);
40
+ if (!(source instanceof HTMLElement) || !(target instanceof Element)) return;
41
+ const pre = target.contains(source) ? target : source.closest(`pre`);
42
+ if (pre) copy(source, pre);
38
43
  }, { capture: true });
39
44
  const reveal = new CSSStyleSheet();
40
45
  reveal.replaceSync(`@layer markdown-copy-code { .markdown-copy-code[hidden] { display: flex; } }`);
@@ -1 +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"}
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// Set on the document by the first copy of this script to run. A page can load it twice (from two of the packages, a bundler's duplicate, a dev server's reload), and two listeners would write the clipboard twice and leave a button named for its copied state.\nconst LOADED = Symbol.for(`mirrordown.copy-code`);\nconst page: Document & { [LOADED]?: true } = document;\n\n// The clipboard is only there in secure contexts.\nif (`CommandEvent` in window && `clipboard` in navigator && !page[LOADED]) {\n page[LOADED] = true;\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 return;\n // `commandfor` finds the first element with the id, which is the button's own `<pre>` unless something before it on the page has the same one (a heading whose slug spells the hash, the same block in another document): the button is in its block, so that is the one it copies.\n const pre = target.contains(source) ? target : source.closest(`pre`);\n if (pre) void copy(source, pre);\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,MAA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B,CAAC,GAAG,SAAS,oBAAoB,MAAM;AACvE"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mirrordown/sat-copy-code",
3
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.2.0",
4
+ "version": "0.2.1",
5
5
  "license": "MIT",
6
6
  "author": "Drake Costa <drake@saeris.io> (https://saeris.gg)",
7
7
  "repository": {
@@ -26,7 +26,7 @@
26
26
  "./*.css": "./dist/*.css"
27
27
  },
28
28
  "dependencies": {
29
- "@mirrordown/core": "^0.1.4"
29
+ "@mirrordown/core": "^0.2.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "satteri": "^0.11.6"