@coldsmirk/inkstone-core 0.29.0 → 0.30.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/README.md +1 -0
- package/dist/index.d.ts +33 -2
- package/dist/index.js +50 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,6 +10,7 @@ Part of [inkstone](https://github.com/coldsmirk/inkstone). The editor layers ([`
|
|
|
10
10
|
- **`configureHighlighter(config)`** — extend or replace the default grammar / theme set, and choose the regex engine, once, before any editor mounts. `DEFAULT_HIGHLIGHTER_LANGS` is exported so extension is additive. `engine` takes the engine itself, a promise of one, or a factory called once when the highlighter is first built — configuration stays synchronous while the engine may be async to make: the strict JavaScript engine by default, `createJavaScriptRegexEngine({ forgiving: true })` for grammars the strict one refuses, or `onigurumaEngine`.
|
|
11
11
|
- **`onigurumaEngine`** — the Oniguruma engine on its WASM, inlined into a chunk of your own bundle (about 600 KB, nothing fetched from anywhere else) and instantiated once, in about 20 ms. Which engine: the JavaScript one translates each grammar's patterns into JavaScript at the first tokenization, on the main thread, and pays for it on whole files — a TypeScript file's first sixty lines cost it about 170 ms, against Oniguruma's 20. Colouring fenced snippets, keep the default; reading whole files in an editor (`@coldsmirk/inkstone-monaco`'s Shiki bridge, `<MonacoEditor>`), pass `onigurumaEngine` as the factory. `onigurumaEngine(wasm)` takes the WASM from elsewhere in any form Shiki's `createOnigurumaEngine` accepts, such as a `fetch("/onig.wasm")`.
|
|
12
12
|
- **`shikiThemeId(dark)`** plus the `INKSTONE_LIGHT_THEME` / `INKSTONE_DARK_THEME` constants — the theme names to pass wherever a Shiki theme id is expected.
|
|
13
|
+
- **`highlighterSources(highlighter)`** — what the shared highlighter was built from and has loaded since: every grammar registration in the order it was given them (the configured `langs` and every later `loadLanguage` / `loadLanguageSync`, which the highlighter wraps to keep them) and the kind of its engine — `oniguruma` wherever an engine was configured, `javascript` where none was. `@coldsmirk/inkstone-monaco`'s tokenization worker builds a highlighter of the same grammars from it; undefined for a highlighter inkstone did not build.
|
|
13
14
|
- **`configureEditorColors(config)`** — set every color the editors paint that is not syntax (a VS Code theme's `colors`, beside its `tokenColors`) in your design language, once, before any editor mounts. Per scheme, CSS color literals for the editor (`background` — content, gutter and the fields of its chrome — `lineHighlight`, `lineNumber`, `lineNumberActive`, `cursor`, `selection`, `scrollbar`), its floating chrome — completion, hover, parameter hints, the find widget and search panels, the context menu — (`widget`, `widgetBorder`, `widgetSelection`, `widgetForeground`, `widgetForegroundMuted`), the `accent` that rings a focused field and marks what a completion matched, and the diagnostic severities `error`, `warning`, `info` and `hint`, named as CodeMirror's `Diagnostic.severity` names them. The catppuccin pair keeps its syntax palette — token colors, the editor's default text, the completion-kind icons that mirror them — and gets these baked into its theme colors — Monaco, static rendering, and CodeMirror's chrome all read the same values — so an editor sits inside the app instead of bringing a second dark. A role or scheme left out keeps the flavor's own color, a later call replaces the schemes it names, and values must be hex colors (`#rrggbb`, `#rrggbbaa`) — Monaco parses theme colors as hex and paints red for anything else, so a `var()` reference or an `rgb(…)` is refused up front. Keep `lineHighlight` translucent: CodeMirror draws selections behind the content, so an opaque active line would hide them. `selection` shows at half its alpha while the editor is unfocused, in both engines; `scrollbar` has no hover state (CodeMirror's `scrollbar-color` has none, so Monaco's slider keeps it too). `getEditorColors(dark)` reads what was configured, and `withEditorColors(theme, colors)` applies a set of them to a Shiki theme of your own.
|
|
14
15
|
- **`CODE_FONT_FAMILY`** — the shared code font stack both editor engines render with: the page's `--inkstone-font-family-code` CSS token when defined, else the platform's native monospace.
|
|
15
16
|
- **`inkstoneRadius(size, fallback)` / `inkstoneShadow(size, fallback)`** — `var()` references to the host's `--inkstone-radius-{small,medium,large,xlarge}` and `--inkstone-shadow-{medium,large,xlarge}` tokens. Set them once on your root and both engines' chrome follows: the Monaco host maps them onto VS Code's `--vscode-cornerRadius-*` / `--vscode-shadow-*` design tokens (which the standalone editor never defines), CodeMirror's polish reads them directly; unset, each engine keeps the look it ships with.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Awaitable, HighlighterCore, HighlighterCore as HighlighterCore$1, LanguageInput, LoadWasmOptions, RegexEngine, ThemeInput, ThemeRegistration } from "shiki/core";
|
|
1
|
+
import { Awaitable, HighlighterCore, HighlighterCore as HighlighterCore$1, LanguageInput, LanguageRegistration, LoadWasmOptions, RegexEngine, ThemeInput, ThemeRegistration } from "shiki/core";
|
|
2
2
|
//#region src/code-font.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* The code font stack every inkstone editor host renders with: the page's
|
|
@@ -197,6 +197,34 @@ interface HighlighterConfig {
|
|
|
197
197
|
* decode it out of a script.
|
|
198
198
|
*/
|
|
199
199
|
declare function onigurumaEngine(wasm?: LoadWasmOptions): Promise<RegexEngine>;
|
|
200
|
+
/**
|
|
201
|
+
* The regex engine a highlighter built elsewhere from the shared one's grammars tokenizes
|
|
202
|
+
* with, to tokenize as the shared one does: `javascript`, the strict JavaScript engine, where
|
|
203
|
+
* the app configured none; `oniguruma` wherever it configured one. An engine is an object of
|
|
204
|
+
* the page's that cannot be sent anywhere, and Oniguruma is the engine every grammar was
|
|
205
|
+
* written against — the one {@link onigurumaEngine} is, and the one an engine configured for
|
|
206
|
+
* the grammars the strict engine refuses stands in for.
|
|
207
|
+
*/
|
|
208
|
+
type HighlighterEngineKind = "javascript" | "oniguruma";
|
|
209
|
+
/**
|
|
210
|
+
* What the shared highlighter was built from, for a highlighter of the same grammars built
|
|
211
|
+
* elsewhere — Monaco's tokenization worker (`@coldsmirk/inkstone-monaco`) is one: every grammar
|
|
212
|
+
* registration it was given, in the order it was given them (shiki builds a grammar again when
|
|
213
|
+
* a language it embeds lazily arrives after it, so the order is part of what was built), and
|
|
214
|
+
* the kind of its engine.
|
|
215
|
+
*/
|
|
216
|
+
interface HighlighterSources {
|
|
217
|
+
readonly engine: HighlighterEngineKind;
|
|
218
|
+
/**
|
|
219
|
+
* Grows as the app loads grammars: read it when a copy is due, never once for good.
|
|
220
|
+
*/
|
|
221
|
+
readonly languages: readonly LanguageRegistration[];
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* What the shared highlighter was built from and has loaded since (see
|
|
225
|
+
* {@link HighlighterSources}); undefined for a highlighter inkstone did not build.
|
|
226
|
+
*/
|
|
227
|
+
declare function highlighterSources(highlighter: HighlighterCore$1): HighlighterSources | undefined;
|
|
200
228
|
/**
|
|
201
229
|
* Configure the shared highlighter before its first use. The highlighter is a process-wide
|
|
202
230
|
* singleton (editors, static rendering, and markdown surfaces all share it for consistent
|
|
@@ -234,6 +262,9 @@ declare function getEditorColors(dark: boolean): EditorColors | undefined;
|
|
|
234
262
|
* A failed build is not cached: the slot resets so a later call retries (and
|
|
235
263
|
* {@link configureHighlighter} becomes callable again), instead of replaying one transient
|
|
236
264
|
* failure — a chunk 404 after a redeploy, a flaky network — for the rest of the session.
|
|
265
|
+
*
|
|
266
|
+
* The highlighter keeps what it was built from and every grammar loaded into it since, for a
|
|
267
|
+
* highlighter of the same grammars built elsewhere ({@link highlighterSources}).
|
|
237
268
|
*/
|
|
238
269
|
declare function getHighlighter(): Promise<HighlighterCore$1>;
|
|
239
270
|
/**
|
|
@@ -242,4 +273,4 @@ declare function getHighlighter(): Promise<HighlighterCore$1>;
|
|
|
242
273
|
*/
|
|
243
274
|
declare function shikiThemeId(dark: boolean): string;
|
|
244
275
|
//#endregion
|
|
245
|
-
export { CODE_FONT_FAMILY, DEFAULT_HIGHLIGHTER_LANGS, type EditorColors, type EditorColorsConfig, type HighlighterConfig, type HighlighterCore, INKSTONE_DARK_THEME, INKSTONE_LIGHT_THEME, type RadiusSize, type ShadowSize, configureEditorColors, configureHighlighter, getEditorColors, getHighlighter, inkstoneRadius, inkstoneShadow, onigurumaEngine, shikiThemeId, withEditorColors };
|
|
276
|
+
export { CODE_FONT_FAMILY, DEFAULT_HIGHLIGHTER_LANGS, type EditorColors, type EditorColorsConfig, type HighlighterConfig, type HighlighterCore, type HighlighterEngineKind, type HighlighterSources, INKSTONE_DARK_THEME, INKSTONE_LIGHT_THEME, type RadiusSize, type ShadowSize, configureEditorColors, configureHighlighter, getEditorColors, getHighlighter, highlighterSources, inkstoneRadius, inkstoneShadow, onigurumaEngine, shikiThemeId, withEditorColors };
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import langSql from "@shikijs/langs/sql";
|
|
|
4
4
|
import langTypescript from "@shikijs/langs/typescript";
|
|
5
5
|
import catppuccinLatte from "@shikijs/themes/catppuccin-latte";
|
|
6
6
|
import catppuccinMacchiato from "@shikijs/themes/catppuccin-macchiato";
|
|
7
|
-
import { createHighlighterCore } from "shiki/core";
|
|
7
|
+
import { createHighlighterCore, isSpecialLang, normalizeGetter } from "shiki/core";
|
|
8
8
|
import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
|
|
9
9
|
import { createOnigurumaEngine } from "shiki/engine/oniguruma";
|
|
10
10
|
//#region src/code-font.ts
|
|
@@ -163,13 +163,47 @@ function getHighlighterRegistry() {
|
|
|
163
163
|
registry = {
|
|
164
164
|
config: {},
|
|
165
165
|
surfaces: {},
|
|
166
|
-
promise: null
|
|
166
|
+
promise: null,
|
|
167
|
+
sources: /* @__PURE__ */ new WeakMap()
|
|
167
168
|
};
|
|
168
169
|
host[HIGHLIGHTER_REGISTRY_KEY] = registry;
|
|
169
170
|
}
|
|
170
171
|
registry.surfaces ??= {};
|
|
172
|
+
registry.sources ??= /* @__PURE__ */ new WeakMap();
|
|
171
173
|
return registry;
|
|
172
174
|
}
|
|
175
|
+
async function resolveLanguages(langs) {
|
|
176
|
+
const loaded = await Promise.all(langs.filter((lang) => !isSpecialLang(lang)).map(async (lang) => {
|
|
177
|
+
const registrations = await normalizeGetter(lang);
|
|
178
|
+
return Array.isArray(registrations) ? registrations : [registrations];
|
|
179
|
+
}));
|
|
180
|
+
return [...new Set(loaded.flat())];
|
|
181
|
+
}
|
|
182
|
+
function keepingSources(highlighter, languages, engine, registry) {
|
|
183
|
+
const kept = [...languages];
|
|
184
|
+
const keep = (registrations) => {
|
|
185
|
+
for (const registration of registrations) if (!kept.includes(registration)) kept.push(registration);
|
|
186
|
+
};
|
|
187
|
+
const { loadLanguage, loadLanguageSync } = highlighter;
|
|
188
|
+
highlighter.loadLanguage = async (...langs) => {
|
|
189
|
+
const registrations = await resolveLanguages(langs);
|
|
190
|
+
await loadLanguage(...registrations);
|
|
191
|
+
keep(registrations);
|
|
192
|
+
};
|
|
193
|
+
highlighter.loadLanguageSync = (...langs) => {
|
|
194
|
+
const registrations = [...new Set(langs.flat())];
|
|
195
|
+
loadLanguageSync(...registrations);
|
|
196
|
+
keep(registrations);
|
|
197
|
+
};
|
|
198
|
+
registry.sources.set(highlighter, {
|
|
199
|
+
engine,
|
|
200
|
+
languages: kept
|
|
201
|
+
});
|
|
202
|
+
return highlighter;
|
|
203
|
+
}
|
|
204
|
+
function highlighterSources(highlighter) {
|
|
205
|
+
return getHighlighterRegistry().sources.get(highlighter);
|
|
206
|
+
}
|
|
173
207
|
function assertConfigurable(registry, caller) {
|
|
174
208
|
if (registry.promise) throw new Error(`${caller} must be called before the first getHighlighter()`);
|
|
175
209
|
}
|
|
@@ -202,15 +236,19 @@ function getHighlighter() {
|
|
|
202
236
|
const registry = getHighlighterRegistry();
|
|
203
237
|
const existing = registry.promise;
|
|
204
238
|
if (existing) return existing;
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
239
|
+
const { config, surfaces } = registry;
|
|
240
|
+
const attempt = (async () => {
|
|
241
|
+
const langs = await resolveLanguages(config.langs ?? DEFAULT_HIGHLIGHTER_LANGS);
|
|
242
|
+
return keepingSources(await createHighlighterCore({
|
|
243
|
+
themes: [
|
|
244
|
+
withEditorColors(catppuccinLatte, surfaces.light),
|
|
245
|
+
withEditorColors(catppuccinMacchiato, surfaces.dark),
|
|
246
|
+
...config.themes ?? []
|
|
247
|
+
],
|
|
248
|
+
langs,
|
|
249
|
+
engine: engineOf(config)
|
|
250
|
+
}), langs, config.engine === void 0 ? "javascript" : "oniguruma", registry);
|
|
251
|
+
})();
|
|
214
252
|
registry.promise = attempt;
|
|
215
253
|
attempt.catch(() => {
|
|
216
254
|
if (registry.promise === attempt) registry.promise = null;
|
|
@@ -221,4 +259,4 @@ function shikiThemeId(dark) {
|
|
|
221
259
|
return dark ? INKSTONE_DARK_THEME : INKSTONE_LIGHT_THEME;
|
|
222
260
|
}
|
|
223
261
|
//#endregion
|
|
224
|
-
export { CODE_FONT_FAMILY, DEFAULT_HIGHLIGHTER_LANGS, INKSTONE_DARK_THEME, INKSTONE_LIGHT_THEME, configureEditorColors, configureHighlighter, getEditorColors, getHighlighter, inkstoneRadius, inkstoneShadow, onigurumaEngine, shikiThemeId, withEditorColors };
|
|
262
|
+
export { CODE_FONT_FAMILY, DEFAULT_HIGHLIGHTER_LANGS, INKSTONE_DARK_THEME, INKSTONE_LIGHT_THEME, configureEditorColors, configureHighlighter, getEditorColors, getHighlighter, highlighterSources, inkstoneRadius, inkstoneShadow, onigurumaEngine, shikiThemeId, withEditorColors };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coldsmirk/inkstone-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.30.0",
|
|
4
4
|
"description": "Shared Shiki highlighter service for the inkstone editor toolkit: one fine-grained singleton (catppuccin themes, the JS regex engine by default or Oniguruma on request) reused by Monaco, static rendering, and markdown surfaces.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"shiki",
|