@farming-labs/svelte-theme 0.2.37 → 0.2.38
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farming-labs/svelte-theme",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.38",
|
|
4
4
|
"description": "Svelte UI components for @farming-labs/docs — layout, sidebar, TOC, search, and theme toggle",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"docs",
|
|
@@ -122,8 +122,8 @@
|
|
|
122
122
|
"dependencies": {
|
|
123
123
|
"gray-matter": "^4.0.3",
|
|
124
124
|
"sugar-high": "^0.9.5",
|
|
125
|
-
"@farming-labs/
|
|
126
|
-
"@farming-labs/
|
|
125
|
+
"@farming-labs/docs": "0.2.38",
|
|
126
|
+
"@farming-labs/svelte": "0.2.38"
|
|
127
127
|
},
|
|
128
128
|
"peerDependencies": {
|
|
129
129
|
"svelte": ">=5.0.0"
|
|
@@ -3,8 +3,14 @@
|
|
|
3
3
|
import SearchDialog from "./SearchDialog.svelte";
|
|
4
4
|
import AskAIDialog from "./AskAIDialog.svelte";
|
|
5
5
|
import FloatingAIChat from "./FloatingAIChat.svelte";
|
|
6
|
+
import { afterNavigate } from "$app/navigation";
|
|
6
7
|
import { page } from "$app/stores";
|
|
8
|
+
import { env as publicEnv } from "$env/dynamic/public";
|
|
7
9
|
import { onMount } from "svelte";
|
|
10
|
+
import {
|
|
11
|
+
emitSvelteDocsClientAnalyticsEvent,
|
|
12
|
+
installSvelteDocsAnalytics,
|
|
13
|
+
} from "../lib/clientAnalytics.js";
|
|
8
14
|
|
|
9
15
|
let {
|
|
10
16
|
tree,
|
|
@@ -79,11 +85,55 @@
|
|
|
79
85
|
].join("");
|
|
80
86
|
});
|
|
81
87
|
|
|
88
|
+
let lastAnalyticsPageKey = "";
|
|
89
|
+
|
|
90
|
+
function emitPageView(url) {
|
|
91
|
+
if (typeof window === "undefined") return;
|
|
92
|
+
if (!url) return;
|
|
93
|
+
|
|
94
|
+
window.setTimeout(() => {
|
|
95
|
+
const pathname = url.pathname.replace(/\/$/, "") || "/";
|
|
96
|
+
const search = url.search;
|
|
97
|
+
const locale =
|
|
98
|
+
url.searchParams.get("lang") ?? url.searchParams.get("locale") ?? activeLocale ?? null;
|
|
99
|
+
const pageKey = `${pathname}${search}|${locale ?? ""}`;
|
|
100
|
+
if (pageKey === lastAnalyticsPageKey) return;
|
|
101
|
+
|
|
102
|
+
lastAnalyticsPageKey = pageKey;
|
|
103
|
+
emitSvelteDocsClientAnalyticsEvent({
|
|
104
|
+
type: "page_view",
|
|
105
|
+
locale,
|
|
106
|
+
path: pathname,
|
|
107
|
+
url: url.href,
|
|
108
|
+
properties: {
|
|
109
|
+
entry: config?.entry,
|
|
110
|
+
framework: "sveltekit",
|
|
111
|
+
pathname,
|
|
112
|
+
...(search ? { search } : {}),
|
|
113
|
+
...(document.title ? { title: document.title } : {}),
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
}, 0);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
afterNavigate(({ to }) => {
|
|
120
|
+
emitPageView(to?.url ?? $page.url);
|
|
121
|
+
});
|
|
122
|
+
|
|
82
123
|
onMount(() => {
|
|
124
|
+
const cleanupAnalytics = installSvelteDocsAnalytics({
|
|
125
|
+
analytics: config?.analytics,
|
|
126
|
+
env: publicEnv,
|
|
127
|
+
});
|
|
128
|
+
|
|
83
129
|
if (forcedTheme) {
|
|
84
130
|
document.documentElement.classList.remove("light", "dark");
|
|
85
131
|
document.documentElement.classList.add(forcedTheme);
|
|
86
132
|
}
|
|
133
|
+
|
|
134
|
+
return () => {
|
|
135
|
+
cleanupAnalytics();
|
|
136
|
+
};
|
|
87
137
|
});
|
|
88
138
|
|
|
89
139
|
let sidebarOpen = $state(false);
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
const DEFAULT_DOCS_CLOUD_ANALYTICS_ENDPOINT = "https://api.farming-labs.dev/v1/analytics/events";
|
|
2
|
+
const VISITOR_ID_STORAGE_KEY = "fd:analytics:visitor-id";
|
|
3
|
+
const SESSION_ID_STORAGE_KEY = "fd:analytics:session-id";
|
|
4
|
+
|
|
5
|
+
function createAnalyticsId(prefix) {
|
|
6
|
+
const random =
|
|
7
|
+
typeof crypto !== "undefined" && typeof crypto.randomUUID === "function"
|
|
8
|
+
? crypto.randomUUID()
|
|
9
|
+
: `${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 12)}`;
|
|
10
|
+
|
|
11
|
+
return `${prefix}_${random}`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function normalizeValue(value) {
|
|
15
|
+
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function isFalsyEnv(value) {
|
|
19
|
+
return /^(0|false|no|off)$/i.test(normalizeValue(value) ?? "");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function asRecord(value) {
|
|
23
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function getRuntimeEnvValue(env, ...names) {
|
|
27
|
+
for (const name of names) {
|
|
28
|
+
const value = normalizeValue(env?.[name]) ?? normalizeValue(globalThis.process?.env?.[name]);
|
|
29
|
+
if (value) return value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function readStorage(storage, key) {
|
|
36
|
+
try {
|
|
37
|
+
return normalizeValue(storage?.getItem(key));
|
|
38
|
+
} catch {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function writeStorage(storage, key, value) {
|
|
44
|
+
try {
|
|
45
|
+
storage?.setItem(key, value);
|
|
46
|
+
} catch {
|
|
47
|
+
// Storage can be unavailable in private browsing or locked-down embeds.
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getBrowserStorage(target, key) {
|
|
52
|
+
try {
|
|
53
|
+
return target[key];
|
|
54
|
+
} catch {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function getOrCreateClientId({ cachedValue, create, storage, storageKey }) {
|
|
60
|
+
const stored = readStorage(storage, storageKey);
|
|
61
|
+
const value = stored ?? cachedValue ?? create();
|
|
62
|
+
|
|
63
|
+
if (!stored) {
|
|
64
|
+
writeStorage(storage, storageKey, value);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function getSvelteDocsClientAnalyticsIdentity() {
|
|
71
|
+
if (typeof window === "undefined") return null;
|
|
72
|
+
|
|
73
|
+
const target = window;
|
|
74
|
+
const visitorId = getOrCreateClientId({
|
|
75
|
+
cachedValue: target.__fdAnalyticsVisitorId__,
|
|
76
|
+
create: () => createAnalyticsId("visitor"),
|
|
77
|
+
storage: getBrowserStorage(target, "localStorage"),
|
|
78
|
+
storageKey: VISITOR_ID_STORAGE_KEY,
|
|
79
|
+
});
|
|
80
|
+
const sessionId = getOrCreateClientId({
|
|
81
|
+
cachedValue: target.__fdAnalyticsSessionId__,
|
|
82
|
+
create: () => createAnalyticsId("session"),
|
|
83
|
+
storage: getBrowserStorage(target, "sessionStorage"),
|
|
84
|
+
storageKey: SESSION_ID_STORAGE_KEY,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
target.__fdAnalyticsVisitorId__ = visitorId;
|
|
88
|
+
target.__fdAnalyticsSessionId__ = sessionId;
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
anonymousId: visitorId,
|
|
92
|
+
visitorId,
|
|
93
|
+
sessionId,
|
|
94
|
+
visitor: {
|
|
95
|
+
id: visitorId,
|
|
96
|
+
},
|
|
97
|
+
session: {
|
|
98
|
+
id: sessionId,
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function normalizeClientAnalyticsProperties(identity, properties) {
|
|
104
|
+
const provided = properties ?? {};
|
|
105
|
+
const visitorId =
|
|
106
|
+
normalizeValue(provided.visitorId) ??
|
|
107
|
+
normalizeValue(asRecord(provided.visitor).id) ??
|
|
108
|
+
normalizeValue(provided.anonymousId) ??
|
|
109
|
+
identity?.visitorId;
|
|
110
|
+
const sessionId =
|
|
111
|
+
normalizeValue(provided.sessionId) ??
|
|
112
|
+
normalizeValue(asRecord(provided.session).id) ??
|
|
113
|
+
identity?.sessionId;
|
|
114
|
+
const merged = {
|
|
115
|
+
...identity,
|
|
116
|
+
...provided,
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
...merged,
|
|
121
|
+
...(visitorId
|
|
122
|
+
? {
|
|
123
|
+
anonymousId: normalizeValue(provided.anonymousId) ?? visitorId,
|
|
124
|
+
visitorId,
|
|
125
|
+
visitor: {
|
|
126
|
+
...asRecord(merged.visitor),
|
|
127
|
+
id: visitorId,
|
|
128
|
+
},
|
|
129
|
+
}
|
|
130
|
+
: {}),
|
|
131
|
+
...(sessionId
|
|
132
|
+
? {
|
|
133
|
+
sessionId,
|
|
134
|
+
session: {
|
|
135
|
+
...asRecord(merged.session),
|
|
136
|
+
id: sessionId,
|
|
137
|
+
},
|
|
138
|
+
}
|
|
139
|
+
: {}),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function emitSvelteDocsClientAnalyticsEvent(event) {
|
|
144
|
+
if (typeof window === "undefined") return;
|
|
145
|
+
|
|
146
|
+
const identity = getSvelteDocsClientAnalyticsIdentity();
|
|
147
|
+
const normalized = {
|
|
148
|
+
...event,
|
|
149
|
+
source: "client",
|
|
150
|
+
path: event.path ?? window.location.pathname,
|
|
151
|
+
url: event.url ?? window.location.href,
|
|
152
|
+
referrer: event.referrer ?? (document.referrer || undefined),
|
|
153
|
+
timestamp: new Date().toISOString(),
|
|
154
|
+
properties: normalizeClientAnalyticsProperties(identity, event.properties),
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const target = window;
|
|
158
|
+
try {
|
|
159
|
+
if (target.__fdAnalytics__) {
|
|
160
|
+
Promise.resolve(target.__fdAnalytics__(normalized)).catch(() => {
|
|
161
|
+
// Analytics should never break the docs UI.
|
|
162
|
+
});
|
|
163
|
+
} else {
|
|
164
|
+
target.__fdAnalyticsQueue__ = [...(target.__fdAnalyticsQueue__ ?? []), normalized].slice(-50);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
window.dispatchEvent(new CustomEvent("fd:analytics", { detail: normalized }));
|
|
168
|
+
} catch {
|
|
169
|
+
// Analytics should never break the docs UI.
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function resolveConsoleLevel(analytics, hasEventHandler) {
|
|
174
|
+
if (!analytics || analytics === true) return analytics === true ? "info" : false;
|
|
175
|
+
if (analytics.console === false) return false;
|
|
176
|
+
if (analytics.console === true) return "info";
|
|
177
|
+
if (["log", "info", "debug"].includes(analytics.console)) return analytics.console;
|
|
178
|
+
return hasEventHandler ? false : "info";
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function resolveSvelteDocsAnalyticsOptions({ analytics, env }) {
|
|
182
|
+
if (
|
|
183
|
+
analytics === false ||
|
|
184
|
+
(analytics && typeof analytics === "object" && analytics.enabled === false)
|
|
185
|
+
) {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const enabled = getRuntimeEnvValue(
|
|
190
|
+
env,
|
|
191
|
+
"PUBLIC_DOCS_CLOUD_ANALYTICS_ENABLED",
|
|
192
|
+
"NEXT_PUBLIC_DOCS_CLOUD_ANALYTICS_ENABLED",
|
|
193
|
+
"DOCS_CLOUD_ANALYTICS_ENABLED",
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
if (isFalsyEnv(enabled)) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const projectId = getRuntimeEnvValue(
|
|
201
|
+
env,
|
|
202
|
+
"PUBLIC_DOCS_CLOUD_PROJECT_ID",
|
|
203
|
+
"NEXT_PUBLIC_DOCS_CLOUD_PROJECT_ID",
|
|
204
|
+
"DOCS_CLOUD_PROJECT_ID",
|
|
205
|
+
);
|
|
206
|
+
const endpoint =
|
|
207
|
+
getRuntimeEnvValue(
|
|
208
|
+
env,
|
|
209
|
+
"PUBLIC_DOCS_CLOUD_ANALYTICS_ENDPOINT",
|
|
210
|
+
"NEXT_PUBLIC_DOCS_CLOUD_ANALYTICS_ENDPOINT",
|
|
211
|
+
"DOCS_CLOUD_ANALYTICS_ENDPOINT",
|
|
212
|
+
) ?? DEFAULT_DOCS_CLOUD_ANALYTICS_ENDPOINT;
|
|
213
|
+
const userOnEvent =
|
|
214
|
+
analytics && typeof analytics === "object" && typeof analytics.onEvent === "function"
|
|
215
|
+
? analytics.onEvent
|
|
216
|
+
: undefined;
|
|
217
|
+
const hasEventHandler = typeof userOnEvent === "function" || Boolean(projectId);
|
|
218
|
+
|
|
219
|
+
if (!analytics && !hasEventHandler) {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return {
|
|
224
|
+
console: resolveConsoleLevel(analytics, hasEventHandler),
|
|
225
|
+
endpoint,
|
|
226
|
+
includeInputs: analytics && typeof analytics === "object" && analytics.includeInputs === true,
|
|
227
|
+
onEvent: userOnEvent,
|
|
228
|
+
projectId,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function normalizeAnalyticsEvent(event, options) {
|
|
233
|
+
const normalized = {
|
|
234
|
+
...event,
|
|
235
|
+
source: event.source ?? "client",
|
|
236
|
+
timestamp: event.timestamp ?? new Date().toISOString(),
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
if (!options.includeInputs && normalized.input) {
|
|
240
|
+
delete normalized.input;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return normalized;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
async function sendDocsCloudAnalyticsEvent(options, event) {
|
|
247
|
+
if (typeof fetch !== "function") return;
|
|
248
|
+
if (!options.projectId) return;
|
|
249
|
+
|
|
250
|
+
try {
|
|
251
|
+
await fetch(options.endpoint, {
|
|
252
|
+
method: "POST",
|
|
253
|
+
headers: {
|
|
254
|
+
"content-type": "application/json",
|
|
255
|
+
},
|
|
256
|
+
body: JSON.stringify({
|
|
257
|
+
projectId: options.projectId,
|
|
258
|
+
event,
|
|
259
|
+
}),
|
|
260
|
+
credentials: "omit",
|
|
261
|
+
keepalive: true,
|
|
262
|
+
});
|
|
263
|
+
} catch {
|
|
264
|
+
// Docs Cloud delivery should never interfere with the docs runtime.
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async function emitAnalyticsEvent(options, event) {
|
|
269
|
+
const normalized = normalizeAnalyticsEvent(event, options);
|
|
270
|
+
|
|
271
|
+
if (options.console) {
|
|
272
|
+
const logger = console[options.console] ?? console.info;
|
|
273
|
+
logger.call(console, "[@farming-labs/docs:analytics]", normalized);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (typeof options.onEvent === "function") {
|
|
277
|
+
try {
|
|
278
|
+
await options.onEvent(normalized);
|
|
279
|
+
} catch {
|
|
280
|
+
// User analytics hooks should never break the docs UI.
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
await sendDocsCloudAnalyticsEvent(options, normalized);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function installSvelteDocsAnalytics({ analytics, env } = {}) {
|
|
288
|
+
if (typeof window === "undefined") return () => {};
|
|
289
|
+
|
|
290
|
+
const options = resolveSvelteDocsAnalyticsOptions({ analytics, env });
|
|
291
|
+
if (!options) return () => {};
|
|
292
|
+
|
|
293
|
+
const target = window;
|
|
294
|
+
const previous = target.__fdAnalytics__;
|
|
295
|
+
const handler = (event) => {
|
|
296
|
+
void emitAnalyticsEvent(options, event);
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
target.__fdAnalytics__ = handler;
|
|
300
|
+
|
|
301
|
+
const queued = target.__fdAnalyticsQueue__ ?? [];
|
|
302
|
+
delete target.__fdAnalyticsQueue__;
|
|
303
|
+
for (const event of queued) handler(event);
|
|
304
|
+
|
|
305
|
+
return () => {
|
|
306
|
+
if (target.__fdAnalytics__ === handler) {
|
|
307
|
+
if (typeof previous === "function") {
|
|
308
|
+
target.__fdAnalytics__ = previous;
|
|
309
|
+
} else {
|
|
310
|
+
delete target.__fdAnalytics__;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
}
|