@easy-web/core 1.3.3 → 1.5.0
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/LICENSE +21 -0
- package/package.json +9 -9
- package/src/components/Header.astro +205 -205
- package/src/components/HeaderCentered.astro +230 -230
- package/src/components/HeaderFlyout.astro +370 -370
- package/src/components/HeaderHideOnScroll.astro +234 -234
- package/src/components/LanguageSwitch.astro +82 -82
- package/src/components/NotFound.astro +115 -115
- package/src/components/PageShell.astro +82 -82
- package/src/components/SeoHead.astro +106 -106
- package/src/components/StructuredData.astro +1 -1
- package/src/components/ThemeToggle.astro +84 -84
- package/src/components/UniversalMedia.astro +106 -106
- package/src/components/__tests__/StructuredData.test.ts +1 -1
- package/src/components/__tests__/UniversalMedia.test.ts +65 -65
- package/src/contracts/__tests__/jsonld.test.ts +1 -1
- package/src/contracts/index.ts +1 -1
- package/src/contracts/jsonld.ts +1 -1
- package/src/i18n/__tests__/factory.test.ts +54 -54
- package/src/i18n/__tests__/localized-paths.test.ts +258 -258
- package/src/i18n/__tests__/routing-multi-locale.test.ts +89 -89
- package/src/i18n/__tests__/seo.test.ts +140 -140
- package/src/i18n/factory.ts +66 -66
- package/src/i18n/index.ts +5 -5
- package/src/i18n/localized-paths.ts +181 -181
- package/src/i18n/seo.ts +131 -131
- package/src/markdown/__tests__/__fixtures__/abs-src-assets.md +1 -1
- package/src/markdown/__tests__/__fixtures__/external-url.md +1 -1
- package/src/markdown/__tests__/__fixtures__/public-slash.md +1 -1
- package/src/markdown/__tests__/__fixtures__/rel-double-dot.md +1 -1
- package/src/markdown/__tests__/index.test.ts +60 -60
- package/src/markdown/index.ts +1 -1
- package/src/markdown/plugin.ts +42 -42
- package/src/schemas/__tests__/notFound.test.ts +32 -32
- package/src/schemas/notFound.ts +33 -33
- package/src/seo/__tests__/crawl-policy.test.ts +50 -50
- package/src/seo/__tests__/integration.test.ts +195 -195
- package/src/seo/__tests__/no-index-resolution.test.ts +53 -53
- package/src/seo/__tests__/robots-txt.test.ts +66 -66
- package/src/seo/__tests__/sitemap-options.test.ts +400 -400
- package/src/seo/crawl-policy.ts +36 -36
- package/src/seo/index.ts +254 -254
- package/src/seo/routes/robots-txt.ts +36 -36
|
@@ -1,181 +1,181 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared model for routes whose slug differs per locale.
|
|
3
|
-
*
|
|
4
|
-
* Both hreflang surfaces consume this one model:
|
|
5
|
-
*
|
|
6
|
-
* - `@easy-web/i18n` `getAlternateLinks()` renders the in-page
|
|
7
|
-
* `<link rel="alternate" hreflang>` tags.
|
|
8
|
-
* - `@easy-web/seo` renders the `xhtml:link` alternates in `sitemap-0.xml`.
|
|
9
|
-
*
|
|
10
|
-
* Keeping the model here is deliberate. Both surfaces previously derived
|
|
11
|
-
* alternates independently by stripping the locale prefix and re-prefixing the
|
|
12
|
-
* remaining path, which silently assumes the slug is identical in every locale.
|
|
13
|
-
* For a pair like DE `/datenschutz/` and EN `/en/privacy/` that assumption is
|
|
14
|
-
* wrong, and the two surfaces failed in different ways: the sitemap omitted the
|
|
15
|
-
* alternates entirely, while the page head emitted `/en/datenschutz/` — a URL
|
|
16
|
-
* that does not exist. One shared model means the two can no longer disagree.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* One translated route, keyed by Astro locale id.
|
|
21
|
-
*
|
|
22
|
-
* Values are complete public route paths including any locale prefix, written
|
|
23
|
-
* exactly as the route is served:
|
|
24
|
-
*
|
|
25
|
-
* ```ts
|
|
26
|
-
* { de: '/datenschutz/', en: '/en/privacy/' }
|
|
27
|
-
* ```
|
|
28
|
-
*
|
|
29
|
-
* Trailing slashes are insignificant for matching — consumers render the URL
|
|
30
|
-
* form appropriate to their surface.
|
|
31
|
-
*/
|
|
32
|
-
export type LocalizedPathGroup = Readonly<Record<string, string>>
|
|
33
|
-
|
|
34
|
-
export function stripQueryAndHash(path: string): string {
|
|
35
|
-
const withoutHash = path.split('#')[0] ?? ''
|
|
36
|
-
return withoutHash.split('?')[0] ?? ''
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Which URL form the site actually serves, mirroring Astro's `build.format`:
|
|
41
|
-
* `always` for `directory` output (`/about/`), `never` for `file` (`/about`).
|
|
42
|
-
*/
|
|
43
|
-
export type TrailingSlash = 'always' | 'never'
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* The URL form `@easy-web/seo` resolved from `astro.config.mjs`, for callers
|
|
47
|
-
* that run outside an Astro integration and so cannot read the config.
|
|
48
|
-
*
|
|
49
|
-
* Components and instance modules render links with this so internal hrefs
|
|
50
|
-
* point at the canonical URL instead of the form the host will redirect away.
|
|
51
|
-
*/
|
|
52
|
-
export function ambientTrailingSlash(): TrailingSlash {
|
|
53
|
-
const runtime = globalThis as {
|
|
54
|
-
process?: { env?: Record<string, string | undefined> }
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return runtime.process?.env?.['EASY_WEB_SEO_TRAILING_SLASH'] === 'never'
|
|
58
|
-
? 'never'
|
|
59
|
-
: 'always'
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Renders a path in the form the site actually serves.
|
|
64
|
-
*
|
|
65
|
-
* Canonical, hreflang alternates and the sitemap must all agree byte-for-byte,
|
|
66
|
-
* or Google treats `/about` and `/about/` as competing duplicates. Instances
|
|
67
|
-
* hand-write `pathname` props in mixed conventions, so every SEO URL is routed
|
|
68
|
-
* through this function rather than trusting the caller's spelling.
|
|
69
|
-
*/
|
|
70
|
-
export function toServedPath(path: string, style: TrailingSlash = 'always'): string {
|
|
71
|
-
const normalized = normalizeLocalizedPath(path)
|
|
72
|
-
|
|
73
|
-
if (normalized === '/' || style === 'never') {
|
|
74
|
-
return normalized
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return `${normalized}/`
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Reduces a path to the form used for comparison and lookup: leading slash,
|
|
82
|
-
* no trailing slash (except root), no query string, no hash.
|
|
83
|
-
*
|
|
84
|
-
* This is what makes `/datenschutz`, `/datenschutz/` and `/datenschutz/?x=1`
|
|
85
|
-
* resolve to the same group. Astro hands `Astro.url.pathname` to the layout
|
|
86
|
-
* without a trailing slash while `@astrojs/sitemap` emits one, so lookup has to
|
|
87
|
-
* be insensitive to that difference.
|
|
88
|
-
*/
|
|
89
|
-
export function normalizeLocalizedPath(path: string): string {
|
|
90
|
-
const withoutQuery = stripQueryAndHash(path)
|
|
91
|
-
const withLeadingSlash = withoutQuery.startsWith('/') ? withoutQuery : `/${withoutQuery}`
|
|
92
|
-
const trimmed = withLeadingSlash.replace(/\/+$/, '')
|
|
93
|
-
|
|
94
|
-
return trimmed === '' ? '/' : trimmed
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Finds the group that claims `path`, or `undefined` when the route uses the
|
|
99
|
-
* same slug in every locale (the common case, handled by prefix derivation).
|
|
100
|
-
*/
|
|
101
|
-
export function findLocalizedGroup(
|
|
102
|
-
path: string,
|
|
103
|
-
groups: readonly LocalizedPathGroup[] | undefined,
|
|
104
|
-
): LocalizedPathGroup | undefined {
|
|
105
|
-
if (!groups || groups.length === 0) {
|
|
106
|
-
return undefined
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const target = normalizeLocalizedPath(path)
|
|
110
|
-
|
|
111
|
-
return groups.find((group) =>
|
|
112
|
-
Object.values(group).some((candidate) => normalizeLocalizedPath(candidate) === target),
|
|
113
|
-
)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Validates declared groups against the configured locales.
|
|
118
|
-
*
|
|
119
|
-
* Returns a list of human-readable problems; an empty array means valid.
|
|
120
|
-
* Callers decide whether to throw or warn — `@easy-web/seo` fails the build,
|
|
121
|
-
* because a stale mapping produces output that is syntactically valid but
|
|
122
|
-
* silently wrong, which is precisely the failure mode this model exists to
|
|
123
|
-
* eliminate.
|
|
124
|
-
*/
|
|
125
|
-
export function validateLocalizedPaths(
|
|
126
|
-
groups: readonly LocalizedPathGroup[],
|
|
127
|
-
locales: readonly string[],
|
|
128
|
-
): string[] {
|
|
129
|
-
const problems: string[] = []
|
|
130
|
-
const claimedBy = new Map<string, number>()
|
|
131
|
-
|
|
132
|
-
groups.forEach((group, index) => {
|
|
133
|
-
const label = `localizedPaths[${index}]`
|
|
134
|
-
const declared = Object.keys(group)
|
|
135
|
-
|
|
136
|
-
const missing = locales.filter((locale) => !declared.includes(locale))
|
|
137
|
-
if (missing.length > 0) {
|
|
138
|
-
problems.push(`${label} is missing the locale(s): ${missing.join(', ')}.`)
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const unknown = declared.filter((locale) => !locales.includes(locale))
|
|
142
|
-
if (unknown.length > 0) {
|
|
143
|
-
problems.push(`${label} declares unknown locale(s): ${unknown.join(', ')}.`)
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const seenInGroup = new Set<string>()
|
|
147
|
-
|
|
148
|
-
for (const locale of declared) {
|
|
149
|
-
const value = group[locale] as string
|
|
150
|
-
|
|
151
|
-
if (typeof value !== 'string' || !value.startsWith('/') || value.startsWith('//')) {
|
|
152
|
-
problems.push(
|
|
153
|
-
`${label}.${locale} must be an absolute path beginning with a single "/", got: ${String(value)}.`,
|
|
154
|
-
)
|
|
155
|
-
continue
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
if (value.includes('?') || value.includes('#')) {
|
|
159
|
-
problems.push(`${label}.${locale} must not contain a query string or hash, got: ${value}.`)
|
|
160
|
-
continue
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const normalized = normalizeLocalizedPath(value)
|
|
164
|
-
|
|
165
|
-
if (seenInGroup.has(normalized)) {
|
|
166
|
-
problems.push(`${label} maps more than one locale to the same path: ${normalized}.`)
|
|
167
|
-
}
|
|
168
|
-
seenInGroup.add(normalized)
|
|
169
|
-
|
|
170
|
-
const owner = claimedBy.get(normalized)
|
|
171
|
-
if (owner !== undefined && owner !== index) {
|
|
172
|
-
problems.push(
|
|
173
|
-
`${normalized} is claimed by both localizedPaths[${owner}] and ${label}; each path may belong to only one group.`,
|
|
174
|
-
)
|
|
175
|
-
}
|
|
176
|
-
claimedBy.set(normalized, index)
|
|
177
|
-
}
|
|
178
|
-
})
|
|
179
|
-
|
|
180
|
-
return problems
|
|
181
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Shared model for routes whose slug differs per locale.
|
|
3
|
+
*
|
|
4
|
+
* Both hreflang surfaces consume this one model:
|
|
5
|
+
*
|
|
6
|
+
* - `@easy-web/i18n` `getAlternateLinks()` renders the in-page
|
|
7
|
+
* `<link rel="alternate" hreflang>` tags.
|
|
8
|
+
* - `@easy-web/seo` renders the `xhtml:link` alternates in `sitemap-0.xml`.
|
|
9
|
+
*
|
|
10
|
+
* Keeping the model here is deliberate. Both surfaces previously derived
|
|
11
|
+
* alternates independently by stripping the locale prefix and re-prefixing the
|
|
12
|
+
* remaining path, which silently assumes the slug is identical in every locale.
|
|
13
|
+
* For a pair like DE `/datenschutz/` and EN `/en/privacy/` that assumption is
|
|
14
|
+
* wrong, and the two surfaces failed in different ways: the sitemap omitted the
|
|
15
|
+
* alternates entirely, while the page head emitted `/en/datenschutz/` — a URL
|
|
16
|
+
* that does not exist. One shared model means the two can no longer disagree.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* One translated route, keyed by Astro locale id.
|
|
21
|
+
*
|
|
22
|
+
* Values are complete public route paths including any locale prefix, written
|
|
23
|
+
* exactly as the route is served:
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* { de: '/datenschutz/', en: '/en/privacy/' }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* Trailing slashes are insignificant for matching — consumers render the URL
|
|
30
|
+
* form appropriate to their surface.
|
|
31
|
+
*/
|
|
32
|
+
export type LocalizedPathGroup = Readonly<Record<string, string>>
|
|
33
|
+
|
|
34
|
+
export function stripQueryAndHash(path: string): string {
|
|
35
|
+
const withoutHash = path.split('#')[0] ?? ''
|
|
36
|
+
return withoutHash.split('?')[0] ?? ''
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Which URL form the site actually serves, mirroring Astro's `build.format`:
|
|
41
|
+
* `always` for `directory` output (`/about/`), `never` for `file` (`/about`).
|
|
42
|
+
*/
|
|
43
|
+
export type TrailingSlash = 'always' | 'never'
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The URL form `@easy-web/seo` resolved from `astro.config.mjs`, for callers
|
|
47
|
+
* that run outside an Astro integration and so cannot read the config.
|
|
48
|
+
*
|
|
49
|
+
* Components and instance modules render links with this so internal hrefs
|
|
50
|
+
* point at the canonical URL instead of the form the host will redirect away.
|
|
51
|
+
*/
|
|
52
|
+
export function ambientTrailingSlash(): TrailingSlash {
|
|
53
|
+
const runtime = globalThis as {
|
|
54
|
+
process?: { env?: Record<string, string | undefined> }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return runtime.process?.env?.['EASY_WEB_SEO_TRAILING_SLASH'] === 'never'
|
|
58
|
+
? 'never'
|
|
59
|
+
: 'always'
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Renders a path in the form the site actually serves.
|
|
64
|
+
*
|
|
65
|
+
* Canonical, hreflang alternates and the sitemap must all agree byte-for-byte,
|
|
66
|
+
* or Google treats `/about` and `/about/` as competing duplicates. Instances
|
|
67
|
+
* hand-write `pathname` props in mixed conventions, so every SEO URL is routed
|
|
68
|
+
* through this function rather than trusting the caller's spelling.
|
|
69
|
+
*/
|
|
70
|
+
export function toServedPath(path: string, style: TrailingSlash = 'always'): string {
|
|
71
|
+
const normalized = normalizeLocalizedPath(path)
|
|
72
|
+
|
|
73
|
+
if (normalized === '/' || style === 'never') {
|
|
74
|
+
return normalized
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return `${normalized}/`
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Reduces a path to the form used for comparison and lookup: leading slash,
|
|
82
|
+
* no trailing slash (except root), no query string, no hash.
|
|
83
|
+
*
|
|
84
|
+
* This is what makes `/datenschutz`, `/datenschutz/` and `/datenschutz/?x=1`
|
|
85
|
+
* resolve to the same group. Astro hands `Astro.url.pathname` to the layout
|
|
86
|
+
* without a trailing slash while `@astrojs/sitemap` emits one, so lookup has to
|
|
87
|
+
* be insensitive to that difference.
|
|
88
|
+
*/
|
|
89
|
+
export function normalizeLocalizedPath(path: string): string {
|
|
90
|
+
const withoutQuery = stripQueryAndHash(path)
|
|
91
|
+
const withLeadingSlash = withoutQuery.startsWith('/') ? withoutQuery : `/${withoutQuery}`
|
|
92
|
+
const trimmed = withLeadingSlash.replace(/\/+$/, '')
|
|
93
|
+
|
|
94
|
+
return trimmed === '' ? '/' : trimmed
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Finds the group that claims `path`, or `undefined` when the route uses the
|
|
99
|
+
* same slug in every locale (the common case, handled by prefix derivation).
|
|
100
|
+
*/
|
|
101
|
+
export function findLocalizedGroup(
|
|
102
|
+
path: string,
|
|
103
|
+
groups: readonly LocalizedPathGroup[] | undefined,
|
|
104
|
+
): LocalizedPathGroup | undefined {
|
|
105
|
+
if (!groups || groups.length === 0) {
|
|
106
|
+
return undefined
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const target = normalizeLocalizedPath(path)
|
|
110
|
+
|
|
111
|
+
return groups.find((group) =>
|
|
112
|
+
Object.values(group).some((candidate) => normalizeLocalizedPath(candidate) === target),
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Validates declared groups against the configured locales.
|
|
118
|
+
*
|
|
119
|
+
* Returns a list of human-readable problems; an empty array means valid.
|
|
120
|
+
* Callers decide whether to throw or warn — `@easy-web/seo` fails the build,
|
|
121
|
+
* because a stale mapping produces output that is syntactically valid but
|
|
122
|
+
* silently wrong, which is precisely the failure mode this model exists to
|
|
123
|
+
* eliminate.
|
|
124
|
+
*/
|
|
125
|
+
export function validateLocalizedPaths(
|
|
126
|
+
groups: readonly LocalizedPathGroup[],
|
|
127
|
+
locales: readonly string[],
|
|
128
|
+
): string[] {
|
|
129
|
+
const problems: string[] = []
|
|
130
|
+
const claimedBy = new Map<string, number>()
|
|
131
|
+
|
|
132
|
+
groups.forEach((group, index) => {
|
|
133
|
+
const label = `localizedPaths[${index}]`
|
|
134
|
+
const declared = Object.keys(group)
|
|
135
|
+
|
|
136
|
+
const missing = locales.filter((locale) => !declared.includes(locale))
|
|
137
|
+
if (missing.length > 0) {
|
|
138
|
+
problems.push(`${label} is missing the locale(s): ${missing.join(', ')}.`)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const unknown = declared.filter((locale) => !locales.includes(locale))
|
|
142
|
+
if (unknown.length > 0) {
|
|
143
|
+
problems.push(`${label} declares unknown locale(s): ${unknown.join(', ')}.`)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const seenInGroup = new Set<string>()
|
|
147
|
+
|
|
148
|
+
for (const locale of declared) {
|
|
149
|
+
const value = group[locale] as string
|
|
150
|
+
|
|
151
|
+
if (typeof value !== 'string' || !value.startsWith('/') || value.startsWith('//')) {
|
|
152
|
+
problems.push(
|
|
153
|
+
`${label}.${locale} must be an absolute path beginning with a single "/", got: ${String(value)}.`,
|
|
154
|
+
)
|
|
155
|
+
continue
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (value.includes('?') || value.includes('#')) {
|
|
159
|
+
problems.push(`${label}.${locale} must not contain a query string or hash, got: ${value}.`)
|
|
160
|
+
continue
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const normalized = normalizeLocalizedPath(value)
|
|
164
|
+
|
|
165
|
+
if (seenInGroup.has(normalized)) {
|
|
166
|
+
problems.push(`${label} maps more than one locale to the same path: ${normalized}.`)
|
|
167
|
+
}
|
|
168
|
+
seenInGroup.add(normalized)
|
|
169
|
+
|
|
170
|
+
const owner = claimedBy.get(normalized)
|
|
171
|
+
if (owner !== undefined && owner !== index) {
|
|
172
|
+
problems.push(
|
|
173
|
+
`${normalized} is claimed by both localizedPaths[${owner}] and ${label}; each path may belong to only one group.`,
|
|
174
|
+
)
|
|
175
|
+
}
|
|
176
|
+
claimedBy.set(normalized, index)
|
|
177
|
+
}
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
return problems
|
|
181
|
+
}
|
package/src/i18n/seo.ts
CHANGED
|
@@ -1,131 +1,131 @@
|
|
|
1
|
-
import {
|
|
2
|
-
findLocalizedGroup,
|
|
3
|
-
toServedPath,
|
|
4
|
-
type LocalizedPathGroup,
|
|
5
|
-
type TrailingSlash,
|
|
6
|
-
} from './localized-paths.js'
|
|
7
|
-
|
|
8
|
-
export type AlternateLink = { hreflang: string; href: string }
|
|
9
|
-
|
|
10
|
-
function toServedUrl(url: string, style: TrailingSlash): string {
|
|
11
|
-
const parsed = new URL(url)
|
|
12
|
-
parsed.pathname = toServedPath(parsed.pathname, style)
|
|
13
|
-
return parsed.href
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function normalizePath(path: string): string {
|
|
17
|
-
return path.startsWith('/') ? path : `/${path}`
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function buildUrl(baseUrl: string, localePath: string, path: string, stripLocales: string[] = []): string {
|
|
21
|
-
const base = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl
|
|
22
|
-
|
|
23
|
-
const hashIdx = path.indexOf('#')
|
|
24
|
-
const hash = hashIdx !== -1 ? path.slice(hashIdx) : ''
|
|
25
|
-
const pathWithoutHash = hashIdx !== -1 ? path.slice(0, hashIdx) : path
|
|
26
|
-
const queryIdx = pathWithoutHash.indexOf('?')
|
|
27
|
-
const search = queryIdx !== -1 ? pathWithoutHash.slice(queryIdx) : ''
|
|
28
|
-
const pathOnly = queryIdx !== -1 ? pathWithoutHash.slice(0, queryIdx) : pathWithoutHash
|
|
29
|
-
|
|
30
|
-
const normalizedPathOnly = normalizePath(pathOnly)
|
|
31
|
-
const segments = normalizedPathOnly.split('/').filter((s, i) => i > 0 || s !== '')
|
|
32
|
-
const wasLocaleOnlyPath = segments.length === 1 && stripLocales.includes(segments[0] ?? '')
|
|
33
|
-
let cleanPath: string
|
|
34
|
-
if (segments.length > 0 && stripLocales.includes(segments[0])) {
|
|
35
|
-
const rest = segments.slice(1)
|
|
36
|
-
cleanPath = rest.length > 0 ? `/${rest.join('/')}` : '/'
|
|
37
|
-
} else {
|
|
38
|
-
cleanPath = normalizedPathOnly
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const suffix = search + hash
|
|
42
|
-
|
|
43
|
-
if (localePath === '') {
|
|
44
|
-
return base + cleanPath + suffix
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const localeSegment = `/${localePath}`
|
|
48
|
-
if (cleanPath === '/') {
|
|
49
|
-
if (wasLocaleOnlyPath) {
|
|
50
|
-
return `${base}${localeSegment}${suffix}`
|
|
51
|
-
}
|
|
52
|
-
return `${base}${localeSegment}/${suffix}`
|
|
53
|
-
}
|
|
54
|
-
return `${base}${localeSegment}${cleanPath}${suffix}`
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function getAlternateLinks(opts: {
|
|
58
|
-
path: string
|
|
59
|
-
locales: readonly string[]
|
|
60
|
-
defaultLocale: string
|
|
61
|
-
baseUrl: string
|
|
62
|
-
/**
|
|
63
|
-
* Routes whose slug differs per locale. When `path` belongs to one of these
|
|
64
|
-
* groups the alternates are read from the group instead of being derived by
|
|
65
|
-
* re-prefixing `path`, which would otherwise point at a URL that does not
|
|
66
|
-
* exist (e.g. `/en/datenschutz` for the DE privacy page).
|
|
67
|
-
*/
|
|
68
|
-
localizedPaths?: readonly LocalizedPathGroup[]
|
|
69
|
-
/** URL form to emit; must match what the site serves. Defaults to Astro's `directory` output. */
|
|
70
|
-
trailingSlash?: TrailingSlash
|
|
71
|
-
}): AlternateLink[] {
|
|
72
|
-
const { path, locales, defaultLocale, baseUrl, localizedPaths } = opts
|
|
73
|
-
const style = opts.trailingSlash ?? 'always'
|
|
74
|
-
|
|
75
|
-
const group = findLocalizedGroup(path, localizedPaths)
|
|
76
|
-
|
|
77
|
-
if (group) {
|
|
78
|
-
const base = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl
|
|
79
|
-
const hrefFor = (locale: string): string | undefined => {
|
|
80
|
-
const declared = group[locale]
|
|
81
|
-
return declared === undefined ? undefined : base + toServedPath(declared, style)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const links: AlternateLink[] = []
|
|
85
|
-
|
|
86
|
-
for (const locale of locales) {
|
|
87
|
-
const href = hrefFor(locale)
|
|
88
|
-
// Omit untranslated locales rather than inventing a URL — the bug this fixes.
|
|
89
|
-
if (href !== undefined) {
|
|
90
|
-
links.push({ hreflang: locale, href })
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const fallback = hrefFor(defaultLocale)
|
|
95
|
-
if (fallback !== undefined) {
|
|
96
|
-
links.push({ hreflang: 'x-default', href: fallback })
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return links
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const stripLocales = [...locales]
|
|
103
|
-
const links: AlternateLink[] = locales.map((locale) => ({
|
|
104
|
-
hreflang: locale,
|
|
105
|
-
href: toServedUrl(
|
|
106
|
-
buildUrl(baseUrl, locale !== defaultLocale ? locale : '', path, stripLocales),
|
|
107
|
-
style,
|
|
108
|
-
),
|
|
109
|
-
}))
|
|
110
|
-
|
|
111
|
-
links.push({
|
|
112
|
-
hreflang: 'x-default',
|
|
113
|
-
href: toServedUrl(buildUrl(baseUrl, '', path, stripLocales), style),
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
return links
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export function getCanonicalUrl(opts: {
|
|
120
|
-
path: string
|
|
121
|
-
locale: string
|
|
122
|
-
defaultLocale: string
|
|
123
|
-
baseUrl: string
|
|
124
|
-
trailingSlash?: TrailingSlash
|
|
125
|
-
}): string {
|
|
126
|
-
const { path, locale, defaultLocale, baseUrl } = opts
|
|
127
|
-
return toServedUrl(
|
|
128
|
-
buildUrl(baseUrl, locale !== defaultLocale ? locale : '', path, [locale, defaultLocale]),
|
|
129
|
-
opts.trailingSlash ?? 'always',
|
|
130
|
-
)
|
|
131
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
findLocalizedGroup,
|
|
3
|
+
toServedPath,
|
|
4
|
+
type LocalizedPathGroup,
|
|
5
|
+
type TrailingSlash,
|
|
6
|
+
} from './localized-paths.js'
|
|
7
|
+
|
|
8
|
+
export type AlternateLink = { hreflang: string; href: string }
|
|
9
|
+
|
|
10
|
+
function toServedUrl(url: string, style: TrailingSlash): string {
|
|
11
|
+
const parsed = new URL(url)
|
|
12
|
+
parsed.pathname = toServedPath(parsed.pathname, style)
|
|
13
|
+
return parsed.href
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function normalizePath(path: string): string {
|
|
17
|
+
return path.startsWith('/') ? path : `/${path}`
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function buildUrl(baseUrl: string, localePath: string, path: string, stripLocales: string[] = []): string {
|
|
21
|
+
const base = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl
|
|
22
|
+
|
|
23
|
+
const hashIdx = path.indexOf('#')
|
|
24
|
+
const hash = hashIdx !== -1 ? path.slice(hashIdx) : ''
|
|
25
|
+
const pathWithoutHash = hashIdx !== -1 ? path.slice(0, hashIdx) : path
|
|
26
|
+
const queryIdx = pathWithoutHash.indexOf('?')
|
|
27
|
+
const search = queryIdx !== -1 ? pathWithoutHash.slice(queryIdx) : ''
|
|
28
|
+
const pathOnly = queryIdx !== -1 ? pathWithoutHash.slice(0, queryIdx) : pathWithoutHash
|
|
29
|
+
|
|
30
|
+
const normalizedPathOnly = normalizePath(pathOnly)
|
|
31
|
+
const segments = normalizedPathOnly.split('/').filter((s, i) => i > 0 || s !== '')
|
|
32
|
+
const wasLocaleOnlyPath = segments.length === 1 && stripLocales.includes(segments[0] ?? '')
|
|
33
|
+
let cleanPath: string
|
|
34
|
+
if (segments.length > 0 && stripLocales.includes(segments[0])) {
|
|
35
|
+
const rest = segments.slice(1)
|
|
36
|
+
cleanPath = rest.length > 0 ? `/${rest.join('/')}` : '/'
|
|
37
|
+
} else {
|
|
38
|
+
cleanPath = normalizedPathOnly
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const suffix = search + hash
|
|
42
|
+
|
|
43
|
+
if (localePath === '') {
|
|
44
|
+
return base + cleanPath + suffix
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const localeSegment = `/${localePath}`
|
|
48
|
+
if (cleanPath === '/') {
|
|
49
|
+
if (wasLocaleOnlyPath) {
|
|
50
|
+
return `${base}${localeSegment}${suffix}`
|
|
51
|
+
}
|
|
52
|
+
return `${base}${localeSegment}/${suffix}`
|
|
53
|
+
}
|
|
54
|
+
return `${base}${localeSegment}${cleanPath}${suffix}`
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function getAlternateLinks(opts: {
|
|
58
|
+
path: string
|
|
59
|
+
locales: readonly string[]
|
|
60
|
+
defaultLocale: string
|
|
61
|
+
baseUrl: string
|
|
62
|
+
/**
|
|
63
|
+
* Routes whose slug differs per locale. When `path` belongs to one of these
|
|
64
|
+
* groups the alternates are read from the group instead of being derived by
|
|
65
|
+
* re-prefixing `path`, which would otherwise point at a URL that does not
|
|
66
|
+
* exist (e.g. `/en/datenschutz` for the DE privacy page).
|
|
67
|
+
*/
|
|
68
|
+
localizedPaths?: readonly LocalizedPathGroup[]
|
|
69
|
+
/** URL form to emit; must match what the site serves. Defaults to Astro's `directory` output. */
|
|
70
|
+
trailingSlash?: TrailingSlash
|
|
71
|
+
}): AlternateLink[] {
|
|
72
|
+
const { path, locales, defaultLocale, baseUrl, localizedPaths } = opts
|
|
73
|
+
const style = opts.trailingSlash ?? 'always'
|
|
74
|
+
|
|
75
|
+
const group = findLocalizedGroup(path, localizedPaths)
|
|
76
|
+
|
|
77
|
+
if (group) {
|
|
78
|
+
const base = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl
|
|
79
|
+
const hrefFor = (locale: string): string | undefined => {
|
|
80
|
+
const declared = group[locale]
|
|
81
|
+
return declared === undefined ? undefined : base + toServedPath(declared, style)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const links: AlternateLink[] = []
|
|
85
|
+
|
|
86
|
+
for (const locale of locales) {
|
|
87
|
+
const href = hrefFor(locale)
|
|
88
|
+
// Omit untranslated locales rather than inventing a URL — the bug this fixes.
|
|
89
|
+
if (href !== undefined) {
|
|
90
|
+
links.push({ hreflang: locale, href })
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const fallback = hrefFor(defaultLocale)
|
|
95
|
+
if (fallback !== undefined) {
|
|
96
|
+
links.push({ hreflang: 'x-default', href: fallback })
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return links
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const stripLocales = [...locales]
|
|
103
|
+
const links: AlternateLink[] = locales.map((locale) => ({
|
|
104
|
+
hreflang: locale,
|
|
105
|
+
href: toServedUrl(
|
|
106
|
+
buildUrl(baseUrl, locale !== defaultLocale ? locale : '', path, stripLocales),
|
|
107
|
+
style,
|
|
108
|
+
),
|
|
109
|
+
}))
|
|
110
|
+
|
|
111
|
+
links.push({
|
|
112
|
+
hreflang: 'x-default',
|
|
113
|
+
href: toServedUrl(buildUrl(baseUrl, '', path, stripLocales), style),
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
return links
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function getCanonicalUrl(opts: {
|
|
120
|
+
path: string
|
|
121
|
+
locale: string
|
|
122
|
+
defaultLocale: string
|
|
123
|
+
baseUrl: string
|
|
124
|
+
trailingSlash?: TrailingSlash
|
|
125
|
+
}): string {
|
|
126
|
+
const { path, locale, defaultLocale, baseUrl } = opts
|
|
127
|
+
return toServedUrl(
|
|
128
|
+
buildUrl(baseUrl, locale !== defaultLocale ? locale : '', path, [locale, defaultLocale]),
|
|
129
|
+
opts.trailingSlash ?? 'always',
|
|
130
|
+
)
|
|
131
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-

|
|
1
|
+

|
|
@@ -1 +1 @@
|
|
|
1
|
-

|
|
1
|
+

|
|
@@ -1 +1 @@
|
|
|
1
|
-

|
|
1
|
+

|
|
@@ -1 +1 @@
|
|
|
1
|
-

|
|
1
|
+

|