@easy-web/core 1.3.3 → 1.4.1

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.
Files changed (43) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +9 -9
  3. package/src/components/Header.astro +205 -205
  4. package/src/components/HeaderCentered.astro +230 -230
  5. package/src/components/HeaderFlyout.astro +370 -370
  6. package/src/components/HeaderHideOnScroll.astro +234 -234
  7. package/src/components/LanguageSwitch.astro +82 -82
  8. package/src/components/NotFound.astro +115 -115
  9. package/src/components/PageShell.astro +82 -82
  10. package/src/components/SeoHead.astro +106 -106
  11. package/src/components/StructuredData.astro +1 -1
  12. package/src/components/ThemeToggle.astro +84 -84
  13. package/src/components/UniversalMedia.astro +106 -106
  14. package/src/components/__tests__/StructuredData.test.ts +1 -1
  15. package/src/components/__tests__/UniversalMedia.test.ts +65 -65
  16. package/src/contracts/__tests__/jsonld.test.ts +1 -1
  17. package/src/contracts/index.ts +1 -1
  18. package/src/contracts/jsonld.ts +1 -1
  19. package/src/i18n/__tests__/factory.test.ts +54 -54
  20. package/src/i18n/__tests__/localized-paths.test.ts +258 -258
  21. package/src/i18n/__tests__/routing-multi-locale.test.ts +89 -89
  22. package/src/i18n/__tests__/seo.test.ts +140 -140
  23. package/src/i18n/factory.ts +66 -66
  24. package/src/i18n/index.ts +5 -5
  25. package/src/i18n/localized-paths.ts +181 -181
  26. package/src/i18n/seo.ts +131 -131
  27. package/src/markdown/__tests__/__fixtures__/abs-src-assets.md +1 -1
  28. package/src/markdown/__tests__/__fixtures__/external-url.md +1 -1
  29. package/src/markdown/__tests__/__fixtures__/public-slash.md +1 -1
  30. package/src/markdown/__tests__/__fixtures__/rel-double-dot.md +1 -1
  31. package/src/markdown/__tests__/index.test.ts +60 -60
  32. package/src/markdown/index.ts +1 -1
  33. package/src/markdown/plugin.ts +42 -42
  34. package/src/schemas/__tests__/notFound.test.ts +32 -32
  35. package/src/schemas/notFound.ts +33 -33
  36. package/src/seo/__tests__/crawl-policy.test.ts +50 -50
  37. package/src/seo/__tests__/integration.test.ts +195 -195
  38. package/src/seo/__tests__/no-index-resolution.test.ts +53 -53
  39. package/src/seo/__tests__/robots-txt.test.ts +66 -66
  40. package/src/seo/__tests__/sitemap-options.test.ts +400 -400
  41. package/src/seo/crawl-policy.ts +36 -36
  42. package/src/seo/index.ts +254 -254
  43. package/src/seo/routes/robots-txt.ts +36 -36
@@ -1,89 +1,89 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { localizedHref, getLocaleFromPath } from '../routing.js';
3
- import { createI18n } from '../factory.js';
4
-
5
- describe('localizedHref – three-locale setup (de/en/fr)', () => {
6
- const opts = (path: string, locale: string) =>
7
- localizedHref({ path, locale, defaultLocale: 'de' });
8
-
9
- it('fr prefix on root path', () => {
10
- expect(opts('/', 'fr')).toBe('/fr');
11
- });
12
- it('fr prefix on /about', () => {
13
- expect(opts('/about', 'fr')).toBe('/fr/about');
14
- });
15
- it('fr prefix on deep path', () => {
16
- expect(opts('/news/2024/article', 'fr')).toBe('/fr/news/2024/article');
17
- });
18
- it('strips /fr prefix for fr locale (idempotent)', () => {
19
- expect(opts('/fr/about', 'fr')).toBe('/fr/about');
20
- });
21
- it('does not strip a foreign locale prefix (/en/about stays as /fr/en/about)', () => {
22
- expect(opts('/en/about', 'fr')).toBe('/fr/en/about');
23
- });
24
- it('en prefix still works', () => {
25
- expect(opts('/about', 'en')).toBe('/en/about');
26
- });
27
- it('default locale de returns path without prefix', () => {
28
- expect(opts('/about', 'de')).toBe('/about');
29
- });
30
- it('preserves query string for fr', () => {
31
- expect(opts('/about?ref=home', 'fr')).toBe('/fr/about?ref=home');
32
- });
33
- it('preserves hash for fr', () => {
34
- expect(opts('/about#section', 'fr')).toBe('/fr/about#section');
35
- });
36
- });
37
-
38
- describe('getLocaleFromPath – three locales', () => {
39
- const locales = ['de', 'en', 'fr'] as const;
40
- const opts = (pathname: string) => getLocaleFromPath({ pathname, locales, defaultLocale: 'de' });
41
-
42
- it('detects fr from /fr/contact', () => {
43
- expect(opts('/fr/contact')).toBe('fr');
44
- });
45
- it('detects fr from /fr (no trailing segment)', () => {
46
- expect(opts('/fr')).toBe('fr');
47
- });
48
- it('detects en from /en/about', () => {
49
- expect(opts('/en/about')).toBe('en');
50
- });
51
- it('returns default for root', () => {
52
- expect(opts('/')).toBe('de');
53
- });
54
- it('returns default for /blog (not a locale prefix)', () => {
55
- expect(opts('/blog')).toBe('de');
56
- });
57
- it('does not match partial segment /french', () => {
58
- expect(opts('/french-cuisine')).toBe('de');
59
- });
60
- });
61
-
62
- describe('createI18n – three-locale factory', () => {
63
- const i18n = createI18n({
64
- locales: ['de', 'en', 'fr'] as const,
65
- defaultLocale: 'de',
66
- baseUrl: 'https://example.com',
67
- });
68
-
69
- it('exposes all three locales', () => {
70
- expect(i18n.locales).toEqual(['de', 'en', 'fr']);
71
- });
72
- it('getAlternateLinks returns 4 entries (3 locales + x-default)', () => {
73
- expect(i18n.getAlternateLinks('/about')).toHaveLength(4);
74
- });
75
- it('getAlternateLinks includes fr hreflang', () => {
76
- const links = i18n.getAlternateLinks('/about');
77
- const fr = links.find(l => l.hreflang === 'fr');
78
- expect(fr?.href).toBe('https://example.com/fr/about/');
79
- });
80
- it('localizedHref for fr', () => {
81
- expect(i18n.localizedHref('/about', 'fr')).toBe('/fr/about');
82
- });
83
- it('getLocaleFromPath detects fr', () => {
84
- expect(i18n.getLocaleFromPath('/fr/contact')).toBe('fr');
85
- });
86
- it('getCanonicalUrl for fr locale', () => {
87
- expect(i18n.getCanonicalUrl('/contact', 'fr')).toBe('https://example.com/fr/contact/');
88
- });
89
- });
1
+ import { describe, expect, it } from 'vitest';
2
+ import { localizedHref, getLocaleFromPath } from '../routing.js';
3
+ import { createI18n } from '../factory.js';
4
+
5
+ describe('localizedHref – three-locale setup (de/en/fr)', () => {
6
+ const opts = (path: string, locale: string) =>
7
+ localizedHref({ path, locale, defaultLocale: 'de' });
8
+
9
+ it('fr prefix on root path', () => {
10
+ expect(opts('/', 'fr')).toBe('/fr');
11
+ });
12
+ it('fr prefix on /about', () => {
13
+ expect(opts('/about', 'fr')).toBe('/fr/about');
14
+ });
15
+ it('fr prefix on deep path', () => {
16
+ expect(opts('/news/2024/article', 'fr')).toBe('/fr/news/2024/article');
17
+ });
18
+ it('strips /fr prefix for fr locale (idempotent)', () => {
19
+ expect(opts('/fr/about', 'fr')).toBe('/fr/about');
20
+ });
21
+ it('does not strip a foreign locale prefix (/en/about stays as /fr/en/about)', () => {
22
+ expect(opts('/en/about', 'fr')).toBe('/fr/en/about');
23
+ });
24
+ it('en prefix still works', () => {
25
+ expect(opts('/about', 'en')).toBe('/en/about');
26
+ });
27
+ it('default locale de returns path without prefix', () => {
28
+ expect(opts('/about', 'de')).toBe('/about');
29
+ });
30
+ it('preserves query string for fr', () => {
31
+ expect(opts('/about?ref=home', 'fr')).toBe('/fr/about?ref=home');
32
+ });
33
+ it('preserves hash for fr', () => {
34
+ expect(opts('/about#section', 'fr')).toBe('/fr/about#section');
35
+ });
36
+ });
37
+
38
+ describe('getLocaleFromPath – three locales', () => {
39
+ const locales = ['de', 'en', 'fr'] as const;
40
+ const opts = (pathname: string) => getLocaleFromPath({ pathname, locales, defaultLocale: 'de' });
41
+
42
+ it('detects fr from /fr/contact', () => {
43
+ expect(opts('/fr/contact')).toBe('fr');
44
+ });
45
+ it('detects fr from /fr (no trailing segment)', () => {
46
+ expect(opts('/fr')).toBe('fr');
47
+ });
48
+ it('detects en from /en/about', () => {
49
+ expect(opts('/en/about')).toBe('en');
50
+ });
51
+ it('returns default for root', () => {
52
+ expect(opts('/')).toBe('de');
53
+ });
54
+ it('returns default for /blog (not a locale prefix)', () => {
55
+ expect(opts('/blog')).toBe('de');
56
+ });
57
+ it('does not match partial segment /french', () => {
58
+ expect(opts('/french-cuisine')).toBe('de');
59
+ });
60
+ });
61
+
62
+ describe('createI18n – three-locale factory', () => {
63
+ const i18n = createI18n({
64
+ locales: ['de', 'en', 'fr'] as const,
65
+ defaultLocale: 'de',
66
+ baseUrl: 'https://example.com',
67
+ });
68
+
69
+ it('exposes all three locales', () => {
70
+ expect(i18n.locales).toEqual(['de', 'en', 'fr']);
71
+ });
72
+ it('getAlternateLinks returns 4 entries (3 locales + x-default)', () => {
73
+ expect(i18n.getAlternateLinks('/about')).toHaveLength(4);
74
+ });
75
+ it('getAlternateLinks includes fr hreflang', () => {
76
+ const links = i18n.getAlternateLinks('/about');
77
+ const fr = links.find(l => l.hreflang === 'fr');
78
+ expect(fr?.href).toBe('https://example.com/fr/about/');
79
+ });
80
+ it('localizedHref for fr', () => {
81
+ expect(i18n.localizedHref('/about', 'fr')).toBe('/fr/about');
82
+ });
83
+ it('getLocaleFromPath detects fr', () => {
84
+ expect(i18n.getLocaleFromPath('/fr/contact')).toBe('fr');
85
+ });
86
+ it('getCanonicalUrl for fr locale', () => {
87
+ expect(i18n.getCanonicalUrl('/contact', 'fr')).toBe('https://example.com/fr/contact/');
88
+ });
89
+ });
@@ -1,140 +1,140 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { getAlternateLinks, getCanonicalUrl } from '../seo.js';
3
-
4
- const BASE = 'https://dev.ismaili.de';
5
-
6
- describe('getAlternateLinks', () => {
7
- it('returns locales.length + 1 entries', () => {
8
- const links = getAlternateLinks({ path: '/about', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
9
- expect(links).toHaveLength(3); // de + en + x-default
10
- });
11
- it('default locale omits prefix', () => {
12
- const links = getAlternateLinks({ path: '/about', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
13
- const de = links.find(l => l.hreflang === 'de');
14
- expect(de?.href).toBe(`${BASE}/about/`);
15
- });
16
- it('non-default locale adds prefix', () => {
17
- const links = getAlternateLinks({ path: '/about', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
18
- const en = links.find(l => l.hreflang === 'en');
19
- expect(en?.href).toBe(`${BASE}/en/about/`);
20
- });
21
- it('x-default points to default locale URL', () => {
22
- const links = getAlternateLinks({ path: '/about', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
23
- const xd = links.find(l => l.hreflang === 'x-default');
24
- expect(xd?.href).toBe(`${BASE}/about/`);
25
- });
26
- it('handles root path for default locale', () => {
27
- const links = getAlternateLinks({ path: '/', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
28
- const de = links.find(l => l.hreflang === 'de');
29
- expect(de?.href).toBe(`${BASE}/`);
30
- });
31
- it('handles trailing slash on baseUrl', () => {
32
- const links = getAlternateLinks({ path: '/about', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: `${BASE}/` });
33
- const de = links.find(l => l.hreflang === 'de');
34
- expect(de?.href).toBe(`${BASE}/about/`);
35
- });
36
- it('works with 3 locales', () => {
37
- const links = getAlternateLinks({ path: '/contact', locales: ['de', 'en', 'fr'], defaultLocale: 'de', baseUrl: 'https://example.com' });
38
- expect(links).toHaveLength(4);
39
- const fr = links.find(l => l.hreflang === 'fr');
40
- expect(fr?.href).toBe('https://example.com/fr/contact/');
41
- });
42
- });
43
-
44
- describe('getCanonicalUrl', () => {
45
- it('default locale returns path without prefix', () => {
46
- expect(getCanonicalUrl({ path: '/about', locale: 'de', defaultLocale: 'de', baseUrl: BASE })).toBe(`${BASE}/about/`);
47
- });
48
- it('non-default locale prepends prefix', () => {
49
- expect(getCanonicalUrl({ path: '/about', locale: 'en', defaultLocale: 'de', baseUrl: BASE })).toBe(`${BASE}/en/about/`);
50
- });
51
- it('handles root path', () => {
52
- expect(getCanonicalUrl({ path: '/', locale: 'de', defaultLocale: 'de', baseUrl: BASE })).toBe(`${BASE}/`);
53
- });
54
- });
55
-
56
- describe('SEO helpers with already-localized input', () => {
57
- it('getAlternateLinks: no href contains /en/en for path /en', () => {
58
- const links = getAlternateLinks({ path: '/en', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
59
- expect(links.every(l => !l.href.includes('/en/en'))).toBe(true);
60
- });
61
-
62
- it('getAlternateLinks: en hreflang href is /en/ (not /en/en)', () => {
63
- const links = getAlternateLinks({ path: '/en', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
64
- const en = links.find(l => l.hreflang === 'en');
65
- expect(en?.href).toBe(`${BASE}/en/`);
66
- });
67
-
68
- it('getAlternateLinks: root path / produces /en/ for en hreflang', () => {
69
- const links = getAlternateLinks({ path: '/', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
70
- const en = links.find(l => l.hreflang === 'en');
71
- expect(en?.href).toBe(`${BASE}/en/`);
72
- });
73
-
74
- it('getCanonicalUrl: already-localized /en resolves to /en/', () => {
75
- expect(getCanonicalUrl({ path: '/en', locale: 'en', defaultLocale: 'de', baseUrl: BASE })).toBe(`${BASE}/en/`);
76
- });
77
-
78
- it('getCanonicalUrl: already-localized /en/about resolves to /en/about/', () => {
79
- expect(getCanonicalUrl({ path: '/en/about', locale: 'en', defaultLocale: 'de', baseUrl: BASE })).toBe(`${BASE}/en/about/`);
80
- });
81
-
82
- it('getCanonicalUrl: /de/about with default locale resolves to /about/', () => {
83
- expect(getCanonicalUrl({ path: '/de/about', locale: 'de', defaultLocale: 'de', baseUrl: BASE })).toBe(`${BASE}/about/`);
84
- });
85
- });
86
-
87
- describe('served URL form is independent of how the caller spells the path', () => {
88
- const spellings = ['/about', '/about/'];
89
-
90
- it('getCanonicalUrl yields one URL for both spellings', () => {
91
- const results = spellings.map((path) =>
92
- getCanonicalUrl({ path, locale: 'de', defaultLocale: 'de', baseUrl: BASE }),
93
- );
94
-
95
- expect(new Set(results).size).toBe(1);
96
- expect(results[0]).toBe(`${BASE}/about/`);
97
- });
98
-
99
- it('getAlternateLinks yields one link set for both spellings', () => {
100
- const results = spellings.map((path) =>
101
- JSON.stringify(getAlternateLinks({ path, locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE })),
102
- );
103
-
104
- expect(new Set(results).size).toBe(1);
105
- });
106
-
107
- it('canonical and the self-referencing alternate are byte-identical', () => {
108
- for (const path of spellings) {
109
- const canonical = getCanonicalUrl({ path, locale: 'de', defaultLocale: 'de', baseUrl: BASE });
110
- const self = getAlternateLinks({ path, locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE })
111
- .find((l) => l.hreflang === 'de');
112
-
113
- expect(self?.href).toBe(canonical);
114
- }
115
- });
116
- });
117
-
118
- describe('trailingSlash: never (Astro build.format "file")', () => {
119
- it('omits the trailing slash from canonical and alternates', () => {
120
- expect(
121
- getCanonicalUrl({ path: '/about/', locale: 'de', defaultLocale: 'de', baseUrl: BASE, trailingSlash: 'never' }),
122
- ).toBe(`${BASE}/about`);
123
-
124
- const links = getAlternateLinks({
125
- path: '/about/',
126
- locales: ['de', 'en'],
127
- defaultLocale: 'de',
128
- baseUrl: BASE,
129
- trailingSlash: 'never',
130
- });
131
-
132
- expect(links.find((l) => l.hreflang === 'en')?.href).toBe(`${BASE}/en/about`);
133
- });
134
-
135
- it('still emits the root as /', () => {
136
- expect(
137
- getCanonicalUrl({ path: '/', locale: 'de', defaultLocale: 'de', baseUrl: BASE, trailingSlash: 'never' }),
138
- ).toBe(`${BASE}/`);
139
- });
140
- });
1
+ import { describe, expect, it } from 'vitest';
2
+ import { getAlternateLinks, getCanonicalUrl } from '../seo.js';
3
+
4
+ const BASE = 'https://dev.ismaili.de';
5
+
6
+ describe('getAlternateLinks', () => {
7
+ it('returns locales.length + 1 entries', () => {
8
+ const links = getAlternateLinks({ path: '/about', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
9
+ expect(links).toHaveLength(3); // de + en + x-default
10
+ });
11
+ it('default locale omits prefix', () => {
12
+ const links = getAlternateLinks({ path: '/about', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
13
+ const de = links.find(l => l.hreflang === 'de');
14
+ expect(de?.href).toBe(`${BASE}/about/`);
15
+ });
16
+ it('non-default locale adds prefix', () => {
17
+ const links = getAlternateLinks({ path: '/about', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
18
+ const en = links.find(l => l.hreflang === 'en');
19
+ expect(en?.href).toBe(`${BASE}/en/about/`);
20
+ });
21
+ it('x-default points to default locale URL', () => {
22
+ const links = getAlternateLinks({ path: '/about', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
23
+ const xd = links.find(l => l.hreflang === 'x-default');
24
+ expect(xd?.href).toBe(`${BASE}/about/`);
25
+ });
26
+ it('handles root path for default locale', () => {
27
+ const links = getAlternateLinks({ path: '/', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
28
+ const de = links.find(l => l.hreflang === 'de');
29
+ expect(de?.href).toBe(`${BASE}/`);
30
+ });
31
+ it('handles trailing slash on baseUrl', () => {
32
+ const links = getAlternateLinks({ path: '/about', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: `${BASE}/` });
33
+ const de = links.find(l => l.hreflang === 'de');
34
+ expect(de?.href).toBe(`${BASE}/about/`);
35
+ });
36
+ it('works with 3 locales', () => {
37
+ const links = getAlternateLinks({ path: '/contact', locales: ['de', 'en', 'fr'], defaultLocale: 'de', baseUrl: 'https://example.com' });
38
+ expect(links).toHaveLength(4);
39
+ const fr = links.find(l => l.hreflang === 'fr');
40
+ expect(fr?.href).toBe('https://example.com/fr/contact/');
41
+ });
42
+ });
43
+
44
+ describe('getCanonicalUrl', () => {
45
+ it('default locale returns path without prefix', () => {
46
+ expect(getCanonicalUrl({ path: '/about', locale: 'de', defaultLocale: 'de', baseUrl: BASE })).toBe(`${BASE}/about/`);
47
+ });
48
+ it('non-default locale prepends prefix', () => {
49
+ expect(getCanonicalUrl({ path: '/about', locale: 'en', defaultLocale: 'de', baseUrl: BASE })).toBe(`${BASE}/en/about/`);
50
+ });
51
+ it('handles root path', () => {
52
+ expect(getCanonicalUrl({ path: '/', locale: 'de', defaultLocale: 'de', baseUrl: BASE })).toBe(`${BASE}/`);
53
+ });
54
+ });
55
+
56
+ describe('SEO helpers with already-localized input', () => {
57
+ it('getAlternateLinks: no href contains /en/en for path /en', () => {
58
+ const links = getAlternateLinks({ path: '/en', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
59
+ expect(links.every(l => !l.href.includes('/en/en'))).toBe(true);
60
+ });
61
+
62
+ it('getAlternateLinks: en hreflang href is /en/ (not /en/en)', () => {
63
+ const links = getAlternateLinks({ path: '/en', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
64
+ const en = links.find(l => l.hreflang === 'en');
65
+ expect(en?.href).toBe(`${BASE}/en/`);
66
+ });
67
+
68
+ it('getAlternateLinks: root path / produces /en/ for en hreflang', () => {
69
+ const links = getAlternateLinks({ path: '/', locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE });
70
+ const en = links.find(l => l.hreflang === 'en');
71
+ expect(en?.href).toBe(`${BASE}/en/`);
72
+ });
73
+
74
+ it('getCanonicalUrl: already-localized /en resolves to /en/', () => {
75
+ expect(getCanonicalUrl({ path: '/en', locale: 'en', defaultLocale: 'de', baseUrl: BASE })).toBe(`${BASE}/en/`);
76
+ });
77
+
78
+ it('getCanonicalUrl: already-localized /en/about resolves to /en/about/', () => {
79
+ expect(getCanonicalUrl({ path: '/en/about', locale: 'en', defaultLocale: 'de', baseUrl: BASE })).toBe(`${BASE}/en/about/`);
80
+ });
81
+
82
+ it('getCanonicalUrl: /de/about with default locale resolves to /about/', () => {
83
+ expect(getCanonicalUrl({ path: '/de/about', locale: 'de', defaultLocale: 'de', baseUrl: BASE })).toBe(`${BASE}/about/`);
84
+ });
85
+ });
86
+
87
+ describe('served URL form is independent of how the caller spells the path', () => {
88
+ const spellings = ['/about', '/about/'];
89
+
90
+ it('getCanonicalUrl yields one URL for both spellings', () => {
91
+ const results = spellings.map((path) =>
92
+ getCanonicalUrl({ path, locale: 'de', defaultLocale: 'de', baseUrl: BASE }),
93
+ );
94
+
95
+ expect(new Set(results).size).toBe(1);
96
+ expect(results[0]).toBe(`${BASE}/about/`);
97
+ });
98
+
99
+ it('getAlternateLinks yields one link set for both spellings', () => {
100
+ const results = spellings.map((path) =>
101
+ JSON.stringify(getAlternateLinks({ path, locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE })),
102
+ );
103
+
104
+ expect(new Set(results).size).toBe(1);
105
+ });
106
+
107
+ it('canonical and the self-referencing alternate are byte-identical', () => {
108
+ for (const path of spellings) {
109
+ const canonical = getCanonicalUrl({ path, locale: 'de', defaultLocale: 'de', baseUrl: BASE });
110
+ const self = getAlternateLinks({ path, locales: ['de', 'en'], defaultLocale: 'de', baseUrl: BASE })
111
+ .find((l) => l.hreflang === 'de');
112
+
113
+ expect(self?.href).toBe(canonical);
114
+ }
115
+ });
116
+ });
117
+
118
+ describe('trailingSlash: never (Astro build.format "file")', () => {
119
+ it('omits the trailing slash from canonical and alternates', () => {
120
+ expect(
121
+ getCanonicalUrl({ path: '/about/', locale: 'de', defaultLocale: 'de', baseUrl: BASE, trailingSlash: 'never' }),
122
+ ).toBe(`${BASE}/about`);
123
+
124
+ const links = getAlternateLinks({
125
+ path: '/about/',
126
+ locales: ['de', 'en'],
127
+ defaultLocale: 'de',
128
+ baseUrl: BASE,
129
+ trailingSlash: 'never',
130
+ });
131
+
132
+ expect(links.find((l) => l.hreflang === 'en')?.href).toBe(`${BASE}/en/about`);
133
+ });
134
+
135
+ it('still emits the root as /', () => {
136
+ expect(
137
+ getCanonicalUrl({ path: '/', locale: 'de', defaultLocale: 'de', baseUrl: BASE, trailingSlash: 'never' }),
138
+ ).toBe(`${BASE}/`);
139
+ });
140
+ });
@@ -1,66 +1,66 @@
1
- import { formatCurrency, formatDate, formatList, formatNumber, formatRelativeTime } from './formatters.js'
2
- import { ambientTrailingSlash } from './localized-paths.js'
3
- import type { LocalizedPathGroup, TrailingSlash } from './localized-paths.js'
4
- import { getLocaleFromPath, localizedHref } from './routing.js'
5
- import { getAlternateLinks, getCanonicalUrl } from './seo.js'
6
-
7
- export interface I18nConfig<L extends string> {
8
- locales: readonly L[]
9
- defaultLocale: L
10
- baseUrl: string
11
- localizedPaths?: readonly LocalizedPathGroup[]
12
- /** Defaults to the form `@easy-web/seo` resolved from `astro.config.mjs`. */
13
- trailingSlash?: TrailingSlash
14
- }
15
-
16
- export function createI18n<L extends string>(cfg: I18nConfig<L>) {
17
- const trailingSlash = cfg.trailingSlash ?? ambientTrailingSlash()
18
-
19
- return {
20
- locales: cfg.locales,
21
- defaultLocale: cfg.defaultLocale,
22
- localizedPaths: cfg.localizedPaths ?? [],
23
- trailingSlash,
24
- localizedHref: (path: string, locale: L): string =>
25
- localizedHref({ path, locale, defaultLocale: cfg.defaultLocale }),
26
- getLocaleFromPath: (pathname: string): L =>
27
- getLocaleFromPath({ pathname, locales: cfg.locales, defaultLocale: cfg.defaultLocale }) as L,
28
- getAlternateLinks: (path: string) =>
29
- getAlternateLinks({
30
- path,
31
- locales: cfg.locales,
32
- defaultLocale: cfg.defaultLocale,
33
- baseUrl: cfg.baseUrl,
34
- localizedPaths: cfg.localizedPaths,
35
- trailingSlash,
36
- }),
37
- getCanonicalUrl: (path: string, locale: L): string =>
38
- getCanonicalUrl({
39
- path,
40
- locale,
41
- defaultLocale: cfg.defaultLocale,
42
- baseUrl: cfg.baseUrl,
43
- trailingSlash,
44
- }),
45
- format: {
46
- date: (locale: L, date: Date | number, opts?: Intl.DateTimeFormatOptions): string =>
47
- formatDate(locale, date, opts),
48
- number: (locale: L, value: number, opts?: Intl.NumberFormatOptions): string =>
49
- formatNumber(locale, value, opts),
50
- relativeTime: (
51
- locale: L,
52
- value: number,
53
- unit: Intl.RelativeTimeFormatUnit,
54
- opts?: Intl.RelativeTimeFormatOptions,
55
- ): string => formatRelativeTime(locale, value, unit, opts),
56
- list: (locale: L, items: string[], opts?: Intl.ListFormatOptions): string =>
57
- formatList(locale, items, opts),
58
- currency: (
59
- locale: L,
60
- value: number,
61
- currency: string,
62
- opts?: Omit<Intl.NumberFormatOptions, 'style' | 'currency'>,
63
- ): string => formatCurrency(locale, value, currency, opts),
64
- },
65
- }
66
- }
1
+ import { formatCurrency, formatDate, formatList, formatNumber, formatRelativeTime } from './formatters.js'
2
+ import { ambientTrailingSlash } from './localized-paths.js'
3
+ import type { LocalizedPathGroup, TrailingSlash } from './localized-paths.js'
4
+ import { getLocaleFromPath, localizedHref } from './routing.js'
5
+ import { getAlternateLinks, getCanonicalUrl } from './seo.js'
6
+
7
+ export interface I18nConfig<L extends string> {
8
+ locales: readonly L[]
9
+ defaultLocale: L
10
+ baseUrl: string
11
+ localizedPaths?: readonly LocalizedPathGroup[]
12
+ /** Defaults to the form `@easy-web/seo` resolved from `astro.config.mjs`. */
13
+ trailingSlash?: TrailingSlash
14
+ }
15
+
16
+ export function createI18n<L extends string>(cfg: I18nConfig<L>) {
17
+ const trailingSlash = cfg.trailingSlash ?? ambientTrailingSlash()
18
+
19
+ return {
20
+ locales: cfg.locales,
21
+ defaultLocale: cfg.defaultLocale,
22
+ localizedPaths: cfg.localizedPaths ?? [],
23
+ trailingSlash,
24
+ localizedHref: (path: string, locale: L): string =>
25
+ localizedHref({ path, locale, defaultLocale: cfg.defaultLocale }),
26
+ getLocaleFromPath: (pathname: string): L =>
27
+ getLocaleFromPath({ pathname, locales: cfg.locales, defaultLocale: cfg.defaultLocale }) as L,
28
+ getAlternateLinks: (path: string) =>
29
+ getAlternateLinks({
30
+ path,
31
+ locales: cfg.locales,
32
+ defaultLocale: cfg.defaultLocale,
33
+ baseUrl: cfg.baseUrl,
34
+ localizedPaths: cfg.localizedPaths,
35
+ trailingSlash,
36
+ }),
37
+ getCanonicalUrl: (path: string, locale: L): string =>
38
+ getCanonicalUrl({
39
+ path,
40
+ locale,
41
+ defaultLocale: cfg.defaultLocale,
42
+ baseUrl: cfg.baseUrl,
43
+ trailingSlash,
44
+ }),
45
+ format: {
46
+ date: (locale: L, date: Date | number, opts?: Intl.DateTimeFormatOptions): string =>
47
+ formatDate(locale, date, opts),
48
+ number: (locale: L, value: number, opts?: Intl.NumberFormatOptions): string =>
49
+ formatNumber(locale, value, opts),
50
+ relativeTime: (
51
+ locale: L,
52
+ value: number,
53
+ unit: Intl.RelativeTimeFormatUnit,
54
+ opts?: Intl.RelativeTimeFormatOptions,
55
+ ): string => formatRelativeTime(locale, value, unit, opts),
56
+ list: (locale: L, items: string[], opts?: Intl.ListFormatOptions): string =>
57
+ formatList(locale, items, opts),
58
+ currency: (
59
+ locale: L,
60
+ value: number,
61
+ currency: string,
62
+ opts?: Omit<Intl.NumberFormatOptions, 'style' | 'currency'>,
63
+ ): string => formatCurrency(locale, value, currency, opts),
64
+ },
65
+ }
66
+ }
package/src/i18n/index.ts CHANGED
@@ -1,5 +1,5 @@
1
- export { createI18n, type I18nConfig } from './factory.js'
2
- export * from './formatters.js'
3
- export * from './localized-paths.js'
4
- export * from './routing.js'
5
- export * from './seo.js'
1
+ export { createI18n, type I18nConfig } from './factory.js'
2
+ export * from './formatters.js'
3
+ export * from './localized-paths.js'
4
+ export * from './routing.js'
5
+ export * from './seo.js'