Skip to content

Build your personal knowledge base with Trilium Notes

fix(script): glob missing from frontend scripts

Elian Doran430fba7e6bb7
diff --git a/packages/codemirror/src/type_completion/glob_types.ts b/packages/codemirror/src/type_completion/glob_types.ts
new file mode 100644
index 0000000000..7ab5479a7f
--- /dev/null
+++ b/packages/codemirror/src/type_completion/glob_types.ts
@@ -0,0 +1,88 @@
+// Global `glob` type declarations injected into the FRONTEND (and JSX) script-note
+// vfs so that `glob` — the browser-global Trilium exposes on `window.glob` — resolves
+// with completion and hover docs, instead of being flagged as an undefined name.
+//
+// Unlike the jQuery globals (read verbatim from `@types/jquery`) and the `api` surface
+// (injected from the shared, drift-guarded `script_api.ts`), `glob`'s real type lives in
+// the client app (`CustomGlobals` in `apps/client/src/types.d.ts`, which extends
+// `BootstrapDefinition` from `@triliumnext/commons`) and pulls in a large graph of
+// client-internal types that can't resolve inside this isolated vfs. So this is a
+// *curated* declaration covering the members useful to script authors — keep it in
+// sync with those two sources when they change. Editor-only IntelliSense, not a
+// runtime contract: loosely-typed members (`appContext`, internal helpers) are dynamic
+// debugging surfaces deliberately left as `unknown`.
+//
+// Imports `ScriptFNote` from the api types file (injected at `/trilium-script-api.ts`)
+// so note-returning members get real note typing. The import makes this a module, so
+// the global is declared inside `declare global`.
+export const globGlobals = `
+import type { ScriptFNote } from "./trilium-script-api";
+
+interface TriliumGlobFroca {
+ getNote(noteId: string, silentNotFoundError?: boolean): Promise<ScriptFNote | null>;
+ getNoteFromCache(noteId: string): ScriptFNote | undefined;
+ getNotes(noteIds: string[], silentNotFoundError?: boolean): Promise<ScriptFNote[]>;
+ getNotesFromCache(noteIds: string[], silentNotFoundError?: boolean): ScriptFNote[];
+}
+
+/**
+ * The global \`glob\` object, available to frontend scripts in the browser
+ * (\`window.glob\`). Holds bootstrap configuration sent by the server plus a few
+ * client helpers. Most scripting should go through the \`api\` global instead;
+ * \`glob\` is useful for environment/version checks and low-level access.
+ */
+interface TriliumGlob {
+ /** Whether the client is running the desktop (Electron) layout. */
+ isDesktop(): boolean;
+ /** Whether the client is running the mobile layout. */
+ isMobile(): boolean;
+ /** The note currently active in the focused tab, or null if none. */
+ getActiveContextNote(): ScriptFNote | null;
+ /** Resolved theme style after applying the "auto" preference. */
+ getThemeStyle(): "auto" | "light" | "dark";
+ /** HTTP headers (incl. CSRF token) to attach to manual fetch/XHR calls. */
+ getHeaders(): Promise<Record<string, string>>;
+ /** Human-readable title for a reference link, resolved asynchronously. */
+ getReferenceLinkTitle(href: string): Promise<string>;
+ /** Human-readable title for a reference link, resolved from cache synchronously. */
+ getReferenceLinkTitleSync(href: string): string;
+ /** Client-side note cache (Froca). Prefer \`api\` methods where available. */
+ froca: TriliumGlobFroca;
+ /** Alias of \`froca\` kept for backwards compatibility. */
+ treeCache: TriliumGlobFroca;
+
+ /** Display name of this Trilium instance, or null if unset. */
+ instanceName: string | null;
+ /** Running version of Trilium. */
+ triliumVersion: string;
+ /** Whether the client is running inside Electron (desktop app). */
+ isElectron: boolean;
+ /** Whether the client is the standalone (in-browser, WASM) build. */
+ isStandalone: boolean;
+ /** Whether this is a development build. */
+ isDev: boolean;
+ /** Active UI locale code (e.g. "en", "de"). */
+ currentLocale: string;
+ /** Whether the active locale is right-to-left. */
+ isRtl: boolean;
+ /** Base URL prefix for the internal REST API. */
+ baseApiUrl: string;
+ /** URL prefix under which static assets are served. */
+ assetPath: string;
+ /** Application path prefix Trilium is mounted at. */
+ appPath: string;
+
+ /**
+ * The root client application context — a large, dynamic internal object
+ * exposed for debugging. Untyped on purpose; prefer the \`api\` global.
+ */
+ appContext: unknown;
+ /** Resolve the widget/component bound to a DOM element. Internal. */
+ getComponentByEl(el: unknown): unknown;
+}
+
+declare global {
+ // eslint-disable-next-line no-var
+ var glob: TriliumGlob;
+}
+`;
diff --git a/packages/codemirror/src/type_completion/index.spec.ts b/packages/codemirror/src/type_completion/index.spec.ts
index 23a49e56ad..ce502a0f9b 100644
--- a/packages/codemirror/src/type_completion/index.spec.ts
+++ b/packages/codemirror/src/type_completion/index.spec.ts
@@ -21,6 +21,8 @@ const TS_TYPE_NOT_ASSIGNABLE = 2322;
const TS_TOP_LEVEL_AWAIT = 1375;
/** "Cannot find name '$'. Do you need to install type definitions for jQuery?…" */
const TS_JQUERY_NOT_FOUND = 2592;
+/** "Cannot find name 'x'." */
+const TS_NAME_NOT_FOUND = 2304;
/** "Unreachable code detected." — the one diagnostic rendered as a warning marker. */
const TS_UNREACHABLE_CODE = 7027;
@@ -99,6 +101,34 @@ describe("script note diagnostics", () => {
expect(codes).toContain(TS_JQUERY_NOT_FOUND);
}, TIMEOUT);
+ it("provides the glob global to frontend scripts (member + note typing)", async () => {
+ const codes = await getScriptDiagnosticCodes(
+ SCRIPT_MIME_FRONTEND,
+ "if (glob.isElectron && glob.isDesktop()) {\n"
+ + " const note = glob.getActiveContextNote();\n"
+ + " api.showMessage(note?.title ?? glob.triliumVersion);\n"
+ + "}"
+ );
+ expect(codes).toEqual([]);
+ }, TIMEOUT);
+
+ it("does not provide glob to backend scripts (browser-only global)", async () => {
+ const codes = await getScriptDiagnosticCodes(
+ SCRIPT_MIME_BACKEND,
+ "const v = glob.triliumVersion;"
+ );
+ // TS2304 "Cannot find name 'glob'." — glob is unavailable server-side.
+ expect(codes).toContain(TS_NAME_NOT_FOUND);
+ }, TIMEOUT);
+
+ it("still surfaces real errors on the glob global (unknown member)", async () => {
+ const codes = await getScriptDiagnosticCodes(
+ SCRIPT_MIME_FRONTEND,
+ "const v = glob.thisGlobMemberDoesNotExist;"
+ );
+ expect(codes).toContain(TS_UNKNOWN_PROPERTY);
+ }, TIMEOUT);
+
it("still surfaces real errors (unknown api member)", async () => {
// Guards against "fixing" false positives by disabling all checking:
// genuine semantic errors must still be reported.
diff --git a/packages/codemirror/src/type_completion/index.ts b/packages/codemirror/src/type_completion/index.ts
index 0baa576ec0..5faaaf2e73 100644
--- a/packages/codemirror/src/type_completion/index.ts
+++ b/packages/codemirror/src/type_completion/index.ts
@@ -64,6 +64,7 @@ const API_GLOBALS_PATH = "/trilium-api.d.ts";
/** The shared public API types module (frontend), injected verbatim from commons. */
const API_TYPES_PATH = "/trilium-script-api.ts";
const JQUERY_DTS_PATH = "/jquery-globals.d.ts";
+const GLOB_DTS_PATH = "/trilium-glob.d.ts";
const TRILIUM_MODULES_DTS_PATH = "/trilium-modules.d.ts";
/** The virtual source-file path used for a given script MIME type. */
@@ -136,6 +137,13 @@ async function createEnv(mime: string, context: ScriptApiContext = {}) {
const { jqueryGlobals } = await import("./jquery_types.js");
fsMap.set(JQUERY_DTS_PATH, jqueryGlobals);
rootFiles.push(JQUERY_DTS_PATH);
+
+ // Frontend scripts also have the browser `glob` global (`window.glob`);
+ // backend scripts don't. Its real type lives in the client app, so a curated
+ // declaration is injected here (see ./glob_types).
+ const { globGlobals } = await import("./glob_types.js");
+ fsMap.set(GLOB_DTS_PATH, globGlobals);
+ rootFiles.push(GLOB_DTS_PATH);
} else {
// Backend `api` types come from the same shared public surface in commons
// (single source of truth, drift-guarded). No jQuery/DOM server-side.