forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics-utils.ts
More file actions
81 lines (70 loc) · 2.73 KB
/
Copy pathanalytics-utils.ts
File metadata and controls
81 lines (70 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { INTEGRATION_ORDER } from "./integrations";
/**
* Normalizes URLs by converting integration routes from /integrations/{integration}/... to /{integration}/...
* Also normalizes trailing slashes (removes them except for root).
* Only normalizes known integration routes to ensure consistency.
*
* Examples:
* - /integrations/langgraph → /langgraph
* - /integrations/langgraph/shared-state → /langgraph/shared-state
* - /langgraph/ → /langgraph
* - /langgraph → /langgraph (already canonical)
* - /reference → /reference (not an integration, unchanged)
*
* Use this for href attributes and canonical URLs.
*/
export function normalizeUrl(url: string): string {
if (!url) return url;
// Split the URL into segments
const segments = url.split("/").filter(Boolean);
// Check if first segment is 'integrations'
if (segments[0] === "integrations" && segments.length > 1) {
const integrationId = segments[1];
// Check if this is a known integration
if (
INTEGRATION_ORDER.includes(
integrationId as (typeof INTEGRATION_ORDER)[number],
)
) {
// Reconstruct as canonical URL: /{integration}/{rest of path}
const restOfPath = segments.slice(2).join("/");
const normalized = `/${integrationId}${restOfPath ? "/" + restOfPath : ""}`;
// Remove trailing slash (except for root)
return normalized === "/" ? "/" : normalized.replace(/\/$/, "");
}
}
// If not an integration route, normalize trailing slashes
return url === "/" ? "/" : url.replace(/\/$/, "");
}
/**
* Normalizes URLs for matching/comparison purposes.
*
* This function:
* 1. First normalizes integration URLs (via normalizeUrl)
* 2. Handles relative paths (../)
* 3. Ensures consistent trailing slash handling
*
* Use this for comparing URLs (e.g., active state detection).
*
* Examples:
* - ../langgraph → /langgraph
* - /integrations/langgraph/ → /langgraph
* - /langgraph/index → /langgraph/index (preserved for index matching logic)
*/
export function normalizeUrlForMatching(url: string): string {
if (!url) return "";
// Handle relative URLs (simplified - assumes they resolve to absolute)
let normalized = url.startsWith("../") ? url.replace(/^\.\.\//, "/") : url;
// First normalize integration URLs and trailing slashes
normalized = normalizeUrl(normalized);
// Remove trailing slashes (except for root) for consistent matching
// Note: We preserve /index for index page matching logic
return normalized === "/" ? "/" : normalized.replace(/\/$/, "");
}
/**
* Normalizes pathnames for analytics tracking.
* Alias for normalizeUrl for clarity in analytics context.
*/
export function normalizePathnameForAnalytics(pathname: string): string {
return normalizeUrl(pathname);
}