@devframes/service-shiki 0.9.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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-PRESENT Anthony Fu <https://github.com/antfu>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,68 @@
1
+ import { DevframeServiceDefinition } from "devframe/types";
2
+ import { codeToHast, codeToTokens } from "shiki";
3
+ //#region src/index.d.ts
4
+ declare const SHIKI_SERVICE_PACKAGE = "@devframes/service-shiki";
5
+ declare const SHIKI_SERVICE_SCOPE = "devframes:service:shiki";
6
+ /** Dual light/dark theme pair, rendered via Shiki's dual-theme CSS variables. */
7
+ interface ShikiThemes {
8
+ light: string;
9
+ dark: string;
10
+ }
11
+ /** Defaults matching the `@antfu/design` light/dark surfaces. */
12
+ declare const SHIKI_DEFAULT_THEMES: ShikiThemes;
13
+ interface ShikiServiceOptions {
14
+ /**
15
+ * Theme pair every request uses unless it carries its own. On merge, the
16
+ * later installer's pair wins.
17
+ */
18
+ themes?: ShikiThemes;
19
+ /**
20
+ * Languages to eagerly load at setup (others load on demand, per request).
21
+ * Merged as a union across installers.
22
+ */
23
+ langs?: string[];
24
+ }
25
+ interface ShikiHighlightInput {
26
+ code: string;
27
+ /** Language id; unknown ids degrade to plain text instead of throwing. */
28
+ lang?: string;
29
+ /** Per-request theme override. */
30
+ themes?: ShikiThemes;
31
+ }
32
+ type ShikiHast = Awaited<ReturnType<typeof codeToHast>>;
33
+ type ShikiTokens = Awaited<ReturnType<typeof codeToTokens>>;
34
+ interface ShikiServiceApi {
35
+ /** Highlight to HTML (dual-theme: light values inline, dark via `--shiki-dark` vars). */
36
+ highlight: (input: ShikiHighlightInput) => Promise<{
37
+ html: string;
38
+ }>;
39
+ /** Highlight to a HAST tree, for surfaces that render their own DOM. */
40
+ codeToHast: (input: ShikiHighlightInput) => Promise<ShikiHast>;
41
+ /** Highlight to themed tokens, for line-oriented renderers (e.g. diff views). */
42
+ codeToTokens: (input: ShikiHighlightInput) => Promise<ShikiTokens>;
43
+ }
44
+ declare module 'devframe' {
45
+ interface DevframeRpcServerFunctions {
46
+ 'devframes:service:shiki:highlight': (input: ShikiHighlightInput) => Promise<{
47
+ html: string;
48
+ }>;
49
+ 'devframes:service:shiki:code-to-hast': (input: ShikiHighlightInput) => Promise<ShikiHast>;
50
+ 'devframes:service:shiki:code-to-tokens': (input: ShikiHighlightInput) => Promise<ShikiTokens>;
51
+ }
52
+ interface DevframeServicesRegistry {
53
+ '@devframes/service-shiki': ShikiServiceApi;
54
+ }
55
+ interface DevframeServicesScopeRegistry {
56
+ '@devframes/service-shiki': 'devframes:service:shiki';
57
+ }
58
+ }
59
+ /**
60
+ * The Shiki wire service — server-side syntax highlighting shared by every
61
+ * plugin on the host, so client bundles stop shipping their own grammars and
62
+ * themes. Shiki itself loads lazily on first use; results are LRU-cached per
63
+ * `(code, lang, themes)` and every RPC function is `cacheable` on the client
64
+ * side too.
65
+ */
66
+ declare function createShikiService(options?: ShikiServiceOptions): DevframeServiceDefinition<ShikiServiceApi, ShikiServiceOptions>;
67
+ //#endregion
68
+ export { SHIKI_DEFAULT_THEMES, SHIKI_SERVICE_PACKAGE, SHIKI_SERVICE_SCOPE, ShikiHast, ShikiHighlightInput, ShikiServiceApi, ShikiServiceOptions, ShikiThemes, ShikiTokens, createShikiService, createShikiService as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,139 @@
1
+ import { defineRpcFunction } from "devframe";
2
+ import { hash } from "devframe/utils/hash";
3
+ import { s } from "devframe/utils/simple-schema";
4
+ //#region package.json
5
+ var version = "0.9.0";
6
+ //#endregion
7
+ //#region src/index.ts
8
+ const SHIKI_SERVICE_PACKAGE = "@devframes/service-shiki";
9
+ const SHIKI_SERVICE_SCOPE = "devframes:service:shiki";
10
+ /** Defaults matching the `@antfu/design` light/dark surfaces. */
11
+ const SHIKI_DEFAULT_THEMES = {
12
+ light: "vitesse-light",
13
+ dark: "vitesse-dark"
14
+ };
15
+ /** Tiny insertion-order LRU — enough to absorb re-renders of the same code. */
16
+ var Lru = class {
17
+ max;
18
+ map = /* @__PURE__ */ new Map();
19
+ constructor(max) {
20
+ this.max = max;
21
+ }
22
+ get(key) {
23
+ const value = this.map.get(key);
24
+ if (value !== void 0) {
25
+ this.map.delete(key);
26
+ this.map.set(key, value);
27
+ }
28
+ return value;
29
+ }
30
+ set(key, value) {
31
+ if (this.map.size >= this.max && !this.map.has(key)) this.map.delete(this.map.keys().next().value);
32
+ this.map.set(key, value);
33
+ }
34
+ };
35
+ const inputSchema = s.object({
36
+ code: s.string(),
37
+ lang: s.optional(s.string()),
38
+ themes: s.optional(s.object({
39
+ light: s.string(),
40
+ dark: s.string()
41
+ }))
42
+ });
43
+ /**
44
+ * The Shiki wire service — server-side syntax highlighting shared by every
45
+ * plugin on the host, so client bundles stop shipping their own grammars and
46
+ * themes. Shiki itself loads lazily on first use; results are LRU-cached per
47
+ * `(code, lang, themes)` and every RPC function is `cacheable` on the client
48
+ * side too.
49
+ */
50
+ function createShikiService(options) {
51
+ return {
52
+ package: SHIKI_SERVICE_PACKAGE,
53
+ version,
54
+ scope: SHIKI_SERVICE_SCOPE,
55
+ options,
56
+ setup(ctx, { options }) {
57
+ const defaultThemes = options?.themes ?? SHIKI_DEFAULT_THEMES;
58
+ let shikiPromise;
59
+ const shiki = () => shikiPromise ??= import("shiki").then(async (mod) => {
60
+ if (options?.langs?.length) await mod.getSingletonHighlighter({
61
+ langs: options.langs.filter((lang) => lang in mod.bundledLanguages),
62
+ themes: [defaultThemes.light, defaultThemes.dark]
63
+ });
64
+ return mod;
65
+ });
66
+ /** Unknown language ids degrade to plain text instead of throwing. */
67
+ async function resolveLang(lang) {
68
+ if (!lang) return "text";
69
+ return lang in (await shiki()).bundledLanguages || [
70
+ "text",
71
+ "plaintext",
72
+ "txt",
73
+ "plain",
74
+ "ansi"
75
+ ].includes(lang) ? lang : "text";
76
+ }
77
+ const cache = new Lru(256);
78
+ function cached(kind, input, compute) {
79
+ const themes = input.themes ?? defaultThemes;
80
+ const key = hash([
81
+ kind,
82
+ input.lang,
83
+ themes,
84
+ input.code
85
+ ]);
86
+ let result = cache.get(key);
87
+ if (!result) {
88
+ result = resolveLang(input.lang).then((lang) => compute(lang, themes));
89
+ cache.set(key, result);
90
+ }
91
+ return result;
92
+ }
93
+ const api = {
94
+ highlight: (input) => cached("html", input, async (lang, themes) => ({ html: await (await shiki()).codeToHtml(input.code, {
95
+ lang,
96
+ themes: { ...themes }
97
+ }) })),
98
+ codeToHast: (input) => cached("hast", input, async (lang, themes) => (await shiki()).codeToHast(input.code, {
99
+ lang,
100
+ themes: { ...themes }
101
+ })),
102
+ codeToTokens: (input) => cached("tokens", input, async (lang, themes) => (await shiki()).codeToTokens(input.code, {
103
+ lang,
104
+ themes: { ...themes }
105
+ }))
106
+ };
107
+ ctx.rpc.register(defineRpcFunction({
108
+ name: "highlight",
109
+ type: "query",
110
+ cacheable: true,
111
+ jsonSerializable: true,
112
+ args: [inputSchema],
113
+ returns: s.object({ html: s.string() }),
114
+ handler: (input) => api.highlight(input)
115
+ }));
116
+ ctx.rpc.register(defineRpcFunction({
117
+ name: "code-to-hast",
118
+ type: "query",
119
+ cacheable: true,
120
+ jsonSerializable: true,
121
+ args: [inputSchema],
122
+ returns: s.object({}),
123
+ handler: (input) => api.codeToHast(input)
124
+ }));
125
+ ctx.rpc.register(defineRpcFunction({
126
+ name: "code-to-tokens",
127
+ type: "query",
128
+ cacheable: true,
129
+ jsonSerializable: true,
130
+ args: [inputSchema],
131
+ returns: s.object({}),
132
+ handler: (input) => api.codeToTokens(input)
133
+ }));
134
+ return api;
135
+ }
136
+ };
137
+ }
138
+ //#endregion
139
+ export { SHIKI_DEFAULT_THEMES, SHIKI_SERVICE_PACKAGE, SHIKI_SERVICE_SCOPE, createShikiService, createShikiService as default };
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@devframes/service-shiki",
3
+ "type": "module",
4
+ "version": "0.9.0",
5
+ "description": "Devframe wire service that renders Shiki syntax highlighting on the server, so plugins stop re-bundling highlighters.",
6
+ "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/devframes/devframe#readme",
9
+ "repository": {
10
+ "directory": "services/shiki",
11
+ "type": "git",
12
+ "url": "git+https://github.com/devframes/devframe.git"
13
+ },
14
+ "bugs": "https://github.com/devframes/devframe/issues",
15
+ "keywords": [
16
+ "devframe",
17
+ "devframe-service",
18
+ "devtools",
19
+ "shiki",
20
+ "syntax-highlighting"
21
+ ],
22
+ "sideEffects": false,
23
+ "exports": {
24
+ ".": "./dist/index.mjs",
25
+ "./package.json": "./package.json"
26
+ },
27
+ "types": "./dist/index.d.mts",
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "peerDependencies": {
32
+ "devframe": "0.9.1"
33
+ },
34
+ "dependencies": {
35
+ "shiki": "^4.4.3"
36
+ },
37
+ "devDependencies": {
38
+ "@types/node": "^26.2.0",
39
+ "tsdown": "^0.22.14",
40
+ "vitest": "^4.1.10",
41
+ "devframe": "0.9.1"
42
+ },
43
+ "scripts": {
44
+ "build": "tsdown",
45
+ "watch": "tsdown --watch",
46
+ "test": "vitest run",
47
+ "typecheck": "tsc --noEmit"
48
+ }
49
+ }