nizel-plugin-code-copy 0.1.4 → 0.1.6
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 +17 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +75 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,6 +4,8 @@ CSP-friendly code copy markup plugin for [Nizel](https://npmjs.com/package/nizel
|
|
|
4
4
|
|
|
5
5
|
Renders copy buttons on fenced code blocks without requiring inline JavaScript — works under strict Content Security Policy.
|
|
6
6
|
|
|
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
|
+
|
|
7
9
|
## Install
|
|
8
10
|
|
|
9
11
|
```bash
|
|
@@ -36,7 +38,21 @@ const result = processor.process('```js\nconsole.log("hello")\n```');
|
|
|
36
38
|
</figure>
|
|
37
39
|
```
|
|
38
40
|
|
|
39
|
-
Wire up the copy behavior with your own JS by querying `[data-nizel-copy-button]`.
|
|
41
|
+
The figure also includes `data-nizel-copy-source` with the original escaped source text. Wire up the copy behavior with your own JS by querying `[data-nizel-copy-button]`.
|
|
42
|
+
|
|
43
|
+
Because code-copy is a wrapper, this works with another plugin that renders the child code block:
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
useNizel({
|
|
47
|
+
plugins: [
|
|
48
|
+
shikiPlugin({ highlighter }),
|
|
49
|
+
diagramsPlugin(),
|
|
50
|
+
codeCopyPlugin(),
|
|
51
|
+
],
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Only explicit Mermaid fences such as ```` ```mermaid ```` become diagrams when the diagrams plugin is enabled.
|
|
40
56
|
|
|
41
57
|
## Options
|
|
42
58
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type NizelCustomBlockNode, type NizelPlugin, type NizelRenderContext, type NizelRootNode } from 'nizel';
|
|
2
2
|
export type CodeCopyPluginOptions = {
|
|
3
3
|
label?: string;
|
|
4
4
|
};
|
|
@@ -7,8 +7,12 @@ export type CodeCopyPluginOptions = {
|
|
|
7
7
|
*/
|
|
8
8
|
export declare const codeCopyPlugin: (options?: CodeCopyPluginOptions) => NizelPlugin;
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* Wraps source-bearing code blocks without replacing the active code renderer.
|
|
11
11
|
*/
|
|
12
|
-
export declare const
|
|
12
|
+
export declare const wrapCodeBlocks: (ast: NizelRootNode) => NizelRootNode;
|
|
13
|
+
/**
|
|
14
|
+
* Renders the code-copy wrapper around already-rendered code-like content.
|
|
15
|
+
*/
|
|
16
|
+
export declare const renderCodeCopyBlock: (node: NizelCustomBlockNode, ctx: NizelRenderContext, label: string) => string;
|
|
13
17
|
export default codeCopyPlugin;
|
|
14
18
|
//# 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,
|
|
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;CAChB,CAAC;AAQF;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,UAAS,qBAA0B,KAAG,WAoBpE,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,OAAO,MAAM,KACZ,MASF,CAAC;AAEF,eAAe,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -7,26 +7,94 @@ export const codeCopyPlugin = (options = {}) => {
|
|
|
7
7
|
return defineNizelPlugin({
|
|
8
8
|
name: 'code-copy',
|
|
9
9
|
blocks: {
|
|
10
|
-
code: {
|
|
11
|
-
name: 'code',
|
|
10
|
+
'code-copy': {
|
|
11
|
+
name: 'code-copy',
|
|
12
12
|
formats: {
|
|
13
13
|
html(node, ctx) {
|
|
14
|
-
if (node.type !== '
|
|
14
|
+
if (node.type !== 'customBlock')
|
|
15
15
|
return '';
|
|
16
16
|
return renderCodeCopyBlock(node, ctx, label);
|
|
17
17
|
},
|
|
18
18
|
},
|
|
19
19
|
},
|
|
20
20
|
},
|
|
21
|
+
hooks: {
|
|
22
|
+
afterParse: wrapCodeBlocks,
|
|
23
|
+
},
|
|
21
24
|
});
|
|
22
25
|
};
|
|
23
26
|
/**
|
|
24
|
-
*
|
|
27
|
+
* Wraps source-bearing code blocks without replacing the active code renderer.
|
|
28
|
+
*/
|
|
29
|
+
export const wrapCodeBlocks = (ast) => ({
|
|
30
|
+
...ast,
|
|
31
|
+
children: ast.children.map(wrapCodeBlock),
|
|
32
|
+
});
|
|
33
|
+
/**
|
|
34
|
+
* Wraps one block node and recurses into block containers.
|
|
35
|
+
*/
|
|
36
|
+
const wrapCodeBlock = (node) => {
|
|
37
|
+
if (node.type === 'code')
|
|
38
|
+
return createCodeCopyNode(node, node);
|
|
39
|
+
if (node.type === 'customBlock') {
|
|
40
|
+
if (node.name === 'code-copy')
|
|
41
|
+
return node;
|
|
42
|
+
const source = sourceFromCustomBlock(node);
|
|
43
|
+
if (source)
|
|
44
|
+
return createCodeCopyNode(source, node);
|
|
45
|
+
return node.children
|
|
46
|
+
? { ...node, children: node.children.map(wrapCodeBlock) }
|
|
47
|
+
: node;
|
|
48
|
+
}
|
|
49
|
+
if (node.type === 'blockquote') {
|
|
50
|
+
return { ...node, children: node.children.map(wrapCodeBlock) };
|
|
51
|
+
}
|
|
52
|
+
if (node.type === 'list') {
|
|
53
|
+
return {
|
|
54
|
+
...node,
|
|
55
|
+
children: node.children.map((item) => ({
|
|
56
|
+
...item,
|
|
57
|
+
children: item.children.map(wrapCodeBlock),
|
|
58
|
+
})),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
return node;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Creates a custom wrapper block that keeps original rendered content as a child.
|
|
65
|
+
*/
|
|
66
|
+
const createCodeCopyNode = (value, child) => ({
|
|
67
|
+
type: 'customBlock',
|
|
68
|
+
name: 'code-copy',
|
|
69
|
+
value,
|
|
70
|
+
children: [child],
|
|
71
|
+
});
|
|
72
|
+
/**
|
|
73
|
+
* Extracts copyable source from custom blocks that expose code-like values.
|
|
74
|
+
*/
|
|
75
|
+
const sourceFromCustomBlock = (node) => {
|
|
76
|
+
if (node.name !== 'diagram')
|
|
77
|
+
return undefined;
|
|
78
|
+
const value = node.value;
|
|
79
|
+
return typeof value?.code === 'string'
|
|
80
|
+
? {
|
|
81
|
+
code: value.code,
|
|
82
|
+
filename: value.filename,
|
|
83
|
+
lang: value.lang,
|
|
84
|
+
}
|
|
85
|
+
: undefined;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Renders the code-copy wrapper around already-rendered code-like content.
|
|
25
89
|
*/
|
|
26
90
|
export const renderCodeCopyBlock = (node, ctx, label) => {
|
|
27
|
-
const
|
|
28
|
-
const filename =
|
|
29
|
-
|
|
91
|
+
const value = node.value;
|
|
92
|
+
const filename = value?.filename ? `<figcaption>${ctx.escape(value.filename)}</figcaption>` : '';
|
|
93
|
+
const source = typeof value?.code === 'string'
|
|
94
|
+
? ` data-nizel-copy-source="${ctx.escape(value.code)}"`
|
|
95
|
+
: '';
|
|
96
|
+
const body = ctx.render(node.children ?? []);
|
|
97
|
+
return `<figure class="nizel-code-copy" data-nizel-code-copy${source}>${filename}<button type="button" class="nizel-code-copy__button" data-nizel-copy-button>${ctx.escape(label)}</button>${body}</figure>`;
|
|
30
98
|
};
|
|
31
99
|
export default codeCopyPlugin;
|
|
32
100
|
//# 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,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,GAMlB,MAAM,OAAO,CAAC;AAYf;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,UAAiC,EAAE,EAAe,EAAE;IACjF,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,MAAM,CAAC;IAEtC,OAAO,iBAAiB,CAAC;QACvB,IAAI,EAAE,WAAW;QACjB,MAAM,EAAE;YACN,WAAW,EAAE;gBACX,IAAI,EAAE,WAAW;gBACjB,OAAO,EAAE;oBACP,IAAI,CAAC,IAAI,EAAE,GAAG;wBACZ,IAAI,IAAI,CAAC,IAAI,KAAK,aAAa;4BAAE,OAAO,EAAE,CAAC;wBAC3C,OAAO,mBAAmB,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;oBAC/C,CAAC;iBACF;aACF;SACF;QACD,KAAK,EAAE;YACL,UAAU,EAAE,cAAc;SAC3B;KACF,CAAC,CAAC;AACL,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,GAAkB,EAAiB,EAAE,CAAC,CAAC;IACpE,GAAG,GAAG;IACN,QAAQ,EAAE,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,aAAa,CAAC;CAC1C,CAAC,CAAC;AAEH;;GAEG;AACH,MAAM,aAAa,GAAG,CAAC,IAAoB,EAAkB,EAAE;IAC7D,IAAI,I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