-
Notifications
You must be signed in to change notification settings - Fork 66.7k
Expand file tree
/
Copy pathresolve-path.ts
More file actions
62 lines (55 loc) · 2.02 KB
/
resolve-path.ts
File metadata and controls
62 lines (55 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import findPage from '@/frame/lib/find-page'
import { allVersionKeys } from '@/versions/lib/all-versions'
import type { Context, Page } from '@/types'
/**
* Resolves an href to a Page object from the context.
*
* Normalizes various href formats (relative, absolute, with/without language
* prefix) to canonical paths, then delegates to findPage for lookup with
* redirect support and English fallback.
*/
export function resolvePath(
href: string,
languageCode: string,
pathname: string,
context: Context,
): Page | undefined {
if (href.startsWith('http://') || href.startsWith('https://')) return undefined
if (!context.pages) return undefined
const { pages, redirects } = context
for (const candidate of candidates(href, languageCode, pathname)) {
const page =
findPage(candidate, pages, redirects) ||
findPage(candidate.replace(/\/?$/, '/'), pages, redirects)
if (page) return page
}
return undefined
}
// Lazily yields candidate paths in priority order, stopping at first match.
function* candidates(href: string, lang: string, pathname: string) {
const langPrefix = `/${lang}/`
const cleanPathname = pathname.replace(/\/$/, '')
if (href.startsWith(langPrefix)) {
// Already has language prefix — use as-is
yield href
} else if (href.startsWith('/')) {
// Leading slash without lang prefix — try relative to pathname first,
// then as a direct path with lang prefix
yield `${cleanPathname}${href}`
yield `${langPrefix.slice(0, -1)}${href}`
} else {
// Relative path — try relative to pathname, then with lang prefix
yield `${cleanPathname}/${href}`
yield `${langPrefix}${href}`
}
// Versioned fallback: try inserting each version slug for
// enterprise-only pages that don't exist on FPT.
const suffix = href.startsWith(langPrefix)
? href.slice(langPrefix.length).replace(/\/$/, '')
: href.replace(/^\//, '').replace(/\/$/, '')
if (suffix) {
for (const version of allVersionKeys) {
yield `${langPrefix}${version}/${suffix}`
}
}
}