nizel-plugin-shiki 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 +23 -0
- package/src/index.js +49 -0
- package/test/shiki.test.mjs +14 -0
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nizel-plugin-shiki",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Optional code highlighting integration point 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,49 @@
|
|
|
1
|
+
import { defineNizelPlugin } from 'nizel';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a code highlighting plugin that accepts a Worker-compatible highlighter.
|
|
5
|
+
*/
|
|
6
|
+
export function shikiPlugin(options = {}) {
|
|
7
|
+
const mode = options.mode ?? 'blocks';
|
|
8
|
+
const highlighter = options.highlighter;
|
|
9
|
+
|
|
10
|
+
return defineNizelPlugin({
|
|
11
|
+
name: 'shiki',
|
|
12
|
+
blocks: {
|
|
13
|
+
code: {
|
|
14
|
+
name: 'code',
|
|
15
|
+
formats: {
|
|
16
|
+
html(node, ctx) {
|
|
17
|
+
if (node.type !== 'code') return '';
|
|
18
|
+
if (mode === 'inline') {
|
|
19
|
+
return fallback(node, ctx);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const highlighted =
|
|
23
|
+
typeof highlighter === 'function'
|
|
24
|
+
? highlighter(node.code, {
|
|
25
|
+
lang: node.lang,
|
|
26
|
+
theme: options.theme,
|
|
27
|
+
meta: node.meta,
|
|
28
|
+
filename: node.filename,
|
|
29
|
+
highlightLines: node.highlightLines,
|
|
30
|
+
})
|
|
31
|
+
: undefined;
|
|
32
|
+
|
|
33
|
+
return typeof highlighted === 'string' ? highlighted : fallback(node, ctx);
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Renders the safe core code block fallback when no highlighter is available.
|
|
43
|
+
*/
|
|
44
|
+
function fallback(node, ctx) {
|
|
45
|
+
const language = node.lang ? ` class="language-${ctx.escape(node.lang)}"` : '';
|
|
46
|
+
return `<pre><code${language}>${ctx.escape(node.code)}</code></pre>`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export default shikiPlugin;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { test } from 'vitest';
|
|
3
|
+
import { useNizel } from 'nizel';
|
|
4
|
+
import { shikiPlugin } from '../src/index.js';
|
|
5
|
+
|
|
6
|
+
test('uses a provided highlighter and falls back safely', async () => {
|
|
7
|
+
const highlighted = useNizel({
|
|
8
|
+
plugins: [shikiPlugin({ highlighter: (code) => `<pre class="hl">${code}</pre>` })],
|
|
9
|
+
});
|
|
10
|
+
const fallback = useNizel({ plugins: [shikiPlugin()] });
|
|
11
|
+
|
|
12
|
+
assert.match(await highlighted.html('```js\nx\n```'), /class="hl"/);
|
|
13
|
+
assert.match(await fallback.html('```js\nx\n```'), /language-js/);
|
|
14
|
+
});
|