@hagicode/hagilight-core 0.17.1-dev.175.1.77872ad → 0.19.1-dev.184.1.830a4c6
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/Analytics51LA.astro +19 -3
- package/CookieConsent.astro +402 -0
- package/Footer.astro +31 -5
- package/FooterBrand.astro +23 -3
- package/GoogleAnalytics.astro +17 -2
- package/PostHog.astro +55 -3
- package/PromotoBanner.astro +3 -3
- package/README.md +102 -8
- package/dist/analytics-events.d.ts +15 -0
- package/dist/analytics-events.js +25 -15
- package/dist/consent-copy/de-DE.d.ts +2 -0
- package/dist/consent-copy/de-DE.js +21 -0
- package/dist/consent-copy/en-US.d.ts +2 -0
- package/dist/consent-copy/en-US.js +21 -0
- package/dist/consent-copy/es-ES.d.ts +2 -0
- package/dist/consent-copy/es-ES.js +21 -0
- package/dist/consent-copy/fr-FR.d.ts +2 -0
- package/dist/consent-copy/fr-FR.js +21 -0
- package/dist/consent-copy/index.d.ts +7 -0
- package/dist/consent-copy/index.js +31 -0
- package/dist/consent-copy/ja-JP.d.ts +2 -0
- package/dist/consent-copy/ja-JP.js +21 -0
- package/dist/consent-copy/ko-KR.d.ts +2 -0
- package/dist/consent-copy/ko-KR.js +21 -0
- package/dist/consent-copy/pt-BR.d.ts +2 -0
- package/dist/consent-copy/pt-BR.js +21 -0
- package/dist/consent-copy/ru-RU.d.ts +2 -0
- package/dist/consent-copy/ru-RU.js +21 -0
- package/dist/consent-copy/types.d.ts +22 -0
- package/dist/consent-copy/types.js +1 -0
- package/dist/consent-copy/zh-CN.d.ts +2 -0
- package/dist/consent-copy/zh-CN.js +21 -0
- package/dist/consent-copy/zh-Hant.d.ts +2 -0
- package/dist/consent-copy/zh-Hant.js +21 -0
- package/dist/consent-details.d.ts +29 -0
- package/dist/consent-details.js +41 -0
- package/dist/consent-gate.d.ts +47 -0
- package/dist/consent-gate.js +243 -0
- package/dist/consent-markup.d.ts +33 -0
- package/dist/consent-markup.js +113 -0
- package/dist/consent-mount.d.ts +26 -0
- package/dist/consent-mount.js +81 -0
- package/dist/consent-popup.d.ts +6 -0
- package/dist/consent-popup.js +203 -0
- package/dist/consent.d.ts +65 -0
- package/dist/consent.js +314 -0
- package/dist/entity-definition.d.ts +19 -0
- package/dist/entity-definition.js +23 -0
- package/dist/link-policy.d.ts +7 -2
- package/dist/link-policy.js +8 -1
- package/dist/links.d.ts +12 -4
- package/dist/links.js +16 -10
- package/dist/posthog-events.d.ts +95 -0
- package/dist/posthog-events.js +263 -0
- package/dist/posthog-interactions.d.ts +85 -0
- package/dist/posthog-interactions.js +280 -0
- package/dist/posthog.d.ts +25 -3
- package/dist/posthog.js +64 -6
- package/dist/promoto-analytics.d.ts +45 -0
- package/dist/promoto-analytics.js +92 -0
- package/dist/promoto-banner.js +62 -4
- package/dist/related-sites.json +2 -2
- package/dist/root-transit.d.ts +7 -1
- package/dist/root-transit.js +42 -1
- package/dist/seo.d.ts +30 -1
- package/dist/seo.js +93 -1
- package/dist/site-metadata.d.ts +11 -6
- package/dist/site-metadata.js +21 -8
- package/package.json +42 -1
package/dist/links.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export interface SiteLink {
|
|
|
12
12
|
ariaLabel?: string;
|
|
13
13
|
target?: '_blank';
|
|
14
14
|
rel?: LinkRel;
|
|
15
|
+
/** The destination before footer campaign tags. Set only on tagged links; the footer brand block re-tags from it. Not rendered. */
|
|
16
|
+
untaggedHref?: string;
|
|
15
17
|
}
|
|
16
18
|
export interface SiteLinkOverride {
|
|
17
19
|
href?: LocalizedText;
|
|
@@ -56,15 +58,21 @@ export declare function resolveSiteLinks(localeInput?: string | null, options?:
|
|
|
56
58
|
};
|
|
57
59
|
};
|
|
58
60
|
header: SiteLink[];
|
|
59
|
-
quick: SiteLink
|
|
60
|
-
|
|
61
|
+
quick: (SiteLink & {
|
|
62
|
+
untaggedHref?: string;
|
|
63
|
+
})[];
|
|
64
|
+
community: (SiteLink & {
|
|
65
|
+
untaggedHref?: string;
|
|
66
|
+
})[];
|
|
61
67
|
filings: SiteLink[];
|
|
62
|
-
relatedSites: {
|
|
68
|
+
relatedSites: ({
|
|
63
69
|
id: string;
|
|
64
70
|
name: string;
|
|
65
71
|
description: string | undefined;
|
|
66
72
|
href: string;
|
|
67
73
|
target: "_blank";
|
|
68
74
|
rel: LinkRel;
|
|
69
|
-
}
|
|
75
|
+
} & {
|
|
76
|
+
untaggedHref?: string;
|
|
77
|
+
})[];
|
|
70
78
|
};
|
package/dist/links.js
CHANGED
|
@@ -18,10 +18,10 @@ const translations = {
|
|
|
18
18
|
'fr-FR': 'Assistance', 'es-ES': 'Soporte', 'pt-BR': 'Suporte', 'ru-RU': 'Поддержка',
|
|
19
19
|
},
|
|
20
20
|
downloadClient: {
|
|
21
|
-
'zh-CN': '下载
|
|
22
|
-
'ja-JP': '
|
|
23
|
-
'de-DE': '
|
|
24
|
-
'es-ES': 'Descargar
|
|
21
|
+
'zh-CN': '下载 HagiCode', 'zh-Hant': '下載 HagiCode', 'en-US': 'Download HagiCode',
|
|
22
|
+
'ja-JP': 'HagiCode をダウンロード', 'ko-KR': 'HagiCode 다운로드',
|
|
23
|
+
'de-DE': 'HagiCode herunterladen', 'fr-FR': 'Télécharger HagiCode',
|
|
24
|
+
'es-ES': 'Descargar HagiCode', 'pt-BR': 'Baixar HagiCode', 'ru-RU': 'Скачать HagiCode',
|
|
25
25
|
},
|
|
26
26
|
about: {
|
|
27
27
|
'zh-CN': '关于 HagiCode', 'zh-Hant': '關於 HagiCode', 'en-US': 'About HagiCode',
|
|
@@ -102,11 +102,11 @@ const translations = {
|
|
|
102
102
|
'pt-BR': 'Grupo QQ 610394020', 'ru-RU': 'Группа QQ 610394020',
|
|
103
103
|
},
|
|
104
104
|
microsoftStore: {
|
|
105
|
-
'zh-CN': '下载
|
|
106
|
-
'en-US': 'Download
|
|
107
|
-
'ko-KR': 'Windows용
|
|
108
|
-
'fr-FR': 'Télécharger
|
|
109
|
-
'pt-BR': 'Baixar
|
|
105
|
+
'zh-CN': '下载 HagiCode Windows 版本', 'zh-Hant': '下載 HagiCode Windows 版本',
|
|
106
|
+
'en-US': 'Download HagiCode for Windows', 'ja-JP': 'HagiCode for Windows をダウンロード',
|
|
107
|
+
'ko-KR': 'Windows용 HagiCode 다운로드', 'de-DE': 'HagiCode für Windows herunterladen',
|
|
108
|
+
'fr-FR': 'Télécharger HagiCode pour Windows', 'es-ES': 'Descargar HagiCode para Windows',
|
|
109
|
+
'pt-BR': 'Baixar HagiCode para Windows', 'ru-RU': 'Скачать HagiCode для Windows',
|
|
110
110
|
},
|
|
111
111
|
icpFiling: {
|
|
112
112
|
'zh-CN': '闽ICP备2026004153号-1',
|
|
@@ -361,8 +361,14 @@ export function resolveSiteLinks(localeInput, options = {}) {
|
|
|
361
361
|
});
|
|
362
362
|
// Campaign tags are added after deduplication and never reach the
|
|
363
363
|
// dedup sets above, so a tagged and a clean destination still collapse.
|
|
364
|
+
// The link id is the campaign and `footer` the content; the brand block
|
|
365
|
+
// re-tags from `untaggedHref` with its own content.
|
|
364
366
|
const tag = (link) => (options.siteId
|
|
365
|
-
? {
|
|
367
|
+
? {
|
|
368
|
+
...link,
|
|
369
|
+
href: withCampaignUtm(link.href, { siteId: options.siteId, medium: 'footer', campaign: link.id, content: 'footer', origin }),
|
|
370
|
+
untaggedHref: link.href,
|
|
371
|
+
}
|
|
366
372
|
: link);
|
|
367
373
|
return {
|
|
368
374
|
locale,
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
export declare const POSTHOG_EVENTS: Readonly<{
|
|
2
|
+
readonly cta_click: readonly ["category", "label", "location", "destination_host", "is_external"];
|
|
3
|
+
readonly promoto_impression: readonly ["promo_id", "position", "rotating"];
|
|
4
|
+
readonly promoto_dismissed: readonly ["promo_id", "visible_ms", "seen_count"];
|
|
5
|
+
readonly promoto_paged: readonly ["from", "to", "method"];
|
|
6
|
+
readonly not_found_viewed: readonly ["path", "referrer_domain"];
|
|
7
|
+
readonly search_used: readonly ["query_length", "results_count", "locale", "query"];
|
|
8
|
+
readonly search_result_clicked: readonly ["rank", "results_count"];
|
|
9
|
+
readonly code_copied: readonly ["language", "page_type", "is_install_page"];
|
|
10
|
+
readonly article_promotion_impression: readonly ["variant", "path"];
|
|
11
|
+
readonly locale_changed: readonly ["from", "to"];
|
|
12
|
+
readonly locale_redirected: readonly ["from", "to", "reason"];
|
|
13
|
+
readonly theme_changed: readonly ["theme"];
|
|
14
|
+
readonly content_width_changed: readonly ["choice"];
|
|
15
|
+
}>;
|
|
16
|
+
export type PostHogEventName = keyof typeof POSTHOG_EVENTS;
|
|
17
|
+
export type PostHogEventProperty<E extends PostHogEventName> = (typeof POSTHOG_EVENTS)[E][number];
|
|
18
|
+
/**
|
|
19
|
+
* Properties an event cannot be sent without. When one is missing or invalid after sanitizing, the whole
|
|
20
|
+
* event is dropped. Events absent from this map need no required property.
|
|
21
|
+
*/
|
|
22
|
+
export declare const POSTHOG_REQUIRED_PROPERTIES: Readonly<Partial<Record<PostHogEventName, readonly string[]>>>;
|
|
23
|
+
/** Closed value lists for enumerated properties. */
|
|
24
|
+
export declare const POSTHOG_REDIRECT_REASONS: readonly ["query", "stored", "browser", "default"];
|
|
25
|
+
export declare const POSTHOG_CONTENT_WIDTHS: readonly ["narrow", "wide"];
|
|
26
|
+
export declare const POSTHOG_SUPER_PROPERTIES: readonly ["site_id", "site_framework", "hagilight_version", "locale", "page_type"];
|
|
27
|
+
export type PostHogSuperProperty = (typeof POSTHOG_SUPER_PROPERTIES)[number];
|
|
28
|
+
export declare const POSTHOG_PAGE_TYPES: readonly ["home", "docs", "blog", "compare", "404", "other"];
|
|
29
|
+
export type PostHogPageType = (typeof POSTHOG_PAGE_TYPES)[number];
|
|
30
|
+
export declare const POSTHOG_FRAMEWORKS: readonly ["astro", "starlight", "docusaurus"];
|
|
31
|
+
export type PostHogFramework = (typeof POSTHOG_FRAMEWORKS)[number];
|
|
32
|
+
export type PostHogPropertyValue = string | number | boolean;
|
|
33
|
+
/** Caller input for an event. Values are untrusted until `sanitizePostHogProperties` checks them. */
|
|
34
|
+
export type PostHogInput<E extends PostHogEventName> = Partial<Record<PostHogEventProperty<E>, unknown>>;
|
|
35
|
+
/** The sanitized properties of an event: only allowlisted keys, with values that the SDK can send. */
|
|
36
|
+
export type PostHogProperties<E extends PostHogEventName> = Partial<Record<PostHogEventProperty<E>, PostHogPropertyValue>>;
|
|
37
|
+
/** The part of the PostHog SDK that Hagilight reads and writes. */
|
|
38
|
+
export interface PostHogClient {
|
|
39
|
+
capture?: (event: string, properties: Record<string, PostHogPropertyValue>, options?: {
|
|
40
|
+
transport: 'sendBeacon';
|
|
41
|
+
}) => unknown;
|
|
42
|
+
register?: (properties: Record<string, string>) => unknown;
|
|
43
|
+
has_opted_out_capturing?: () => boolean;
|
|
44
|
+
__loaded?: boolean;
|
|
45
|
+
}
|
|
46
|
+
/** The destination of a link: its lowercase host, and whether it differs from the current page's host. */
|
|
47
|
+
export interface PostHogDestination {
|
|
48
|
+
/** Omitted for non-HTTP(S) or unparseable destinations. */
|
|
49
|
+
host?: string;
|
|
50
|
+
isExternal: boolean;
|
|
51
|
+
}
|
|
52
|
+
export interface PagePathOptions {
|
|
53
|
+
/** The site's base path, for example `/docs/`. Defaults to `/`. */
|
|
54
|
+
base?: string;
|
|
55
|
+
/** Locale path segments to skip before classifying. */
|
|
56
|
+
locales?: readonly string[];
|
|
57
|
+
}
|
|
58
|
+
export type PagePathType = Exclude<PostHogPageType, '404' | 'other'>;
|
|
59
|
+
/**
|
|
60
|
+
* Resolves a destination against the current page. Returns only a host and a flag, so a path, query
|
|
61
|
+
* or hash cannot reach an event by construction. Never throws.
|
|
62
|
+
*/
|
|
63
|
+
export declare function destinationOf(url: unknown, baseHref?: string): PostHogDestination;
|
|
64
|
+
/** Returns the path with its query and fragment removed, or `undefined` unless it starts with `/`. */
|
|
65
|
+
export declare function sanitizePath(value: unknown): string | undefined;
|
|
66
|
+
/** The lowercase host name of a referrer, or `undefined` when it is empty, relative, or unparseable. */
|
|
67
|
+
export declare function referrerDomain(value: unknown): string | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* Keeps only the event's allowlisted properties whose values are strings, finite numbers or booleans.
|
|
70
|
+
* Returns an empty object for an unknown event. Never throws.
|
|
71
|
+
*/
|
|
72
|
+
export declare function sanitizePostHogProperties<E extends PostHogEventName>(event: E, input: unknown): PostHogProperties<E>;
|
|
73
|
+
/** Returns the page type when it is in the vocabulary, and `other` for anything else. */
|
|
74
|
+
export declare function resolvePageType(value: unknown): PostHogPageType;
|
|
75
|
+
/**
|
|
76
|
+
* Classifies a pathname for integrations without a per-page source. Never returns `404`: a page that
|
|
77
|
+
* is not recognized is a `docs` page, because most Hagilight sites serve their documentation at the root.
|
|
78
|
+
*/
|
|
79
|
+
export declare function classifyPagePath(pathname: string, { base, locales }?: PagePathOptions): PagePathType;
|
|
80
|
+
/**
|
|
81
|
+
* The only Hagilight code path that calls `posthog.capture`. It sends nothing and throws nothing when
|
|
82
|
+
* `window.posthog` is absent, when the SDK is loaded and opted out, or when no property survives sanitizing.
|
|
83
|
+
* While the SDK is still a stub, the opt-out check is skipped and the capture queues; the SDK applies
|
|
84
|
+
* opt-out when it replays the queue.
|
|
85
|
+
*/
|
|
86
|
+
export declare function capturePostHogEvent<E extends PostHogEventName>(event: E, input: PostHogInput<E>, posthog?: PostHogClient | undefined): void;
|
|
87
|
+
/** Builds the `cta_click` input for a tagged click. Never throws. */
|
|
88
|
+
export declare function handlePostHogClick(event: {
|
|
89
|
+
composedPath(): readonly unknown[];
|
|
90
|
+
}, capture: (name: 'cta_click', input: PostHogInput<'cta_click'>) => void, baseHref?: string): void;
|
|
91
|
+
/**
|
|
92
|
+
* Installs one passive capture-phase click listener per document, independent of Google Analytics.
|
|
93
|
+
* It never cancels, stops or mutates the click. Returns `false` when the document is already instrumented.
|
|
94
|
+
*/
|
|
95
|
+
export declare function installPostHogClickTracking(target?: Pick<EventTarget, 'addEventListener'>, getPosthog?: () => PostHogClient | undefined): boolean;
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { readTaggedClick } from './analytics-events.js';
|
|
2
|
+
/*
|
|
3
|
+
* The PostHog event vocabulary: the only place Hagilight event names, per-event property allowlists,
|
|
4
|
+
* super-property keys, page types and framework names are spelled. Browser-safe: no Node or Astro imports.
|
|
5
|
+
* Adding an event needs only a new `POSTHOG_EVENTS` entry and a snapshot update in the same change.
|
|
6
|
+
*/
|
|
7
|
+
export const POSTHOG_EVENTS = Object.freeze({
|
|
8
|
+
cta_click: ['category', 'label', 'location', 'destination_host', 'is_external'],
|
|
9
|
+
// Promotion banner events. `promo_id` equals the CTA's `data-ga-label`; positions are 1-based.
|
|
10
|
+
promoto_impression: ['promo_id', 'position', 'rotating'],
|
|
11
|
+
promoto_dismissed: ['promo_id', 'visible_ms', 'seen_count'],
|
|
12
|
+
promoto_paged: ['from', 'to', 'method'],
|
|
13
|
+
// Interaction events. Search text, code contents and full URLs never appear in any property.
|
|
14
|
+
not_found_viewed: ['path', 'referrer_domain'],
|
|
15
|
+
search_used: ['query_length', 'results_count', 'locale', 'query'],
|
|
16
|
+
search_result_clicked: ['rank', 'results_count'],
|
|
17
|
+
code_copied: ['language', 'page_type', 'is_install_page'],
|
|
18
|
+
article_promotion_impression: ['variant', 'path'],
|
|
19
|
+
locale_changed: ['from', 'to'],
|
|
20
|
+
locale_redirected: ['from', 'to', 'reason'],
|
|
21
|
+
theme_changed: ['theme'],
|
|
22
|
+
content_width_changed: ['choice'],
|
|
23
|
+
});
|
|
24
|
+
/**
|
|
25
|
+
* Properties an event cannot be sent without. When one is missing or invalid after sanitizing, the whole
|
|
26
|
+
* event is dropped. Events absent from this map need no required property.
|
|
27
|
+
*/
|
|
28
|
+
export const POSTHOG_REQUIRED_PROPERTIES = Object.freeze({
|
|
29
|
+
not_found_viewed: ['path'],
|
|
30
|
+
search_used: ['query_length', 'results_count', 'locale'],
|
|
31
|
+
search_result_clicked: ['rank', 'results_count'],
|
|
32
|
+
code_copied: ['language', 'page_type', 'is_install_page'],
|
|
33
|
+
article_promotion_impression: ['variant', 'path'],
|
|
34
|
+
locale_changed: ['from', 'to'],
|
|
35
|
+
locale_redirected: ['from', 'to', 'reason'],
|
|
36
|
+
theme_changed: ['theme'],
|
|
37
|
+
content_width_changed: ['choice'],
|
|
38
|
+
});
|
|
39
|
+
/** Closed value lists for enumerated properties. */
|
|
40
|
+
export const POSTHOG_REDIRECT_REASONS = ['query', 'stored', 'browser', 'default'];
|
|
41
|
+
export const POSTHOG_CONTENT_WIDTHS = ['narrow', 'wide'];
|
|
42
|
+
export const POSTHOG_SUPER_PROPERTIES = ['site_id', 'site_framework', 'hagilight_version', 'locale', 'page_type'];
|
|
43
|
+
export const POSTHOG_PAGE_TYPES = ['home', 'docs', 'blog', 'compare', '404', 'other'];
|
|
44
|
+
export const POSTHOG_FRAMEWORKS = ['astro', 'starlight', 'docusaurus'];
|
|
45
|
+
const HTTP_PROTOCOLS = new Set(['http:', 'https:']);
|
|
46
|
+
const HOST_PATTERN = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/u;
|
|
47
|
+
const ABSOLUTE_URL_PATTERN = /^https?:\/\//iu;
|
|
48
|
+
/**
|
|
49
|
+
* Resolves a destination against the current page. Returns only a host and a flag, so a path, query
|
|
50
|
+
* or hash cannot reach an event by construction. Never throws.
|
|
51
|
+
*/
|
|
52
|
+
export function destinationOf(url, baseHref) {
|
|
53
|
+
try {
|
|
54
|
+
if (typeof url !== 'string' || !url.trim())
|
|
55
|
+
return { isExternal: false };
|
|
56
|
+
const target = new URL(url, baseHref || undefined);
|
|
57
|
+
if (!HTTP_PROTOCOLS.has(target.protocol))
|
|
58
|
+
return { isExternal: false };
|
|
59
|
+
const host = target.hostname.toLowerCase();
|
|
60
|
+
let currentHost;
|
|
61
|
+
try {
|
|
62
|
+
currentHost = new URL(baseHref ?? '').hostname.toLowerCase();
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
// Without a readable page URL, every resolvable host counts as external.
|
|
66
|
+
}
|
|
67
|
+
return { host, isExternal: host !== currentHost };
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return { isExternal: false };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const MAX_QUERY_CODE_POINTS = 64;
|
|
74
|
+
const LANGUAGE_PATTERN = /^[a-z0-9+#._-]{1,32}$/u;
|
|
75
|
+
const VARIANT_PATTERN = /^[a-z0-9._-]{1,64}$/u;
|
|
76
|
+
const LOCALE_PATTERN = /^[A-Za-z]{2,8}(?:-[A-Za-z0-9]{2,8})*$/u;
|
|
77
|
+
const THEME_PATTERN = /^[a-z0-9-]{1,40}$/u;
|
|
78
|
+
const COUNT_PROPERTIES = new Set(['query_length', 'results_count']);
|
|
79
|
+
/** Returns the path with its query and fragment removed, or `undefined` unless it starts with `/`. */
|
|
80
|
+
export function sanitizePath(value) {
|
|
81
|
+
if (typeof value !== 'string')
|
|
82
|
+
return undefined;
|
|
83
|
+
let path = value.trim();
|
|
84
|
+
if (ABSOLUTE_URL_PATTERN.test(path)) {
|
|
85
|
+
try {
|
|
86
|
+
path = new URL(path).pathname;
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
path = path.split(/[?#]/u, 1)[0] ?? '';
|
|
93
|
+
return path.startsWith('/') ? path : undefined;
|
|
94
|
+
}
|
|
95
|
+
/** The lowercase host name of a referrer, or `undefined` when it is empty, relative, or unparseable. */
|
|
96
|
+
export function referrerDomain(value) {
|
|
97
|
+
if (typeof value !== 'string' || !value.trim())
|
|
98
|
+
return undefined;
|
|
99
|
+
const host = destinationOf(value).host;
|
|
100
|
+
return host !== undefined && HOST_PATTERN.test(host) ? host : undefined;
|
|
101
|
+
}
|
|
102
|
+
function sanitizeValue(property, value) {
|
|
103
|
+
if (typeof value === 'boolean')
|
|
104
|
+
return value;
|
|
105
|
+
if (typeof value === 'number') {
|
|
106
|
+
if (!Number.isFinite(value))
|
|
107
|
+
return undefined;
|
|
108
|
+
if (COUNT_PROPERTIES.has(property))
|
|
109
|
+
return Number.isInteger(value) && value >= 0 ? value : undefined;
|
|
110
|
+
if (property === 'rank')
|
|
111
|
+
return Number.isInteger(value) && value >= 1 ? value : undefined;
|
|
112
|
+
return value;
|
|
113
|
+
}
|
|
114
|
+
if (typeof value !== 'string')
|
|
115
|
+
return undefined;
|
|
116
|
+
// Numeric and boolean properties never accept text, whatever it contains.
|
|
117
|
+
if (COUNT_PROPERTIES.has(property) || property === 'rank' || property === 'is_install_page')
|
|
118
|
+
return undefined;
|
|
119
|
+
if (property === 'path')
|
|
120
|
+
return sanitizePath(value);
|
|
121
|
+
if (property === 'referrer_domain')
|
|
122
|
+
return referrerDomain(value) ?? (HOST_PATTERN.test(value.toLowerCase()) ? value.toLowerCase() : undefined);
|
|
123
|
+
// A URL-valued input keeps only its host: never a path, query, hash or embedded credentials.
|
|
124
|
+
if (ABSOLUTE_URL_PATTERN.test(value))
|
|
125
|
+
return destinationOf(value, undefined).host;
|
|
126
|
+
if (property === 'destination_host') {
|
|
127
|
+
const host = value.toLowerCase();
|
|
128
|
+
return HOST_PATTERN.test(host) ? host : undefined;
|
|
129
|
+
}
|
|
130
|
+
switch (property) {
|
|
131
|
+
case 'query': {
|
|
132
|
+
const text = value.trim();
|
|
133
|
+
if (!text || /[/\\]/u.test(text))
|
|
134
|
+
return undefined;
|
|
135
|
+
return [...text].slice(0, MAX_QUERY_CODE_POINTS).join('');
|
|
136
|
+
}
|
|
137
|
+
case 'language':
|
|
138
|
+
return LANGUAGE_PATTERN.test(value) || value === 'unknown' ? value : undefined;
|
|
139
|
+
case 'page_type':
|
|
140
|
+
return POSTHOG_PAGE_TYPES.includes(value) ? value : undefined;
|
|
141
|
+
case 'variant':
|
|
142
|
+
return VARIANT_PATTERN.test(value) || value === 'default' ? value : undefined;
|
|
143
|
+
case 'locale':
|
|
144
|
+
case 'from':
|
|
145
|
+
case 'to':
|
|
146
|
+
return LOCALE_PATTERN.test(value) ? value : undefined;
|
|
147
|
+
case 'reason':
|
|
148
|
+
return POSTHOG_REDIRECT_REASONS.includes(value) ? value : undefined;
|
|
149
|
+
case 'theme':
|
|
150
|
+
return THEME_PATTERN.test(value) ? value : undefined;
|
|
151
|
+
case 'choice':
|
|
152
|
+
return POSTHOG_CONTENT_WIDTHS.includes(value) ? value : undefined;
|
|
153
|
+
default:
|
|
154
|
+
return value;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Keeps only the event's allowlisted properties whose values are strings, finite numbers or booleans.
|
|
159
|
+
* Returns an empty object for an unknown event. Never throws.
|
|
160
|
+
*/
|
|
161
|
+
export function sanitizePostHogProperties(event, input) {
|
|
162
|
+
const sanitized = {};
|
|
163
|
+
try {
|
|
164
|
+
if (!Object.hasOwn(POSTHOG_EVENTS, event) || typeof input !== 'object' || input === null)
|
|
165
|
+
return sanitized;
|
|
166
|
+
for (const property of POSTHOG_EVENTS[event]) {
|
|
167
|
+
const value = sanitizeValue(property, input[property]);
|
|
168
|
+
if (value !== undefined)
|
|
169
|
+
sanitized[property] = value;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
// Analytics must never throw into the page.
|
|
174
|
+
}
|
|
175
|
+
return sanitized;
|
|
176
|
+
}
|
|
177
|
+
/** Returns the page type when it is in the vocabulary, and `other` for anything else. */
|
|
178
|
+
export function resolvePageType(value) {
|
|
179
|
+
return POSTHOG_PAGE_TYPES.includes(value) ? value : 'other';
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Classifies a pathname for integrations without a per-page source. Never returns `404`: a page that
|
|
183
|
+
* is not recognized is a `docs` page, because most Hagilight sites serve their documentation at the root.
|
|
184
|
+
*/
|
|
185
|
+
export function classifyPagePath(pathname, { base = '/', locales = [] } = {}) {
|
|
186
|
+
const baseSegments = base.split('/').filter(Boolean);
|
|
187
|
+
let segments = (typeof pathname === 'string' ? pathname : '').split('/').filter(Boolean);
|
|
188
|
+
if (baseSegments.every((segment, index) => segments[index] === segment)) {
|
|
189
|
+
segments = segments.slice(baseSegments.length);
|
|
190
|
+
}
|
|
191
|
+
if (segments.length > 0 && locales.includes(segments[0]))
|
|
192
|
+
segments = segments.slice(1);
|
|
193
|
+
if (segments.length === 0)
|
|
194
|
+
return 'home';
|
|
195
|
+
if (segments[0] === 'blog')
|
|
196
|
+
return 'blog';
|
|
197
|
+
if (segments[0] === 'compare')
|
|
198
|
+
return 'compare';
|
|
199
|
+
return 'docs';
|
|
200
|
+
}
|
|
201
|
+
function windowPostHog() {
|
|
202
|
+
return globalThis.window?.posthog;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* The only Hagilight code path that calls `posthog.capture`. It sends nothing and throws nothing when
|
|
206
|
+
* `window.posthog` is absent, when the SDK is loaded and opted out, or when no property survives sanitizing.
|
|
207
|
+
* While the SDK is still a stub, the opt-out check is skipped and the capture queues; the SDK applies
|
|
208
|
+
* opt-out when it replays the queue.
|
|
209
|
+
*/
|
|
210
|
+
export function capturePostHogEvent(event, input, posthog = windowPostHog()) {
|
|
211
|
+
try {
|
|
212
|
+
if (!Object.hasOwn(POSTHOG_EVENTS, event))
|
|
213
|
+
return;
|
|
214
|
+
const properties = sanitizePostHogProperties(event, input);
|
|
215
|
+
if (Object.keys(properties).length === 0)
|
|
216
|
+
return;
|
|
217
|
+
const required = POSTHOG_REQUIRED_PROPERTIES[event] ?? [];
|
|
218
|
+
if (required.some((name) => properties[name] === undefined))
|
|
219
|
+
return;
|
|
220
|
+
if (!posthog || typeof posthog.capture !== 'function')
|
|
221
|
+
return;
|
|
222
|
+
if (posthog.__loaded === true && typeof posthog.has_opted_out_capturing === 'function' && posthog.has_opted_out_capturing())
|
|
223
|
+
return;
|
|
224
|
+
posthog.capture(event, properties, { transport: 'sendBeacon' });
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
// Analytics must never interfere with the page.
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/** Builds the `cta_click` input for a tagged click. Never throws. */
|
|
231
|
+
export function handlePostHogClick(event, capture, baseHref = globalThis.location?.href ?? '') {
|
|
232
|
+
try {
|
|
233
|
+
const tagged = readTaggedClick(event);
|
|
234
|
+
if (!tagged)
|
|
235
|
+
return;
|
|
236
|
+
const destination = destinationOf(tagged.url, baseHref);
|
|
237
|
+
capture('cta_click', {
|
|
238
|
+
category: tagged.category,
|
|
239
|
+
label: tagged.label,
|
|
240
|
+
location: tagged.location,
|
|
241
|
+
destination_host: destination.host,
|
|
242
|
+
is_external: destination.isExternal,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
catch {
|
|
246
|
+
// Analytics must never interfere with the click.
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const CLICK_TRACKING = Symbol.for('hagilight.posthog-click-tracking');
|
|
250
|
+
/**
|
|
251
|
+
* Installs one passive capture-phase click listener per document, independent of Google Analytics.
|
|
252
|
+
* It never cancels, stops or mutates the click. Returns `false` when the document is already instrumented.
|
|
253
|
+
*/
|
|
254
|
+
export function installPostHogClickTracking(target = document, getPosthog = windowPostHog) {
|
|
255
|
+
const marked = target;
|
|
256
|
+
if (marked[CLICK_TRACKING])
|
|
257
|
+
return false;
|
|
258
|
+
marked[CLICK_TRACKING] = true;
|
|
259
|
+
target.addEventListener('click', (event) => {
|
|
260
|
+
handlePostHogClick(event, (name, input) => capturePostHogEvent(name, input, getPosthog()));
|
|
261
|
+
}, { capture: true, passive: true });
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { type PostHogClient } from './posthog-events.js';
|
|
2
|
+
export declare const SEARCH_DEBOUNCE_MS = 800;
|
|
3
|
+
export declare const SEARCH_SETTLE_TIMEOUT_MS = 2000;
|
|
4
|
+
export declare const SEARCH_POLL_MS = 100;
|
|
5
|
+
/** Minimal element surface the observers read. Browser elements satisfy it. */
|
|
6
|
+
export interface InteractionElement {
|
|
7
|
+
closest(selector: string): InteractionElement | null;
|
|
8
|
+
querySelector(selector: string): InteractionElement | null;
|
|
9
|
+
querySelectorAll(selector: string): ArrayLike<InteractionElement>;
|
|
10
|
+
getAttribute(name: string): string | null;
|
|
11
|
+
readonly textContent?: string | null;
|
|
12
|
+
readonly value?: string;
|
|
13
|
+
}
|
|
14
|
+
/** Minimal document surface: the delegated listeners, the page language and the page-type meta element. */
|
|
15
|
+
export interface InteractionDocument {
|
|
16
|
+
readonly documentElement: {
|
|
17
|
+
readonly lang?: string;
|
|
18
|
+
};
|
|
19
|
+
addEventListener(type: string, listener: (event: InteractionEvent) => void, options?: boolean | {
|
|
20
|
+
capture?: boolean;
|
|
21
|
+
}): void;
|
|
22
|
+
querySelector(selector: string): InteractionElement | null;
|
|
23
|
+
}
|
|
24
|
+
export interface InteractionEvent {
|
|
25
|
+
readonly target: unknown;
|
|
26
|
+
}
|
|
27
|
+
/** Timers are injected so tests can drive the debounce and the settle wait deterministically. */
|
|
28
|
+
export interface InteractionTimers {
|
|
29
|
+
setTimeout(callback: () => void, ms: number): unknown;
|
|
30
|
+
clearTimeout(handle: unknown): void;
|
|
31
|
+
}
|
|
32
|
+
export interface InteractionEnvironment {
|
|
33
|
+
timers: InteractionTimers;
|
|
34
|
+
posthog(): PostHogClient | undefined;
|
|
35
|
+
/** The page's `window.__hagilightPosthogConfig`, written by the init script only for a non-default option. */
|
|
36
|
+
config(): {
|
|
37
|
+
searchQuery?: unknown;
|
|
38
|
+
} | undefined;
|
|
39
|
+
pathname(): string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns the search text when it is safe to send, or `undefined`. The whole text is checked before it is
|
|
43
|
+
* truncated, so a secret that starts after the 64th character still withholds the text.
|
|
44
|
+
*/
|
|
45
|
+
export declare function sanitizeSearchQuery(value: unknown): string | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* The result count of a settled Pagefind list. The first number of the message is the total, after the query
|
|
48
|
+
* text is removed so digits typed by the visitor are not counted, and locale grouping (`1,234`) is folded.
|
|
49
|
+
* With no number, an empty list is zero results; anything else is unknown and sends nothing.
|
|
50
|
+
*/
|
|
51
|
+
export declare function readResultsCount(root: InteractionElement, query: string): number | undefined;
|
|
52
|
+
/** A declared language from `data-language` or a `language-*` class, lowercased; `unknown` otherwise. */
|
|
53
|
+
export declare function normalizeCodeLanguage(value: string | null | undefined): string;
|
|
54
|
+
/**
|
|
55
|
+
* Installs the delegated interaction observers once per document. Returns `false` when already installed.
|
|
56
|
+
* Every handler is wrapped so an observer failure never reaches the page.
|
|
57
|
+
*/
|
|
58
|
+
export declare function installPostHogInteractions(doc?: InteractionDocument, overrides?: Partial<InteractionEnvironment>): boolean;
|
|
59
|
+
export declare const PROMOTION_IMPRESSION_THRESHOLD = 0.5;
|
|
60
|
+
export declare const PROMOTION_IMPRESSION_DWELL_MS = 1000;
|
|
61
|
+
/** The pool key of the centre fan card, from its image file name, or `default` when no fan was dealt. */
|
|
62
|
+
export declare function promotionVariantOf(card: InteractionElement): string;
|
|
63
|
+
interface ImpressionObserver {
|
|
64
|
+
observe(target: unknown): void;
|
|
65
|
+
disconnect(): void;
|
|
66
|
+
}
|
|
67
|
+
export interface PromotionImpressionEnvironment {
|
|
68
|
+
timers: InteractionTimers;
|
|
69
|
+
posthog(): PostHogClient | undefined;
|
|
70
|
+
pathname(): string;
|
|
71
|
+
IntersectionObserver?: new (callback: (entries: readonly {
|
|
72
|
+
isIntersecting: boolean;
|
|
73
|
+
intersectionRatio: number;
|
|
74
|
+
}[]) => void, options: {
|
|
75
|
+
threshold: number;
|
|
76
|
+
}) => ImpressionObserver;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Observes one promotion card and sends `article_promotion_impression` after sustained visibility. Returns
|
|
80
|
+
* `false` without an IntersectionObserver, so no event is sent there. Never throws.
|
|
81
|
+
*/
|
|
82
|
+
export declare function observePromotionImpression(card: InteractionElement, overrides?: Partial<PromotionImpressionEnvironment>): boolean;
|
|
83
|
+
/** Observes the page's promotion card, if the page has one. */
|
|
84
|
+
export declare function installPromotionImpression(doc?: InteractionDocument): boolean;
|
|
85
|
+
export {};
|