nizel-plugin-code-copy 0.1.0 → 0.1.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/README.md +49 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/package.json +19 -7
- package/src/index.js +0 -30
- package/test/code-copy.test.mjs +0 -13
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# nizel-plugin-code-copy
|
|
2
|
+
|
|
3
|
+
CSP-friendly code copy markup plugin for [Nizel](https://npmjs.com/package/nizel).
|
|
4
|
+
|
|
5
|
+
Renders copy buttons on fenced code blocks without requiring inline JavaScript — works under strict Content Security Policy.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install nizel-plugin-code-copy
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { useNizel } from 'nizel';
|
|
17
|
+
import { codeCopyPlugin } from 'nizel-plugin-code-copy';
|
|
18
|
+
|
|
19
|
+
const processor = useNizel({
|
|
20
|
+
plugins: [
|
|
21
|
+
codeCopyPlugin({ label: 'Copy' }),
|
|
22
|
+
],
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const result = processor.process('```js\nconsole.log("hello")\n```');
|
|
26
|
+
// Produces a <figure> with a <button data-nizel-copy-button> and the code block
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Output Structure
|
|
30
|
+
|
|
31
|
+
```html
|
|
32
|
+
<figure class="nizel-code-copy" data-nizel-code-copy>
|
|
33
|
+
<figcaption>filename.js</figcaption> <!-- if filename provided -->
|
|
34
|
+
<button type="button" class="nizel-code-copy__button" data-nizel-copy-button>Copy</button>
|
|
35
|
+
<pre><code class="language-js">console.log("hello")</code></pre>
|
|
36
|
+
</figure>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Wire up the copy behavior with your own JS by querying `[data-nizel-copy-button]`.
|
|
40
|
+
|
|
41
|
+
## Options
|
|
42
|
+
|
|
43
|
+
| Option | Type | Default | Description |
|
|
44
|
+
| --- | --- | --- | --- |
|
|
45
|
+
| `label` | `string` | `'Copy'` | Button text |
|
|
46
|
+
|
|
47
|
+
## License
|
|
48
|
+
|
|
49
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type NizelCodeNode, type NizelPlugin, type NizelRenderContext } from 'nizel';
|
|
2
|
+
export type CodeCopyPluginOptions = {
|
|
3
|
+
label?: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Creates a plugin that renders CSP-friendly copy controls for fenced code blocks.
|
|
7
|
+
*/
|
|
8
|
+
export declare const codeCopyPlugin: (options?: CodeCopyPluginOptions) => NizelPlugin;
|
|
9
|
+
/**
|
|
10
|
+
* Renders the code-copy block wrapper for a parsed code block.
|
|
11
|
+
*/
|
|
12
|
+
export declare const renderCodeCopyBlock: (node: NizelCodeNode, ctx: NizelRenderContext, label: string) => string;
|
|
13
|
+
export default codeCopyPlugin;
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACxB,MAAM,OAAO,CAAC;AAEf,MAAM,MAAM,qBAAqB,GAAG;IAClC,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,UAAS,qBAA0B,KAAG,WAiBpE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAAI,MAAM,aAAa,EAAE,KAAK,kBAAkB,EAAE,OAAO,MAAM,KAAG,MAKjG,CAAC;AAEF,eAAe,cAAc,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { defineNizelPlugin, } from 'nizel';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a plugin that renders CSP-friendly copy controls for fenced code blocks.
|
|
4
|
+
*/
|
|
5
|
+
export const codeCopyPlugin = (options = {}) => {
|
|
6
|
+
const label = options.label ?? 'Copy';
|
|
7
|
+
return defineNizelPlugin({
|
|
8
|
+
name: 'code-copy',
|
|
9
|
+
blocks: {
|
|
10
|
+
code: {
|
|
11
|
+
name: 'code',
|
|
12
|
+
formats: {
|
|
13
|
+
html(node, ctx) {
|
|
14
|
+
if (node.type !== 'code')
|
|
15
|
+
return '';
|
|
16
|
+
return renderCodeCopyBlock(node, ctx, label);
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Renders the code-copy block wrapper for a parsed code block.
|
|
25
|
+
*/
|
|
26
|
+
export const renderCodeCopyBlock = (node, ctx, label) => {
|
|
27
|
+
const language = node.lang ? ` class="language-${ctx.escape(node.lang)}"` : '';
|
|
28
|
+
const filename = node.filename ? `<figcaption>${ctx.escape(node.filename)}</figcaption>` : '';
|
|
29
|
+
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>`;
|
|
30
|
+
};
|
|
31
|
+
export default codeCopyPlugin;
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,GAIlB,MAAM,OAAO,CAAC;AAMf;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAA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ly code copy markup plugin for Nizel.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/silvandiepen/nizel"
|
|
8
|
+
},
|
|
5
9
|
"type": "module",
|
|
6
|
-
"main": "./
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
7
12
|
"exports": {
|
|
8
|
-
".":
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
}
|
|
9
17
|
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
10
22
|
"scripts": {
|
|
11
|
-
"build": "
|
|
12
|
-
"typecheck": "
|
|
13
|
-
"test": "vitest run test/*.mjs",
|
|
14
|
-
"test:watch": "vitest test/*.mjs"
|
|
23
|
+
"build": "tsc -p tsconfig.json",
|
|
24
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
25
|
+
"test": "npm run build && vitest run test/*.mjs",
|
|
26
|
+
"test:watch": "npm run build && vitest test/*.mjs"
|
|
15
27
|
},
|
|
16
28
|
"peerDependencies": {
|
|
17
29
|
"nizel": "^0.1.0"
|
package/src/index.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
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;
|
package/test/code-copy.test.mjs
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
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
|
-
});
|