nizel-plugin-shiki 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 ADDED
@@ -0,0 +1,79 @@
1
+ # nizel-plugin-shiki
2
+
3
+ Code syntax highlighting integration for [Nizel](https://npmjs.com/package/nizel).
4
+
5
+ Accepts a Worker-compatible highlighter function and passes code blocks through it. Falls back to plain `<pre><code>` when no highlighter is provided.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install nizel-plugin-shiki
11
+ ```
12
+
13
+ Install Shiki separately when you want Shiki-powered output:
14
+
15
+ ```bash
16
+ npm install shiki
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```js
22
+ import { useNizel } from 'nizel';
23
+ import { shikiPlugin } from 'nizel-plugin-shiki';
24
+ import { createHighlighter } from 'shiki';
25
+
26
+ const highlighter = await createHighlighter({
27
+ themes: ['github-dark'],
28
+ langs: ['javascript', 'typescript'],
29
+ });
30
+
31
+ const processor = useNizel({
32
+ plugins: [
33
+ shikiPlugin({
34
+ highlighter(code, { lang, theme }) {
35
+ return highlighter.codeToHtml(code, { lang, theme: theme || 'github-dark' });
36
+ },
37
+ theme: 'github-dark',
38
+ }),
39
+ ],
40
+ });
41
+
42
+ const result = processor.process('```js\nconsole.log("hello")\n```');
43
+ // Highlighted HTML from Shiki
44
+ ```
45
+
46
+ ## Worker and no-WASM usage
47
+
48
+ Use the `nizel-plugin-shiki/javascript` entrypoint when Shiki must run without the Oniguruma WASM engine, such as in Worker bundles.
49
+
50
+ ```js
51
+ import { useNizel } from 'nizel';
52
+ import { shikiPlugin } from 'nizel-plugin-shiki';
53
+ import { createJavaScriptShikiHighlighter } from 'nizel-plugin-shiki/javascript';
54
+
55
+ const highlighter = await createJavaScriptShikiHighlighter({
56
+ themes: ['github-dark'],
57
+ langs: ['javascript', 'typescript'],
58
+ defaultTheme: 'github-dark',
59
+ defaultLang: 'text',
60
+ });
61
+
62
+ const processor = useNizel({
63
+ plugins: [shikiPlugin({ highlighter })],
64
+ });
65
+ ```
66
+
67
+ The root `nizel-plugin-shiki` entrypoint does not import Shiki. The `/javascript` entrypoint imports Shiki with `shiki/engine/javascript`, so bundlers can select the JavaScript regex engine intentionally.
68
+
69
+ ## Options
70
+
71
+ | Option | Type | Default | Description |
72
+ | --- | --- | --- | --- |
73
+ | `highlighter` | `function` | — | Function receiving `(code, { lang, theme, meta, filename, highlightLines })`, returns HTML string |
74
+ | `theme` | `string` | — | Default theme name passed to the highlighter |
75
+ | `mode` | `'blocks' \| 'inline'` | `'blocks'` | Set to `'inline'` to skip highlighting (plain fallback) |
76
+
77
+ ## License
78
+
79
+ MIT
@@ -0,0 +1,23 @@
1
+ import { type NizelCodeNode, type NizelPlugin, type NizelRenderContext } from 'nizel';
2
+ export type ShikiHighlighterInput = {
3
+ lang?: string;
4
+ theme?: string;
5
+ meta?: string;
6
+ filename?: string;
7
+ highlightLines?: number[];
8
+ };
9
+ export type ShikiPluginOptions = {
10
+ highlighter?: (code: string, input: ShikiHighlighterInput) => string | undefined;
11
+ mode?: 'blocks' | 'inline';
12
+ theme?: string;
13
+ };
14
+ /**
15
+ * Creates a code highlighting plugin that accepts a Worker-compatible highlighter function.
16
+ */
17
+ export declare const shikiPlugin: (options?: ShikiPluginOptions) => NizelPlugin;
18
+ /**
19
+ * Renders the safe core code block fallback when no highlighter is available.
20
+ */
21
+ export declare const fallback: (node: NizelCodeNode, ctx: NizelRenderContext) => string;
22
+ export default shikiPlugin;
23
+ //# 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,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,qBAAqB,KAAK,MAAM,GAAG,SAAS,CAAC;IACjF,IAAI,CAAC,EAAE,QAAQ,GAAG,QAAQ,CAAC;IAC3B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,GAAI,UAAS,kBAAuB,KAAG,WA4B9D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,QAAQ,GAAI,MAAM,aAAa,EAAE,KAAK,kBAAkB,KAAG,MAGvE,CAAC;AAEF,eAAe,WAAW,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,41 @@
1
+ import { defineNizelPlugin, } from 'nizel';
2
+ /**
3
+ * Creates a code highlighting plugin that accepts a Worker-compatible highlighter function.
4
+ */
5
+ export const shikiPlugin = (options = {}) => {
6
+ const mode = options.mode ?? 'blocks';
7
+ const highlighter = options.highlighter;
8
+ return defineNizelPlugin({
9
+ name: 'shiki',
10
+ blocks: {
11
+ code: {
12
+ name: 'code',
13
+ formats: {
14
+ html(node, ctx) {
15
+ if (node.type !== 'code')
16
+ return '';
17
+ if (mode === 'inline')
18
+ return fallback(node, ctx);
19
+ const highlighted = highlighter?.(node.code, {
20
+ lang: node.lang,
21
+ theme: options.theme,
22
+ meta: node.meta,
23
+ filename: node.filename,
24
+ highlightLines: node.highlightLines,
25
+ });
26
+ return typeof highlighted === 'string' ? highlighted : fallback(node, ctx);
27
+ },
28
+ },
29
+ },
30
+ },
31
+ });
32
+ };
33
+ /**
34
+ * Renders the safe core code block fallback when no highlighter is available.
35
+ */
36
+ export const fallback = (node, ctx) => {
37
+ const language = node.lang ? ` class="language-${ctx.escape(node.lang)}"` : '';
38
+ return `<pre><code${language}>${ctx.escape(node.code)}</code></pre>`;
39
+ };
40
+ export default shikiPlugin;
41
+ //# 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;AAgBf;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,UAA8B,EAAE,EAAe,EAAE;IAC3E,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,QAAQ,CAAC;IACtC,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IAExC,OAAO,iBAAiB,CAAC;QACvB,IAAI,EAAE,OAAO;QACb,MAAM,EAAE;YACN,IAAI,EAAE;gBACJ,IAAI,EAAE,MAAM;gBACZ,OAAO,EAAE;oBACP,IAAI,CAAC,IAAI,EAAE,GAAG;wBACZ,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM;4BAAE,OAAO,EAAE,CAAC;wBACpC,IAAI,IAAI,KAAK,QAAQ;4BAAE,OAAO,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;wBAElD,MAAM,WAAW,GAAG,WAAW,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE;4BAC3C,IAAI,EAAE,IAAI,CAAC,IAAI;4BACf,KAAK,EAAE,OAAO,CAAC,KAAK;4BACpB,IAAI,EAAE,IAAI,CAAC,IAAI;4BACf,QAAQ,EAAE,IAAI,CAAC,QAAQ;4BACvB,cAAc,EAAE,IAAI,CAAC,cAAc;yBACpC,CAAC,CAAC;wBAEH,OAAO,OAAO,WAAW,KAAK,QAAQ,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;oBAC7E,CAAC;iBACF;aACF;SACF;KACF,CAAC,CAAC;AACL,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,IAAmB,EAAE,GAAuB,EAAU,EAAE;IAC/E,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,oBAAoB,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;IAC/E,OAAO,aAAa,QAAQ,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC;AACvE,CAAC,CAAC;AAEF,eAAe,WAAW,CAAC"}
@@ -0,0 +1,21 @@
1
+ import { type BundledHighlighterOptions } from 'shiki/core';
2
+ import type { ShikiHighlighterInput } from './index.js';
3
+ export type JavaScriptShikiHighlighterOptions = Omit<BundledHighlighterOptions<string, string>, 'engine'> & {
4
+ /**
5
+ * Language to use when a code fence does not declare one.
6
+ */
7
+ defaultLang?: string;
8
+ /**
9
+ * Theme to use when the plugin input does not provide one.
10
+ */
11
+ defaultTheme?: string;
12
+ };
13
+ export type JavaScriptShikiHighlighter = (code: string, input: ShikiHighlighterInput) => string | undefined;
14
+ /**
15
+ * Creates a Shiki highlighter callback that uses Shiki's JavaScript regex engine.
16
+ *
17
+ * Use this helper in Worker-style runtimes when bundling Shiki's default
18
+ * Oniguruma WASM engine is not desirable.
19
+ */
20
+ export declare const createJavaScriptShikiHighlighter: (options: JavaScriptShikiHighlighterOptions) => Promise<JavaScriptShikiHighlighter>;
21
+ //# sourceMappingURL=javascript.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"javascript.d.ts","sourceRoot":"","sources":["../src/javascript.ts"],"names":[],"mappings":"AAAA,OAAO,EAA4B,KAAK,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAItF,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAQxD,MAAM,MAAM,iCAAiC,GAAG,IAAI,CAClD,yBAAyB,CAAC,MAAM,EAAE,MAAM,CAAC,EACzC,QAAQ,CACT,GAAG;IACF;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG,CACvC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,qBAAqB,KACzB,MAAM,GAAG,SAAS,CAAC;AAQxB;;;;;GAKG;AACH,eAAO,MAAM,gCAAgC,GAC3C,SAAS,iCAAiC,KACzC,OAAO,CAAC,0BAA0B,CAcpC,CAAC"}
@@ -0,0 +1,33 @@
1
+ import { createBundledHighlighter } from 'shiki/core';
2
+ import { createJavaScriptRegexEngine } from 'shiki/engine/javascript';
3
+ import { bundledLanguages } from 'shiki/langs';
4
+ import { bundledThemes } from 'shiki/themes';
5
+ const createHighlighter = createBundledHighlighter({
6
+ langs: bundledLanguages,
7
+ themes: bundledThemes,
8
+ engine: createJavaScriptRegexEngine,
9
+ });
10
+ /**
11
+ * Returns the first string theme from a Shiki theme list.
12
+ */
13
+ const firstStringTheme = (themes) => themes.find((theme) => typeof theme === 'string');
14
+ /**
15
+ * Creates a Shiki highlighter callback that uses Shiki's JavaScript regex engine.
16
+ *
17
+ * Use this helper in Worker-style runtimes when bundling Shiki's default
18
+ * Oniguruma WASM engine is not desirable.
19
+ */
20
+ export const createJavaScriptShikiHighlighter = async (options) => {
21
+ const highlighter = await createHighlighter({
22
+ ...options,
23
+ engine: createJavaScriptRegexEngine(),
24
+ });
25
+ const defaultLang = options.defaultLang ?? 'text';
26
+ const defaultTheme = options.defaultTheme ?? firstStringTheme(options.themes) ?? 'github-dark';
27
+ return (code, input) => highlighter.codeToHtml(code, {
28
+ lang: input.lang ?? defaultLang,
29
+ theme: input.theme ?? defaultTheme,
30
+ meta: input.meta ? { __raw: input.meta } : undefined,
31
+ });
32
+ };
33
+ //# sourceMappingURL=javascript.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"javasc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AAC,CAAC;AACP,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,23 +1,50 @@
1
1
  {
2
2
  "name": "nizel-plugin-shiki",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Optional code highlighting integration point for Nizel.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/silvandiepen/nizel"
8
+ },
5
9
  "type": "module",
6
- "main": "./src/index.js",
10
+ "main": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
7
12
  "exports": {
8
- ".": "./src/index.js"
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js"
16
+ },
17
+ "./javascript": {
18
+ "types": "./dist/javascript.d.ts",
19
+ "import": "./dist/javascript.js"
20
+ }
9
21
  },
22
+ "files": [
23
+ "dist",
24
+ "README.md"
25
+ ],
10
26
  "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"
27
+ "build": "tsc -p tsconfig.json",
28
+ "typecheck": "tsc -p tsconfig.json --noEmit",
29
+ "test": "npm run build && vitest run test/*.mjs",
30
+ "test:watch": "npm run build && vitest test/*.mjs"
15
31
  },
16
32
  "peerDependencies": {
17
- "nizel": "^0.1.0"
33
+ "nizel": "^0.1.0",
34
+ "shiki": "^4.0.2"
35
+ },
36
+ "peerDependenciesMeta": {
37
+ "shiki": {
38
+ "optional": true
39
+ }
18
40
  },
19
41
  "publishConfig": {
20
42
  "access": "public",
21
43
  "provenance": true
44
+ },
45
+ "devDependencies": {
46
+ "esbuild": "^0.27.3",
47
+ "miniflare": "^4.20260430.0",
48
+ "shiki": "^4.0.2"
22
49
  }
23
50
  }
package/src/index.js DELETED
@@ -1,49 +0,0 @@
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;
@@ -1,14 +0,0 @@
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
- });