@hagicode/hagilight-core 0.9.8-dev.115.1.3488f89 → 0.9.8

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.
@@ -4,14 +4,17 @@ import type { PromotionCard } from './dist/promotions.js';
4
4
  interface Props {
5
5
  fallback?: PromotionCard;
6
6
  locale?: string;
7
+ /** Identifies the hosting site in campaign tags; omitted leaves destinations untagged. */
8
+ siteId?: string;
7
9
  }
8
10
 
9
- const { fallback, locale = Astro.currentLocale ?? 'en' } = Astro.props;
11
+ const { fallback, locale = Astro.currentLocale ?? 'en', siteId } = Astro.props;
10
12
  ---
11
13
 
12
14
  <hagilight-promoto-banner
13
15
  class="hagilight-promoto"
14
16
  data-locale={locale}
17
+ data-site-id={siteId?.trim() || undefined}
15
18
  data-fallback={fallback ? JSON.stringify(fallback) : undefined}
16
19
  hidden
17
20
  >
package/README.md CHANGED
@@ -108,6 +108,51 @@ The banner stays hidden while the page is at the top during a domain's first 30
108
108
 
109
109
  A promotion's `image` may also carry `sources`, the alternative image files that Index publishes in the order AVIF, WebP, then the original. The banner renders them as a `<picture>` in that order, with the original as its `<img>`, and skips any entry whose `type` is not an `image/*` media type or whose URL is unsafe. Each `src` is resolved against `https://index.hagicode.com` when the promotion documents load. If the browser fails to load a tier, the banner removes it and the next tier is used. A card without `sources` renders the plain `<img>` as before, and `sources` never affect the dismissal signature, so a dismissed banner stays dismissed when only its image chain changes.
110
110
 
111
+ ## First-party links and campaign tags
112
+
113
+ Shared link resolution classifies every destination from its parsed URL, never from substring matching, and chooses the relation attribute by that class.
114
+
115
+ | Class | Destinations | `rel` on a new-tab link |
116
+ | --- | --- | --- |
117
+ | `same-origin` | Relative URLs, or the site's own origin when `siteUrl` is set | `noopener` |
118
+ | `first-party` | Host `hagicode.com` or a subdomain of it (case-insensitive); `github.com` under the `HagiCode-org` organization | `noopener` |
119
+ | `third-party` | Every other `http:`/`https:` destination, and any other protocol | `noopener noreferrer` |
120
+
121
+ Look-alikes are third-party: `hagicode.com.evil.example`, `nothagicode.com`, `github.com/HagiCode-org-fake/site`, and `example.com/hagicode.com`. Same-tab links get no policy `rel`. The site-wide `Referrer-Policy` is unchanged, so a first-party link sends only the origin, and no site should declare `unsafe-url`.
122
+
123
+ The helpers are exported from `@hagicode/hagilight-core/links`: `classifyLink(href, { origin? })`, `getLinkRel(class)`, and `withCampaignUtm(href, { siteId, medium, origin? })`. `SiteLink.rel` is typed `'noopener' | 'noopener noreferrer'`.
124
+
125
+ ### Campaign tags
126
+
127
+ Set `siteId` to the identifier of the hosting site, and first-party `hagicode.com` destinations in the footer get campaign parameters:
128
+
129
+ ```js
130
+ resolveSiteLinks('en-US', { siteId: 'docs' });
131
+ // quick → downloadClient: https://www.hagicode.com/en-US/desktop/?utm_source=docs&utm_medium=footer
132
+ ```
133
+
134
+ - Footer `quick`, `community` and related-site entries are tagged with `utm_medium=footer`. The banner call to action is tagged with `utm_medium=promoto`. Header and filing links are not tagged.
135
+ - Only `https:` destinations on `hagicode.com` hosts are tagged. GitHub, `mailto:`, `tel:`, third-party, same-origin and relative destinations are left unchanged, and so is every destination when `siteId` is absent or blank.
136
+ - Existing `utm_source` or `utm_medium` values win. Other query parameters and the fragment are kept.
137
+ - Tags are added after deduplication, so a tagged and a clean destination still collapse into one entry. `normalizeUrl` is unchanged. Canonical URLs, `og:url` and sitemap entries are built from page URLs and never contain `utm_*`.
138
+ - Footer clicks report the tagged `href` as `link_url` in the GA click event. The `link_url` dimension therefore includes `utm_source` and `utm_medium` for first-party footer clicks. No new event parameter is added.
139
+
140
+ ### Options
141
+
142
+ - `links.siteId` (Starlight, Docusaurus, and the `Footer`/`Header` components) enables footer tags. The Starlight and Docusaurus promotion banners read this same value.
143
+ - `PromotoBanner` accepts an optional `siteId` prop, written to `data-site-id`. Without it the banner keeps the class-based `rel` and leaves destinations unchanged.
144
+ - Each slide is classified and tagged on its own when it renders, so a rotation from a third-party campaign to a first-party one gets the first-party `rel` and tags.
145
+
146
+ ### Google Analytics cross-domain linker
147
+
148
+ Decision, recorded 2026-10-15 under the `first-party-link-policy-and-attribution` change: **no linker domain list is configured.**
149
+
150
+ - Every first-party host that runs HagiCode's shared `gtag` is a subdomain of `hagicode.com` (`www`, `docs`, `cost`, `design`, `index`, `soul`, `omniroute`, `openspec`, `impeccable`, `builder`, `newbe`, `awesome`, `tasks`, `trait`, `status`, `api`, and the download hosts `dl-server`, `server.dl`, `desktop.dl`, `dl-desktop`). They share the measurement ID `G-EN03FMT2Q4`, and GA4 sets its cookie on the registrable domain `hagicode.com`, so `_ga` and the session already follow a visitor between these subdomains. A linker would add nothing for them.
151
+ - One first-party destination sits on another registrable domain: `github.com/HagiCode-org/*`. It runs no HagiCode `gtag` and cannot read a `_gl` parameter, so a linker would only append `_gl` to GitHub links and make them longer. No linker is needed for it.
152
+ - Other hosts that appear in the sites, such as `hagicode-org.github.io` and Azure blob storage, are third-party under this policy and are outside the decision.
153
+ - Constraints that any future linker must respect: one `gtag('config', id)` call per page, so a `linker` setting is added to that single call in `GoogleAnalytics.astro`; the sites with their own inline `gtag` copies would each need the same change; and `_gl` must never reach canonical or sitemap URLs.
154
+ - Revisit this decision when a HagiCode-owned site runs the shared `gtag` on a registrable domain other than `hagicode.com`, or when a first-party destination on another domain also runs HagiCode's `gtag` and needs session continuity. Then list the host, add it to the linker in the single `config` call, and update the GA reference document, which lives in the monorepo root and needs its owner's follow-up edit.
155
+
111
156
  ## Google Analytics click events
112
157
 
113
158
  `GoogleAnalytics` also reports clicks on the key links and download buttons that the shared components render. There is nothing to configure: events follow the same rules as page views, so they are sent only on production pages where Google Analytics is enabled, never on the Starlight 404 page, and silently skipped when `gtag` is missing (development builds, blocked scripts).
@@ -0,0 +1,33 @@
1
+ /**
2
+ * First-party link policy shared by the footer, header and promotion banner.
3
+ *
4
+ * Kept free of JSON imports so the browser-side promotion banner can use it
5
+ * without pulling in the link catalog. `links.ts` re-exports these helpers.
6
+ */
7
+ export type LinkClass = 'same-origin' | 'first-party' | 'third-party';
8
+ export type LinkRel = 'noopener' | 'noopener noreferrer';
9
+ export type CampaignMedium = 'footer' | 'promoto';
10
+ export interface ClassifyLinkOptions {
11
+ /** The hosting site's origin, e.g. `https://docs.hagicode.com`, when known. */
12
+ origin?: string;
13
+ }
14
+ export interface CampaignTagOptions extends ClassifyLinkOptions {
15
+ /** The hosting site's identifier. Without it, no tag is added. */
16
+ siteId?: string | null;
17
+ medium: CampaignMedium;
18
+ }
19
+ /**
20
+ * Classifies a destination from its parsed URL. Relative destinations are
21
+ * same-origin; anything that is not http(s) is treated as third-party, which
22
+ * keeps a strict relation attribute on it.
23
+ */
24
+ export declare function classifyLink(href: string, options?: ClassifyLinkOptions): LinkClass;
25
+ /** The relation attribute for a new-tab link of the given class. */
26
+ export declare function getLinkRel(linkClass: LinkClass): LinkRel;
27
+ /**
28
+ * Adds `utm_source=<siteId>` and `utm_medium=<medium>` to first-party
29
+ * `hagicode.com` destinations. Existing `utm_*` keys win, other parameters and
30
+ * the fragment are preserved, and every other class of destination is returned
31
+ * unchanged.
32
+ */
33
+ export declare function withCampaignUtm(href: string, options: CampaignTagOptions): string;
@@ -0,0 +1,93 @@
1
+ /**
2
+ * First-party link policy shared by the footer, header and promotion banner.
3
+ *
4
+ * Kept free of JSON imports so the browser-side promotion banner can use it
5
+ * without pulling in the link catalog. `links.ts` re-exports these helpers.
6
+ */
7
+ const PLACEHOLDER_ORIGIN = 'https://hagilight.invalid';
8
+ const SCHEME_PATTERN = /^[a-z][a-z\d+.-]*:/iu;
9
+ const FIRST_PARTY_GITHUB_ORG = 'hagicode-org';
10
+ function originOf(value) {
11
+ if (!value)
12
+ return undefined;
13
+ try {
14
+ const { origin } = new URL(value);
15
+ return origin === 'null' ? undefined : origin;
16
+ }
17
+ catch {
18
+ return undefined;
19
+ }
20
+ }
21
+ function isFirstPartyUrl(url) {
22
+ const host = url.hostname.toLowerCase();
23
+ if (host === 'hagicode.com' || host.endsWith('.hagicode.com'))
24
+ return true;
25
+ if (host !== 'github.com')
26
+ return false;
27
+ const [organization] = url.pathname.split('/').filter(Boolean);
28
+ return organization?.toLowerCase() === FIRST_PARTY_GITHUB_ORG;
29
+ }
30
+ /**
31
+ * Classifies a destination from its parsed URL. Relative destinations are
32
+ * same-origin; anything that is not http(s) is treated as third-party, which
33
+ * keeps a strict relation attribute on it.
34
+ */
35
+ export function classifyLink(href, options = {}) {
36
+ const input = href.trim();
37
+ if (!input)
38
+ return 'third-party';
39
+ if (!SCHEME_PATTERN.test(input) && !input.startsWith('//'))
40
+ return 'same-origin';
41
+ let url;
42
+ try {
43
+ url = new URL(input, options.origin ?? PLACEHOLDER_ORIGIN);
44
+ }
45
+ catch {
46
+ return 'third-party';
47
+ }
48
+ if (url.protocol !== 'http:' && url.protocol !== 'https:')
49
+ return 'third-party';
50
+ if (url.origin === originOf(options.origin))
51
+ return 'same-origin';
52
+ return isFirstPartyUrl(url) ? 'first-party' : 'third-party';
53
+ }
54
+ /** The relation attribute for a new-tab link of the given class. */
55
+ export function getLinkRel(linkClass) {
56
+ return linkClass === 'third-party' ? 'noopener noreferrer' : 'noopener';
57
+ }
58
+ /**
59
+ * Adds `utm_source=<siteId>` and `utm_medium=<medium>` to first-party
60
+ * `hagicode.com` destinations. Existing `utm_*` keys win, other parameters and
61
+ * the fragment are preserved, and every other class of destination is returned
62
+ * unchanged.
63
+ */
64
+ export function withCampaignUtm(href, options) {
65
+ const siteId = options.siteId?.trim();
66
+ if (!siteId)
67
+ return href;
68
+ let url;
69
+ try {
70
+ url = new URL(href.trim(), options.origin ?? PLACEHOLDER_ORIGIN);
71
+ }
72
+ catch {
73
+ return href;
74
+ }
75
+ if (url.protocol !== 'https:')
76
+ return href;
77
+ if (url.origin === originOf(options.origin))
78
+ return href;
79
+ const host = url.hostname.toLowerCase();
80
+ if (host !== 'hagicode.com' && !host.endsWith('.hagicode.com'))
81
+ return href;
82
+ const existingKeys = new Set(url.search.replace(/^\?/u, '').split('&').filter(Boolean).map((pair) => pair.split('=')[0]));
83
+ const additions = [];
84
+ if (!existingKeys.has('utm_source'))
85
+ additions.push(`utm_source=${encodeURIComponent(siteId)}`);
86
+ if (!existingKeys.has('utm_medium'))
87
+ additions.push(`utm_medium=${options.medium}`);
88
+ if (additions.length === 0)
89
+ return href;
90
+ const current = url.search.replace(/^\?/u, '');
91
+ url.search = [current, ...additions].filter(Boolean).join('&');
92
+ return url.toString();
93
+ }
package/dist/links.d.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import type { LinkRel } from './link-policy.js';
2
+ export { classifyLink, getLinkRel, withCampaignUtm } from './link-policy.js';
3
+ export type { CampaignMedium, CampaignTagOptions, ClassifyLinkOptions, LinkClass, LinkRel, } from './link-policy.js';
1
4
  export type SiteLinkKey = 'home' | 'blog' | 'support' | 'downloadClient' | 'about' | 'dockerCompose' | 'productDocs' | 'blogPosts' | 'sitemap' | 'rss' | 'rssLocale' | 'costCalculator' | 'github' | 'discord' | 'issueFeedback' | 'contactEmail' | 'qqGroup' | 'microsoftStore' | 'icpFiling' | 'publicSecurityFiling';
2
5
  export type LinkGroup = 'header' | 'quick' | 'community' | 'filings';
3
6
  export type FooterLinkSection = 'relatedSites' | 'quick' | 'community';
@@ -8,7 +11,7 @@ export interface SiteLink {
8
11
  href: string;
9
12
  ariaLabel?: string;
10
13
  target?: '_blank';
11
- rel?: 'noopener noreferrer';
14
+ rel?: LinkRel;
12
15
  }
13
16
  export interface SiteLinkOverride {
14
17
  href?: LocalizedText;
@@ -62,7 +65,6 @@ export declare function resolveSiteLinks(localeInput?: string | null, options?:
62
65
  description: string | undefined;
63
66
  href: string;
64
67
  target: "_blank";
65
- rel: "noopener noreferrer";
68
+ rel: LinkRel;
66
69
  }[];
67
70
  };
68
- export {};
package/dist/links.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import docsRelatedSites from './related-sites.json' with { type: 'json' };
2
+ import { classifyLink, getLinkRel, withCampaignUtm } from './link-policy.js';
3
+ export { classifyLink, getLinkRel, withCampaignUtm } from './link-policy.js';
2
4
  const translations = {
3
5
  home: {
4
6
  'zh-CN': '首页', 'zh-Hant': '首頁', 'en-US': 'Home', 'ja-JP': 'ホーム',
@@ -238,7 +240,18 @@ function normalizeUrl(value) {
238
240
  url.pathname = url.pathname.replace(/\/+$/u, '') || '/';
239
241
  return url.origin === 'https://hagilight.invalid' ? url.pathname : url.toString();
240
242
  }
241
- function resolveLink(key, locale, definition, override) {
243
+ function siteOriginOf(siteUrl) {
244
+ if (!siteUrl)
245
+ return undefined;
246
+ try {
247
+ const { protocol, origin } = new URL(siteUrl);
248
+ return protocol === 'http:' || protocol === 'https:' ? origin : undefined;
249
+ }
250
+ catch {
251
+ return undefined;
252
+ }
253
+ }
254
+ function resolveLink(key, locale, definition, override, origin) {
242
255
  const defaultHref = typeof definition.href === 'function' ? definition.href(locale) : definition.href;
243
256
  const href = override?.href === undefined ? defaultHref : resolveLocalized(override.href, locale, defaultHref);
244
257
  const label = override?.label === undefined
@@ -253,11 +266,12 @@ function resolveLink(key, locale, definition, override) {
253
266
  ...(definition.ariaLabel
254
267
  ? { ariaLabel: resolveLocalized(definition.ariaLabel, locale, label) }
255
268
  : {}),
256
- ...(external ? { target: '_blank', rel: 'noopener noreferrer' } : {}),
269
+ ...(external ? { target: '_blank', rel: getLinkRel(classifyLink(href, { origin })) } : {}),
257
270
  };
258
271
  }
259
272
  export function resolveSiteLinks(localeInput, options = {}) {
260
273
  const locale = normalizeLocale(localeInput);
274
+ const origin = siteOriginOf(options.siteUrl);
261
275
  if (options.rssFeedUrl !== undefined && typeof options.rssFeedUrl !== 'string') {
262
276
  throw new TypeError('RSS feed URL must be a string.');
263
277
  }
@@ -282,7 +296,7 @@ export function resolveSiteLinks(localeInput, options = {}) {
282
296
  : key === 'rssLocale'
283
297
  ? { ...defaultLinks[key], href: options.rssLocaleFeedUrl ?? '' }
284
298
  : defaultLinks[key];
285
- return resolveLink(key, locale, definition, override);
299
+ return resolveLink(key, locale, definition, override, origin);
286
300
  });
287
301
  const extraLinks = (options.extraLinks?.[group] ?? []).map((entry, index) => {
288
302
  const definition = {
@@ -294,7 +308,7 @@ export function resolveSiteLinks(localeInput, options = {}) {
294
308
  if (typeof id !== 'string' || !id.trim()) {
295
309
  throw new TypeError('Extra link IDs must be non-empty strings.');
296
310
  }
297
- return resolveLink(id, locale, definition);
311
+ return resolveLink(id, locale, definition, undefined, origin);
298
312
  });
299
313
  const links = [...catalogLinks, ...extraLinks].filter((link) => link !== undefined);
300
314
  if (group !== 'quick' && group !== 'community')
@@ -342,9 +356,14 @@ export function resolveSiteLinks(localeInput, options = {}) {
342
356
  : undefined,
343
357
  href,
344
358
  target: '_blank',
345
- rel: 'noopener noreferrer',
359
+ rel: getLinkRel(classifyLink(href, { origin })),
346
360
  }];
347
361
  });
362
+ // Campaign tags are added after deduplication and never reach the
363
+ // dedup sets above, so a tagged and a clean destination still collapse.
364
+ const tag = (link) => (options.siteId
365
+ ? { ...link, href: withCampaignUtm(link.href, { siteId: options.siteId, medium: 'footer', origin }) }
366
+ : link);
348
367
  return {
349
368
  locale,
350
369
  labels: {
@@ -398,9 +417,9 @@ export function resolveSiteLinks(localeInput, options = {}) {
398
417
  },
399
418
  },
400
419
  header: resolvedGroups.header,
401
- quick: resolvedGroups.quick,
402
- community: resolvedGroups.community,
420
+ quick: resolvedGroups.quick.map(tag),
421
+ community: resolvedGroups.community.map(tag),
403
422
  filings: resolvedGroups.filings,
404
- relatedSites,
423
+ relatedSites: relatedSites.map(tag),
405
424
  };
406
425
  }
@@ -1,4 +1,5 @@
1
1
  import { gaEventAttributes } from './analytics-events.js';
2
+ import { classifyLink, getLinkRel, withCampaignUtm } from './link-policy.js';
2
3
  import { loadActivePromotions, } from './promotions.js';
3
4
  const ELEMENT_NAME = 'hagilight-promoto-banner';
4
5
  const DISMISSED_SIGNATURE_KEY = 'hagilight:promoto-banner:dismissed-signature';
@@ -315,9 +316,15 @@ export function definePromotoBannerElement() {
315
316
  copy.append(title, description);
316
317
  const link = document.createElement('a');
317
318
  link.className = 'hagilight-promoto__cta';
318
- link.href = card.link;
319
+ // Classified and tagged per slide, so rotation never reuses a previous decision.
320
+ const origin = globalThis.location?.origin;
321
+ link.href = withCampaignUtm(card.link, {
322
+ siteId: this.dataset.siteId,
323
+ medium: 'promoto',
324
+ origin,
325
+ });
319
326
  link.target = '_blank';
320
- link.rel = 'noopener noreferrer';
327
+ link.rel = getLinkRel(classifyLink(card.link, { origin }));
321
328
  link.textContent = card.ctaLabel;
322
329
  for (const [name, value] of Object.entries(gaEventAttributes({
323
330
  category: 'promotion',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hagicode/hagilight-core",
3
- "version": "0.9.8-dev.115.1.3488f89",
3
+ "version": "0.9.8",
4
4
  "type": "module",
5
5
  "description": "Shared TypeScript utilities, components, and assets for HagiCode Astro and Starlight sites",
6
6
  "repository": {