vscode-shiki-bridge 0.0.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/LICENSE +21 -0
- package/README.md +48 -0
- package/dist/index.cjs +219 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +201 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Yoav Balasiano
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# vscode-shiki-bridge
|
|
2
|
+
|
|
3
|
+
🌉 Extracts the user's VS Code theme and language grammars for Shiki
|
|
4
|
+
|
|
5
|
+
## Why?
|
|
6
|
+
|
|
7
|
+
VS Code doesn't provide a built-in way to render syntax-highlighted code blocks in webviews that match the user's current theme and installed language extensions. This library solves that by extracting the user's VS Code configuration (themes & language grammars) and passing it to Shiki, so you can render code blocks that look exactly like the editor.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install vscode-shiki-bridge shiki
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { createHighlighter } from "shiki";
|
|
19
|
+
import { getUserTheme, getUserLangs } from "vscode-shiki-bridge";
|
|
20
|
+
|
|
21
|
+
const [theme, themes] = await getUserTheme();
|
|
22
|
+
const langs = await getUserLangs(["graphql"]);
|
|
23
|
+
// Create Shiki highlighter with the extracted themes and langs
|
|
24
|
+
const highlighter = await createHighlighter({ themes, langs });
|
|
25
|
+
|
|
26
|
+
// Highlight GraphQL code with the user's theme
|
|
27
|
+
const html = highlighter.codeToHtml(
|
|
28
|
+
`type User {
|
|
29
|
+
name: String
|
|
30
|
+
age: Int
|
|
31
|
+
}`,
|
|
32
|
+
{
|
|
33
|
+
lang: "graphql",
|
|
34
|
+
theme,
|
|
35
|
+
}
|
|
36
|
+
);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Development and Debug
|
|
40
|
+
|
|
41
|
+
1. Open the project in VS Code / Cursor
|
|
42
|
+
2. Press `F5` to start debugging (opens a new VS Code window with the example extension)
|
|
43
|
+
3. Run the "Shiki Preview" command from the Command Palette to see your highlighted code blocks
|
|
44
|
+
4. Make changes and reload the window to test
|
|
45
|
+
|
|
46
|
+
## License
|
|
47
|
+
|
|
48
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
const jsonc_parser = __toESM(require("jsonc-parser"));
|
|
25
|
+
|
|
26
|
+
//#region src/vscode-utils.ts
|
|
27
|
+
function getVscode() {
|
|
28
|
+
try {
|
|
29
|
+
return require("vscode");
|
|
30
|
+
} catch {
|
|
31
|
+
throw new Error("vscode-shiki-bridge: The 'vscode' API is only available inside the VS Code extension host.");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/user-theme.ts
|
|
37
|
+
/**
|
|
38
|
+
* Read the currently selected user theme's theme JSON.
|
|
39
|
+
* Returns null when not running inside VS Code, no theme is configured,
|
|
40
|
+
* or the theme file cannot be found/read.
|
|
41
|
+
*/
|
|
42
|
+
async function getUserTheme() {
|
|
43
|
+
try {
|
|
44
|
+
const vscode = getVscode();
|
|
45
|
+
const workbenchConfig = vscode.workspace.getConfiguration("workbench");
|
|
46
|
+
const themeName = workbenchConfig.get("colorTheme");
|
|
47
|
+
if (!themeName) return THEME_NOT_FOUND_RESULT;
|
|
48
|
+
const decoder = new TextDecoder("utf-8");
|
|
49
|
+
for (const extension of vscode.extensions.all) {
|
|
50
|
+
const contributions = extension.packageJSON?.contributes?.themes ?? [];
|
|
51
|
+
const matchedTheme = contributions.find((contribution) => contribution.path && (contribution.id === themeName || contribution.label === themeName));
|
|
52
|
+
if (!matchedTheme) continue;
|
|
53
|
+
try {
|
|
54
|
+
const themeUri = vscode.Uri.joinPath(extension.extensionUri, matchedTheme.path);
|
|
55
|
+
const rawBytes = await vscode.workspace.fs.readFile(themeUri);
|
|
56
|
+
const jsonText = decoder.decode(rawBytes);
|
|
57
|
+
const json = (0, jsonc_parser.parse)(jsonText);
|
|
58
|
+
if (json.name) return [json.name, [json]];
|
|
59
|
+
return THEME_NOT_FOUND_RESULT;
|
|
60
|
+
} catch {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return THEME_NOT_FOUND_RESULT;
|
|
65
|
+
} catch {
|
|
66
|
+
return THEME_NOT_FOUND_RESULT;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const THEME_NOT_FOUND_RESULT = ["none", []];
|
|
70
|
+
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/user-language-inference.ts
|
|
73
|
+
/**
|
|
74
|
+
* This function is vibe coded, I should revisit it in the future.
|
|
75
|
+
* Infer required builtin Shiki language ids from a list of extension grammars.
|
|
76
|
+
*
|
|
77
|
+
* This inspects `embeddedLangs` provided by the collector and scans the grammar
|
|
78
|
+
* `patterns`, `repository`, and `injections` for `include` targets like
|
|
79
|
+
* `source.ts#...`, mapping known scopes to Shiki builtin language ids
|
|
80
|
+
* (e.g., `source.ts` -> `typescript`).
|
|
81
|
+
*
|
|
82
|
+
* Returns a deduped array of builtin language ids in lowercase, excluding
|
|
83
|
+
* any languages that are already present in the given `langs` by name or alias.
|
|
84
|
+
*/
|
|
85
|
+
function inferBuiltinLanguageIds(langs) {
|
|
86
|
+
const scopeToBuiltinLang = {
|
|
87
|
+
"source.ts": "typescript",
|
|
88
|
+
"source.tsx": "tsx",
|
|
89
|
+
"source.js": "javascript",
|
|
90
|
+
"source.jsx": "jsx",
|
|
91
|
+
"source.json": "json",
|
|
92
|
+
"text.html.basic": "html",
|
|
93
|
+
"source.css": "css",
|
|
94
|
+
"source.scss": "scss",
|
|
95
|
+
"source.sass": "sass",
|
|
96
|
+
"source.less": "less",
|
|
97
|
+
"source.graphql": "graphql",
|
|
98
|
+
"source.yaml": "yaml",
|
|
99
|
+
"source.toml": "toml",
|
|
100
|
+
"source.rust": "rust",
|
|
101
|
+
"source.python": "python",
|
|
102
|
+
"source.java": "java",
|
|
103
|
+
"source.go": "go",
|
|
104
|
+
"source.cpp": "cpp",
|
|
105
|
+
"source.c": "c",
|
|
106
|
+
"source.swift": "swift",
|
|
107
|
+
"source.kotlin": "kotlin",
|
|
108
|
+
"source.shell": "bash",
|
|
109
|
+
"source.bash": "bash",
|
|
110
|
+
"source.diff": "diff",
|
|
111
|
+
"text.xml": "xml",
|
|
112
|
+
"text.markdown": "markdown"
|
|
113
|
+
};
|
|
114
|
+
const presentLanguageNames = new Set();
|
|
115
|
+
for (const entry of langs) {
|
|
116
|
+
const candidates = [entry.name, ...entry.aliases ?? []].filter(Boolean).map((x) => x.toLowerCase());
|
|
117
|
+
for (const c of candidates) presentLanguageNames.add(c);
|
|
118
|
+
}
|
|
119
|
+
function extractIncludeTargets(grammar) {
|
|
120
|
+
const targets = new Set();
|
|
121
|
+
function visit(node) {
|
|
122
|
+
if (!node || typeof node !== "object") return;
|
|
123
|
+
const objectNode = node;
|
|
124
|
+
const include = objectNode.include;
|
|
125
|
+
if (typeof include === "string") targets.add(include);
|
|
126
|
+
const patterns = Array.isArray(objectNode.patterns) ? objectNode.patterns : [];
|
|
127
|
+
for (const p of patterns) visit(p);
|
|
128
|
+
const repository = objectNode.repository;
|
|
129
|
+
if (repository && typeof repository === "object") for (const key of Object.keys(repository)) visit(repository[key]);
|
|
130
|
+
}
|
|
131
|
+
visit(grammar);
|
|
132
|
+
const injections = grammar?.injections;
|
|
133
|
+
if (injections && typeof injections === "object") for (const key of Object.keys(injections)) visit(injections[key]);
|
|
134
|
+
return targets;
|
|
135
|
+
}
|
|
136
|
+
const inferred = new Set();
|
|
137
|
+
for (const entry of langs) {
|
|
138
|
+
for (const embedded of entry.embeddedLangs ?? []) if (typeof embedded === "string") inferred.add(embedded.toLowerCase());
|
|
139
|
+
const includeTargets = extractIncludeTargets(entry);
|
|
140
|
+
for (const inc of includeTargets) {
|
|
141
|
+
if (inc.startsWith("#")) continue;
|
|
142
|
+
const scope = inc.split("#", 1)[0];
|
|
143
|
+
const mapped = scopeToBuiltinLang[scope];
|
|
144
|
+
if (mapped) inferred.add(mapped.toLowerCase());
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const result = [];
|
|
148
|
+
for (const langName of inferred) if (!presentLanguageNames.has(langName)) result.push(langName);
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region src/user-languages.ts
|
|
154
|
+
/**
|
|
155
|
+
* Collect TextMate grammars contributed by installed VS Code extensions.
|
|
156
|
+
* @param langIds - If provided, only loads grammars for those specific language IDs.
|
|
157
|
+
*/
|
|
158
|
+
async function getUserLangs(langIds) {
|
|
159
|
+
try {
|
|
160
|
+
const vscode = getVscode();
|
|
161
|
+
const decoder = new TextDecoder("utf-8");
|
|
162
|
+
const extensionLangs = [];
|
|
163
|
+
const seenScope = new Set();
|
|
164
|
+
const normalizedLangIds = langIds?.map((id) => id.toLowerCase());
|
|
165
|
+
for (const extension of vscode.extensions.all) {
|
|
166
|
+
const contributes = extension.packageJSON?.contributes ?? {};
|
|
167
|
+
if (!contributes.grammars || contributes.grammars.length === 0) continue;
|
|
168
|
+
const languageIdToAliases = new Map();
|
|
169
|
+
for (const lang of contributes.languages ?? []) if (lang?.id) languageIdToAliases.set(lang.id, lang.aliases ?? []);
|
|
170
|
+
if (normalizedLangIds) {
|
|
171
|
+
const hasMatchingGrammar = contributes.grammars.some((grammar) => {
|
|
172
|
+
if (!grammar?.language) return false;
|
|
173
|
+
const names = [grammar.language, ...languageIdToAliases.get(grammar.language) ?? []].filter(Boolean).map((name) => name.toLowerCase());
|
|
174
|
+
return normalizedLangIds.some((desiredName) => names.includes(desiredName));
|
|
175
|
+
});
|
|
176
|
+
if (!hasMatchingGrammar) continue;
|
|
177
|
+
}
|
|
178
|
+
for (const grammar of contributes.grammars) {
|
|
179
|
+
if (!grammar?.path || !grammar.scopeName) continue;
|
|
180
|
+
if (!/\.json$/i.test(grammar.path)) continue;
|
|
181
|
+
if (!grammar.language) continue;
|
|
182
|
+
if (seenScope.has(grammar.scopeName)) continue;
|
|
183
|
+
if (normalizedLangIds) {
|
|
184
|
+
const names = [grammar.language, ...languageIdToAliases.get(grammar.language) ?? []].filter(Boolean).map((name) => name.toLowerCase());
|
|
185
|
+
const match = normalizedLangIds.find((desiredName) => names.includes(desiredName));
|
|
186
|
+
if (!match) continue;
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
const uri = vscode.Uri.joinPath(extension.extensionUri, grammar.path);
|
|
190
|
+
const raw = await vscode.workspace.fs.readFile(uri);
|
|
191
|
+
const jsonText = decoder.decode(raw);
|
|
192
|
+
const grammarJson = (0, jsonc_parser.parse)(jsonText);
|
|
193
|
+
const embeddedLangs = grammar.embeddedLanguages ? Array.from(new Set(Object.values(grammar.embeddedLanguages).filter(Boolean))) : void 0;
|
|
194
|
+
seenScope.add(grammar.scopeName);
|
|
195
|
+
extensionLangs.push({
|
|
196
|
+
...grammarJson,
|
|
197
|
+
name: grammar.language,
|
|
198
|
+
embeddedLangs,
|
|
199
|
+
aliases: languageIdToAliases.get(grammar.language) ?? void 0
|
|
200
|
+
});
|
|
201
|
+
} catch {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (normalizedLangIds) {
|
|
207
|
+
const builtinLangs = inferBuiltinLanguageIds(extensionLangs);
|
|
208
|
+
return [...extensionLangs, ...builtinLangs];
|
|
209
|
+
}
|
|
210
|
+
return extensionLangs;
|
|
211
|
+
} catch {
|
|
212
|
+
return [];
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
//#endregion
|
|
217
|
+
exports.getUserLangs = getUserLangs;
|
|
218
|
+
exports.getUserTheme = getUserTheme;
|
|
219
|
+
exports.inferBuiltinLanguageIds = inferBuiltinLanguageIds;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { LanguageRegistration, ThemeRegistrationAny } from "shiki/types";
|
|
2
|
+
|
|
3
|
+
//#region src/user-theme.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Read the currently selected user theme's theme JSON.
|
|
7
|
+
* Returns null when not running inside VS Code, no theme is configured,
|
|
8
|
+
* or the theme file cannot be found/read.
|
|
9
|
+
*/
|
|
10
|
+
declare function getUserTheme(): Promise<[string, ThemeRegistrationAny[]]>;
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/user-languages.d.ts
|
|
13
|
+
/**
|
|
14
|
+
* Collect TextMate grammars contributed by installed VS Code extensions.
|
|
15
|
+
* @param langIds - If provided, only loads grammars for those specific language IDs.
|
|
16
|
+
*/
|
|
17
|
+
declare function getUserLangs(langIds?: string[]): Promise<(string | LanguageRegistration)[]>;
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/user-language-inference.d.ts
|
|
20
|
+
/**
|
|
21
|
+
* This function is vibe coded, I should revisit it in the future.
|
|
22
|
+
* Infer required builtin Shiki language ids from a list of extension grammars.
|
|
23
|
+
*
|
|
24
|
+
* This inspects `embeddedLangs` provided by the collector and scans the grammar
|
|
25
|
+
* `patterns`, `repository`, and `injections` for `include` targets like
|
|
26
|
+
* `source.ts#...`, mapping known scopes to Shiki builtin language ids
|
|
27
|
+
* (e.g., `source.ts` -> `typescript`).
|
|
28
|
+
*
|
|
29
|
+
* Returns a deduped array of builtin language ids in lowercase, excluding
|
|
30
|
+
* any languages that are already present in the given `langs` by name or alias.
|
|
31
|
+
*/
|
|
32
|
+
declare function inferBuiltinLanguageIds(langs: LanguageRegistration[]): string[];
|
|
33
|
+
//#endregion
|
|
34
|
+
export { getUserLangs, getUserTheme, inferBuiltinLanguageIds };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { parse } from "jsonc-parser";
|
|
2
|
+
|
|
3
|
+
//#region rolldown:runtime
|
|
4
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { get: (a, b) => (typeof require !== "undefined" ? require : a)[b] }) : x)(function(x) {
|
|
5
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
6
|
+
throw Error("Calling `require` for \"" + x + "\" in an environment that doesn't expose the `require` function.");
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/vscode-utils.ts
|
|
11
|
+
function getVscode() {
|
|
12
|
+
try {
|
|
13
|
+
return __require("vscode");
|
|
14
|
+
} catch {
|
|
15
|
+
throw new Error("vscode-shiki-bridge: The 'vscode' API is only available inside the VS Code extension host.");
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/user-theme.ts
|
|
21
|
+
/**
|
|
22
|
+
* Read the currently selected user theme's theme JSON.
|
|
23
|
+
* Returns null when not running inside VS Code, no theme is configured,
|
|
24
|
+
* or the theme file cannot be found/read.
|
|
25
|
+
*/
|
|
26
|
+
async function getUserTheme() {
|
|
27
|
+
try {
|
|
28
|
+
const vscode = getVscode();
|
|
29
|
+
const workbenchConfig = vscode.workspace.getConfiguration("workbench");
|
|
30
|
+
const themeName = workbenchConfig.get("colorTheme");
|
|
31
|
+
if (!themeName) return THEME_NOT_FOUND_RESULT;
|
|
32
|
+
const decoder = new TextDecoder("utf-8");
|
|
33
|
+
for (const extension of vscode.extensions.all) {
|
|
34
|
+
const contributions = extension.packageJSON?.contributes?.themes ?? [];
|
|
35
|
+
const matchedTheme = contributions.find((contribution) => contribution.path && (contribution.id === themeName || contribution.label === themeName));
|
|
36
|
+
if (!matchedTheme) continue;
|
|
37
|
+
try {
|
|
38
|
+
const themeUri = vscode.Uri.joinPath(extension.extensionUri, matchedTheme.path);
|
|
39
|
+
const rawBytes = await vscode.workspace.fs.readFile(themeUri);
|
|
40
|
+
const jsonText = decoder.decode(rawBytes);
|
|
41
|
+
const json = parse(jsonText);
|
|
42
|
+
if (json.name) return [json.name, [json]];
|
|
43
|
+
return THEME_NOT_FOUND_RESULT;
|
|
44
|
+
} catch {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return THEME_NOT_FOUND_RESULT;
|
|
49
|
+
} catch {
|
|
50
|
+
return THEME_NOT_FOUND_RESULT;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const THEME_NOT_FOUND_RESULT = ["none", []];
|
|
54
|
+
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/user-language-inference.ts
|
|
57
|
+
/**
|
|
58
|
+
* This function is vibe coded, I should revisit it in the future.
|
|
59
|
+
* Infer required builtin Shiki language ids from a list of extension grammars.
|
|
60
|
+
*
|
|
61
|
+
* This inspects `embeddedLangs` provided by the collector and scans the grammar
|
|
62
|
+
* `patterns`, `repository`, and `injections` for `include` targets like
|
|
63
|
+
* `source.ts#...`, mapping known scopes to Shiki builtin language ids
|
|
64
|
+
* (e.g., `source.ts` -> `typescript`).
|
|
65
|
+
*
|
|
66
|
+
* Returns a deduped array of builtin language ids in lowercase, excluding
|
|
67
|
+
* any languages that are already present in the given `langs` by name or alias.
|
|
68
|
+
*/
|
|
69
|
+
function inferBuiltinLanguageIds(langs) {
|
|
70
|
+
const scopeToBuiltinLang = {
|
|
71
|
+
"source.ts": "typescript",
|
|
72
|
+
"source.tsx": "tsx",
|
|
73
|
+
"source.js": "javascript",
|
|
74
|
+
"source.jsx": "jsx",
|
|
75
|
+
"source.json": "json",
|
|
76
|
+
"text.html.basic": "html",
|
|
77
|
+
"source.css": "css",
|
|
78
|
+
"source.scss": "scss",
|
|
79
|
+
"source.sass": "sass",
|
|
80
|
+
"source.less": "less",
|
|
81
|
+
"source.graphql": "graphql",
|
|
82
|
+
"source.yaml": "yaml",
|
|
83
|
+
"source.toml": "toml",
|
|
84
|
+
"source.rust": "rust",
|
|
85
|
+
"source.python": "python",
|
|
86
|
+
"source.java": "java",
|
|
87
|
+
"source.go": "go",
|
|
88
|
+
"source.cpp": "cpp",
|
|
89
|
+
"source.c": "c",
|
|
90
|
+
"source.swift": "swift",
|
|
91
|
+
"source.kotlin": "kotlin",
|
|
92
|
+
"source.shell": "bash",
|
|
93
|
+
"source.bash": "bash",
|
|
94
|
+
"source.diff": "diff",
|
|
95
|
+
"text.xml": "xml",
|
|
96
|
+
"text.markdown": "markdown"
|
|
97
|
+
};
|
|
98
|
+
const presentLanguageNames = new Set();
|
|
99
|
+
for (const entry of langs) {
|
|
100
|
+
const candidates = [entry.name, ...entry.aliases ?? []].filter(Boolean).map((x) => x.toLowerCase());
|
|
101
|
+
for (const c of candidates) presentLanguageNames.add(c);
|
|
102
|
+
}
|
|
103
|
+
function extractIncludeTargets(grammar) {
|
|
104
|
+
const targets = new Set();
|
|
105
|
+
function visit(node) {
|
|
106
|
+
if (!node || typeof node !== "object") return;
|
|
107
|
+
const objectNode = node;
|
|
108
|
+
const include = objectNode.include;
|
|
109
|
+
if (typeof include === "string") targets.add(include);
|
|
110
|
+
const patterns = Array.isArray(objectNode.patterns) ? objectNode.patterns : [];
|
|
111
|
+
for (const p of patterns) visit(p);
|
|
112
|
+
const repository = objectNode.repository;
|
|
113
|
+
if (repository && typeof repository === "object") for (const key of Object.keys(repository)) visit(repository[key]);
|
|
114
|
+
}
|
|
115
|
+
visit(grammar);
|
|
116
|
+
const injections = grammar?.injections;
|
|
117
|
+
if (injections && typeof injections === "object") for (const key of Object.keys(injections)) visit(injections[key]);
|
|
118
|
+
return targets;
|
|
119
|
+
}
|
|
120
|
+
const inferred = new Set();
|
|
121
|
+
for (const entry of langs) {
|
|
122
|
+
for (const embedded of entry.embeddedLangs ?? []) if (typeof embedded === "string") inferred.add(embedded.toLowerCase());
|
|
123
|
+
const includeTargets = extractIncludeTargets(entry);
|
|
124
|
+
for (const inc of includeTargets) {
|
|
125
|
+
if (inc.startsWith("#")) continue;
|
|
126
|
+
const scope = inc.split("#", 1)[0];
|
|
127
|
+
const mapped = scopeToBuiltinLang[scope];
|
|
128
|
+
if (mapped) inferred.add(mapped.toLowerCase());
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const result = [];
|
|
132
|
+
for (const langName of inferred) if (!presentLanguageNames.has(langName)) result.push(langName);
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
//#endregion
|
|
137
|
+
//#region src/user-languages.ts
|
|
138
|
+
/**
|
|
139
|
+
* Collect TextMate grammars contributed by installed VS Code extensions.
|
|
140
|
+
* @param langIds - If provided, only loads grammars for those specific language IDs.
|
|
141
|
+
*/
|
|
142
|
+
async function getUserLangs(langIds) {
|
|
143
|
+
try {
|
|
144
|
+
const vscode = getVscode();
|
|
145
|
+
const decoder = new TextDecoder("utf-8");
|
|
146
|
+
const extensionLangs = [];
|
|
147
|
+
const seenScope = new Set();
|
|
148
|
+
const normalizedLangIds = langIds?.map((id) => id.toLowerCase());
|
|
149
|
+
for (const extension of vscode.extensions.all) {
|
|
150
|
+
const contributes = extension.packageJSON?.contributes ?? {};
|
|
151
|
+
if (!contributes.grammars || contributes.grammars.length === 0) continue;
|
|
152
|
+
const languageIdToAliases = new Map();
|
|
153
|
+
for (const lang of contributes.languages ?? []) if (lang?.id) languageIdToAliases.set(lang.id, lang.aliases ?? []);
|
|
154
|
+
if (normalizedLangIds) {
|
|
155
|
+
const hasMatchingGrammar = contributes.grammars.some((grammar) => {
|
|
156
|
+
if (!grammar?.language) return false;
|
|
157
|
+
const names = [grammar.language, ...languageIdToAliases.get(grammar.language) ?? []].filter(Boolean).map((name) => name.toLowerCase());
|
|
158
|
+
return normalizedLangIds.some((desiredName) => names.includes(desiredName));
|
|
159
|
+
});
|
|
160
|
+
if (!hasMatchingGrammar) continue;
|
|
161
|
+
}
|
|
162
|
+
for (const grammar of contributes.grammars) {
|
|
163
|
+
if (!grammar?.path || !grammar.scopeName) continue;
|
|
164
|
+
if (!/\.json$/i.test(grammar.path)) continue;
|
|
165
|
+
if (!grammar.language) continue;
|
|
166
|
+
if (seenScope.has(grammar.scopeName)) continue;
|
|
167
|
+
if (normalizedLangIds) {
|
|
168
|
+
const names = [grammar.language, ...languageIdToAliases.get(grammar.language) ?? []].filter(Boolean).map((name) => name.toLowerCase());
|
|
169
|
+
const match = normalizedLangIds.find((desiredName) => names.includes(desiredName));
|
|
170
|
+
if (!match) continue;
|
|
171
|
+
}
|
|
172
|
+
try {
|
|
173
|
+
const uri = vscode.Uri.joinPath(extension.extensionUri, grammar.path);
|
|
174
|
+
const raw = await vscode.workspace.fs.readFile(uri);
|
|
175
|
+
const jsonText = decoder.decode(raw);
|
|
176
|
+
const grammarJson = parse(jsonText);
|
|
177
|
+
const embeddedLangs = grammar.embeddedLanguages ? Array.from(new Set(Object.values(grammar.embeddedLanguages).filter(Boolean))) : void 0;
|
|
178
|
+
seenScope.add(grammar.scopeName);
|
|
179
|
+
extensionLangs.push({
|
|
180
|
+
...grammarJson,
|
|
181
|
+
name: grammar.language,
|
|
182
|
+
embeddedLangs,
|
|
183
|
+
aliases: languageIdToAliases.get(grammar.language) ?? void 0
|
|
184
|
+
});
|
|
185
|
+
} catch {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (normalizedLangIds) {
|
|
191
|
+
const builtinLangs = inferBuiltinLanguageIds(extensionLangs);
|
|
192
|
+
return [...extensionLangs, ...builtinLangs];
|
|
193
|
+
}
|
|
194
|
+
return extensionLangs;
|
|
195
|
+
} catch {
|
|
196
|
+
return [];
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
//#endregion
|
|
201
|
+
export { getUserLangs, getUserTheme, inferBuiltinLanguageIds };
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vscode-shiki-bridge",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Embed Shiki code blocks in VS Code, inheriting the user's themes and languages",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/yoavbls/vscode-shiki-bridge#readme",
|
|
8
|
+
"workspaces": [
|
|
9
|
+
"example/vscode-shiki-bridge-example-extension"
|
|
10
|
+
],
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/yoavbls/vscode-shiki-bridge/issues"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/yoavbls/vscode-shiki-bridge.git"
|
|
17
|
+
},
|
|
18
|
+
"author": "Yoav Balasiano <yoavbls@gmail.com>",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"main": "./dist/index.js",
|
|
23
|
+
"module": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"development": "./src/index.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsdown",
|
|
37
|
+
"dev": "tsdown --watch",
|
|
38
|
+
"ci": "npm run typecheck && npm run lint",
|
|
39
|
+
"typecheck": "tsc --noEmit",
|
|
40
|
+
"lint": "eslint .",
|
|
41
|
+
"release": "bumpp && npm publish",
|
|
42
|
+
"prepare": "tsdown"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/node": "^22.15.17",
|
|
46
|
+
"@typescript-eslint/eslint-plugin": "^8.39.0",
|
|
47
|
+
"@typescript-eslint/parser": "^8.39.0",
|
|
48
|
+
"bumpp": "^10.1.0",
|
|
49
|
+
"eslint": "^9.32.0",
|
|
50
|
+
"tsdown": "^0.11.9",
|
|
51
|
+
"typescript": "^5.8.3"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"@types/vscode": "^1.102.0",
|
|
55
|
+
"shiki": "^3.9.2"
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"jsonc-parser": "^3.3.1"
|
|
59
|
+
}
|
|
60
|
+
}
|