File tree Expand file tree Collapse file tree 2 files changed +49
-12
lines changed
src/templates/assets/javascripts/integrations/version Expand file tree Collapse file tree 2 files changed +49
-12
lines changed Original file line number Diff line number Diff line change
1
+ import { Sitemap } from "../sitemap"
2
+
3
+ type CorrespondingURLParams = {
4
+ selectedVersionSitemap : Sitemap
5
+ selectedVersionBaseURL : URL
6
+ currentLocation : URL
7
+ currentBaseURL : string
8
+ }
9
+
10
+ /**
11
+ * Choose a URL to navigate to when the user chooses a version in the version
12
+ * selector.
13
+ *
14
+ * @param selectedVersionSitemap - as obtained by fetchSitemap from `${selectedVersionBaseURL}/sitemap.xml`
15
+ * @param selectedVersionBaseURL - usually `${currentBaseURL}/../selectedVersion`
16
+ * @param currentLocation - current web browser location
17
+ * @param currentBaseURL - as obtained from `config.base`
18
+ * @returns the URL to navigate to or null if we can't be sure that the
19
+ * corresponding page to the current page exists in the selected version
20
+ */
21
+ export function selectedVersionCorrespondingURL (
22
+ { selectedVersionSitemap,
23
+ selectedVersionBaseURL,
24
+ currentLocation,
25
+ currentBaseURL} : CorrespondingURLParams
26
+ ) : URL | undefined {
27
+ const result = currentLocation . href . replace (
28
+ currentBaseURL ,
29
+ selectedVersionBaseURL . href ,
30
+ )
31
+ return selectedVersionSitemap . has ( result . split ( "#" ) [ 0 ] )
32
+ ? new URL ( result )
33
+ : undefined
34
+ }
Original file line number Diff line number Diff line change @@ -48,6 +48,8 @@ import {
48
48
49
49
import { fetchSitemap } from "../sitemap"
50
50
51
+ import { selectedVersionCorrespondingURL } from "./correspondingPage"
52
+
51
53
/* ----------------------------------------------------------------------------
52
54
* Helper types
53
55
* ------------------------------------------------------------------------- */
@@ -122,22 +124,23 @@ export function setupVersionSelector(
122
124
return EMPTY
123
125
}
124
126
ev . preventDefault ( )
125
- return of ( url )
127
+ return of ( new URL ( url ) )
126
128
}
127
129
}
128
130
return EMPTY
129
131
} ) ,
130
- switchMap ( url => {
131
- return fetchSitemap ( new URL ( url ) )
132
- . pipe (
133
- map ( sitemap => {
134
- const location = getLocation ( )
135
- const path = location . href . replace ( config . base , url )
136
- return sitemap . has ( path . split ( "#" ) [ 0 ] )
137
- ? new URL ( path )
138
- : new URL ( url )
139
- } )
140
- )
132
+ switchMap ( selectedVersionBaseURL => {
133
+ return fetchSitemap ( selectedVersionBaseURL ) . pipe (
134
+ map (
135
+ sitemap =>
136
+ selectedVersionCorrespondingURL ( {
137
+ selectedVersionSitemap : sitemap ,
138
+ selectedVersionBaseURL,
139
+ currentLocation : getLocation ( ) ,
140
+ currentBaseURL : config . base
141
+ } ) ?? selectedVersionBaseURL ,
142
+ ) ,
143
+ )
141
144
} )
142
145
)
143
146
)
You can’t perform that action at this time.
0 commit comments