nizel-plugin-code-copy 0.1.6 → 0.1.8
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/README.md +16 -5
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +27 -5
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# nizel-plugin-code-copy
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Code copy plugin for [Nizel](https://npmjs.com/package/nizel).
|
|
4
4
|
|
|
5
|
-
Renders copy buttons on fenced code blocks
|
|
5
|
+
Renders copy buttons on fenced code blocks. By default, buttons include inline JavaScript that copies the original source text.
|
|
6
6
|
|
|
7
7
|
The plugin wraps code-like blocks instead of replacing the active code renderer, so it can be combined with syntax highlighting or explicit diagram plugins.
|
|
8
8
|
|
|
@@ -25,7 +25,7 @@ const processor = useNizel({
|
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
const result = processor.process('```js\nconsole.log("hello")\n```');
|
|
28
|
-
// Produces a <figure> with a <button data-nizel-copy-button> and the code block
|
|
28
|
+
// Produces a <figure> with a copying <button data-nizel-copy-button> and the code block
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
## Output Structure
|
|
@@ -33,12 +33,21 @@ const result = processor.process('```js\nconsole.log("hello")\n```');
|
|
|
33
33
|
```html
|
|
34
34
|
<figure class="nizel-code-copy" data-nizel-code-copy>
|
|
35
35
|
<figcaption>filename.js</figcaption> <!-- if filename provided -->
|
|
36
|
-
<
|
|
36
|
+
<textarea class="nizel-code-copy__source" data-nizel-copy-source hidden readonly tabindex="-1" aria-hidden="true" style="display: none">console.log("hello")</textarea>
|
|
37
|
+
<button type="button" class="nizel-code-copy__button" data-nizel-copy-button onclick="...">Copy</button>
|
|
37
38
|
<pre><code class="language-js">console.log("hello")</code></pre>
|
|
38
39
|
</figure>
|
|
39
40
|
```
|
|
40
41
|
|
|
41
|
-
The
|
|
42
|
+
The hidden textarea contains the original escaped source text. The button copies from `[data-nizel-copy-source]`, so syntax highlighting or renderer changes do not affect the copied value.
|
|
43
|
+
|
|
44
|
+
For strict Content Security Policy or framework-owned behavior, switch to button-only mode:
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
codeCopyPlugin({ mode: 'button' });
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Then wire up copy behavior with your own JS by querying `[data-nizel-copy-button]`.
|
|
42
51
|
|
|
43
52
|
Because code-copy is a wrapper, this works with another plugin that renders the child code block:
|
|
44
53
|
|
|
@@ -59,6 +68,8 @@ Only explicit Mermaid fences such as ```` ```mermaid ```` become diagrams when t
|
|
|
59
68
|
| Option | Type | Default | Description |
|
|
60
69
|
| --- | --- | --- | --- |
|
|
61
70
|
| `label` | `string` | `'Copy'` | Button text |
|
|
71
|
+
| `copiedLabel` | `string` | `'Copied'` | Temporary button text after copying |
|
|
72
|
+
| `mode` | `'inline' \| 'button'` | `'inline'` | Inline copy handler or button-only markup |
|
|
62
73
|
|
|
63
74
|
## License
|
|
64
75
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { type NizelCustomBlockNode, type NizelPlugin, type NizelRenderContext, type NizelRootNode } from 'nizel';
|
|
2
2
|
export type CodeCopyPluginOptions = {
|
|
3
|
+
copiedLabel?: string;
|
|
3
4
|
label?: string;
|
|
5
|
+
mode?: 'inline' | 'button';
|
|
4
6
|
};
|
|
5
7
|
/**
|
|
6
|
-
* Creates a plugin that renders
|
|
8
|
+
* Creates a plugin that renders copy controls for fenced code blocks.
|
|
7
9
|
*/
|
|
8
10
|
export declare const codeCopyPlugin: (options?: CodeCopyPluginOptions) => NizelPlugin;
|
|
9
11
|
/**
|
|
@@ -13,6 +15,6 @@ export declare const wrapCodeBlocks: (ast: NizelRootNode) => NizelRootNode;
|
|
|
13
15
|
/**
|
|
14
16
|
* Renders the code-copy wrapper around already-rendered code-like content.
|
|
15
17
|
*/
|
|
16
|
-
export declare const renderCodeCopyBlock: (node: NizelCustomBlockNode, ctx: NizelRenderContext,
|
|
18
|
+
export declare const renderCodeCopyBlock: (node: NizelCustomBlockNode, ctx: NizelRenderContext, options: Required<CodeCopyPluginOptions>) => string;
|
|
17
19
|
export default codeCopyPlugin;
|
|
18
20
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,oBAAoB,EACzB,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACvB,KAAK,aAAa,EACnB,MAAM,OAAO,CAAC;AAEf,MAAM,MAAM,qBAAqB,GAAG;IAClC,KAAK,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,oBAAoB,EACzB,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACvB,KAAK,aAAa,EACnB,MAAM,OAAO,CAAC;AAEf,MAAM,MAAM,qBAAqB,GAAG;IAClC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,QAAQ,GAAG,QAAQ,CAAC;CAC5B,CAAC;AAQF;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,UAAS,qBAA0B,KAAG,WAsBpE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,KAAK,aAAa,KAAG,aAGlD,CAAC;AAgEH;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAC9B,MAAM,oBAAoB,EAC1B,KAAK,kBAAkB,EACvB,SAAS,QAAQ,CAAC,qBAAqB,CAAC,KACvC,MAYF,CAAC;AAsBF,eAAe,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { defineNizelPlugin, } from 'nizel';
|
|
2
2
|
/**
|
|
3
|
-
* Creates a plugin that renders
|
|
3
|
+
* Creates a plugin that renders copy controls for fenced code blocks.
|
|
4
4
|
*/
|
|
5
5
|
export const codeCopyPlugin = (options = {}) => {
|
|
6
|
+
const copiedLabel = options.copiedLabel ?? 'Copied';
|
|
6
7
|
const label = options.label ?? 'Copy';
|
|
8
|
+
const mode = options.mode ?? 'inline';
|
|
7
9
|
return defineNizelPlugin({
|
|
8
10
|
name: 'code-copy',
|
|
9
11
|
blocks: {
|
|
@@ -13,7 +15,7 @@ export const codeCopyPlugin = (options = {}) => {
|
|
|
13
15
|
html(node, ctx) {
|
|
14
16
|
if (node.type !== 'customBlock')
|
|
15
17
|
return '';
|
|
16
|
-
return renderCodeCopyBlock(node, ctx, label);
|
|
18
|
+
return renderCodeCopyBlock(node, ctx, { copiedLabel, label, mode });
|
|
17
19
|
},
|
|
18
20
|
},
|
|
19
21
|
},
|
|
@@ -87,14 +89,34 @@ const sourceFromCustomBlock = (node) => {
|
|
|
87
89
|
/**
|
|
88
90
|
* Renders the code-copy wrapper around already-rendered code-like content.
|
|
89
91
|
*/
|
|
90
|
-
export const renderCodeCopyBlock = (node, ctx,
|
|
92
|
+
export const renderCodeCopyBlock = (node, ctx, options) => {
|
|
91
93
|
const value = node.value;
|
|
92
94
|
const filename = value?.filename ? `<figcaption>${ctx.escape(value.filename)}</figcaption>` : '';
|
|
93
95
|
const source = typeof value?.code === 'string'
|
|
94
|
-
?
|
|
96
|
+
? `<textarea class="nizel-code-copy__source" data-nizel-copy-source hidden readonly tabindex="-1" aria-hidden="true" style="display: none">${ctx.escape(value.code)}</textarea>`
|
|
95
97
|
: '';
|
|
96
98
|
const body = ctx.render(node.children ?? []);
|
|
97
|
-
|
|
99
|
+
const onclick = options.mode === 'inline'
|
|
100
|
+
? ` onclick="${ctx.escape(createInlineCopyHandler(options.copiedLabel))}"`
|
|
101
|
+
: '';
|
|
102
|
+
return `<figure class="nizel-code-copy" data-nizel-code-copy>${filename}${source}<button type="button" class="nizel-code-copy__button" data-nizel-copy-button${onclick}>${ctx.escape(options.label)}</button>${body}</figure>`;
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Creates the inline browser copy handler used by the default copy mode.
|
|
106
|
+
*/
|
|
107
|
+
const createInlineCopyHandler = (copiedLabel) => {
|
|
108
|
+
const label = jsString(copiedLabel);
|
|
109
|
+
return `var b=this,f=b.closest('[data-nizel-code-copy]'),s=f?f.querySelector('[data-nizel-copy-source]'):null,v=s?s.value:'',p=b.textContent,d=function(){b.textContent='${label}';setTimeout(function(){b.textContent=p;},1200)},c=function(){var t=document.createElement('textarea');t.value=v;t.setAttribute('readonly','');t.style.position='fixed';t.style.opacity='0';document.body.appendChild(t);t.select();document.execCommand('copy');document.body.removeChild(t);d()};if(navigator.clipboard&&navigator.clipboard.writeText){navigator.clipboard.writeText(v).then(d,c)}else{c()}`;
|
|
98
110
|
};
|
|
111
|
+
/**
|
|
112
|
+
* Escapes a string for single-quoted JavaScript string literals.
|
|
113
|
+
*/
|
|
114
|
+
const jsString = (value) => value
|
|
115
|
+
.replace(/\\/g, '\\\\')
|
|
116
|
+
.replace(/'/g, "\\'")
|
|
117
|
+
.replace(/\r/g, '\\r')
|
|
118
|
+
.replace(/\n/g, '\\n')
|
|
119
|
+
.replace(/\u2028/g, '\\u2028')
|
|
120
|
+
.replace(/\u2029/g, '\\u2029');
|
|
99
121
|
export default codeCopyPlugin;
|
|
100
122
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,GAMlB,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mapp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AQ,CAAC,WAAW,CAAC,CAAC;IAEpC,OAAO,oKAAoK,KAAK,gZAAgZ,CAAC;AACnkB,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,QAAQ,GAAG,CAAC,KAAa,EAAU,EAAE,CAAC,KAAK;KAC9C,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC;KACtB,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC;KACpB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC;KACrB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC;KACrB,OAAO,CAAC,SAAS,EAAE,SAAS,CAAC;KAC7B,OAAO,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;AAEjC,eAAe,cAAc,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nizel-plugin-code-copy",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.8",
|
|
4
|
+
"description": "Code copy controls for Nizel.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "https://github.com/silvandiepen/nizel"
|