nizel-plugin-code-copy 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/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "nizel-plugin-code-copy",
3
+ "version": "0.1.0",
4
+ "description": "CSP-friendly code copy markup plugin for Nizel.",
5
+ "type": "module",
6
+ "main": "./src/index.js",
7
+ "exports": {
8
+ ".": "./src/index.js"
9
+ },
10
+ "scripts": {
11
+ "build": "node --check src/index.js",
12
+ "typecheck": "node --check src/index.js",
13
+ "test": "vitest run test/*.mjs",
14
+ "test:watch": "vitest test/*.mjs"
15
+ },
16
+ "peerDependencies": {
17
+ "nizel": "^0.1.0"
18
+ },
19
+ "publishConfig": {
20
+ "access": "public",
21
+ "provenance": true
22
+ }
23
+ }
package/src/index.js ADDED
@@ -0,0 +1,30 @@
1
+ import { defineNizelPlugin } from 'nizel';
2
+
3
+ /**
4
+ * Creates a plugin that renders CSP-friendly copy controls for code blocks.
5
+ */
6
+ export function codeCopyPlugin(options = {}) {
7
+ const label = options.label ?? 'Copy';
8
+
9
+ return defineNizelPlugin({
10
+ name: 'code-copy',
11
+ blocks: {
12
+ code: {
13
+ name: 'code',
14
+ formats: {
15
+ html(node, ctx) {
16
+ if (node.type !== 'code') return '';
17
+ const language = node.lang ? ` class="language-${ctx.escape(node.lang)}"` : '';
18
+ const filename = node.filename
19
+ ? `<figcaption>${ctx.escape(node.filename)}</figcaption>`
20
+ : '';
21
+
22
+ return `<figure class="nizel-code-copy" data-nizel-code-copy>${filename}<button type="button" class="nizel-code-copy__button" data-nizel-copy-button>${ctx.escape(label)}</button><pre><code${language}>${ctx.escape(node.code)}</code></pre></figure>`;
23
+ },
24
+ },
25
+ },
26
+ },
27
+ });
28
+ }
29
+
30
+ export default codeCopyPlugin;
@@ -0,0 +1,13 @@
1
+ import assert from 'node:assert/strict';
2
+ import { test } from 'vitest';
3
+ import { useNizel } from 'nizel';
4
+ import { codeCopyPlugin } from '../src/index.js';
5
+
6
+ test('renders CSP-friendly copy markup for code blocks', async () => {
7
+ const nizel = useNizel({ plugins: [codeCopyPlugin()] });
8
+ const html = await nizel.html('```ts filename="demo.ts"\nconst x = 1;\n```');
9
+
10
+ assert.match(html, /data-nizel-code-copy/);
11
+ assert.match(html, /demo.ts/);
12
+ assert.doesNotMatch(html, /onclick=/);
13
+ });