File tree Expand file tree Collapse file tree 2 files changed +52
-12
lines changed
src/templates/assets/javascripts/integrations/version Expand file tree Collapse file tree 2 files changed +52
-12
lines changed Original file line number Diff line number Diff line change 1+ // This is a separate file so that `mocha` can load it without needing
2+ // DOM shims.
3+
4+ import { Sitemap } from "../sitemap"
5+
6+ type CorrespondingURLParams = {
7+ selectedVersionSitemap : Sitemap
8+ selectedVersionBaseURL : URL
9+ currentLocation : URL
10+ currentBaseURL : string
11+ }
12+
13+ /**
14+ * Choose a URL to navigate to when the user chooses a version in the version
15+ * selector.
16+ *
17+ * @param selectedVersionSitemap
18+ * @param selectedVersionBaseURL
19+ * @param currentLocation
20+ * @param currentBaseURL
21+ * @returns the URL to navigate to or null if we can't be sure that the
22+ * corresponding page to the current page exists in the selected version
23+ */
24+ export function selectedVersionCorrespondingURL (
25+ { selectedVersionSitemap,
26+ selectedVersionBaseURL,
27+ currentLocation,
28+ currentBaseURL} : CorrespondingURLParams
29+ ) : URL | undefined {
30+ const result = currentLocation . href . replace (
31+ currentBaseURL ,
32+ selectedVersionBaseURL . href ,
33+ )
34+ return selectedVersionSitemap . has ( result . split ( "#" ) [ 0 ] )
35+ ? new URL ( result )
36+ : undefined
37+ }
Original file line number Diff line number Diff line change @@ -48,6 +48,8 @@ import {
4848
4949import { fetchSitemap } from "../sitemap"
5050
51+ import { selectedVersionCorrespondingURL } from "./correspondingPage"
52+
5153/* ----------------------------------------------------------------------------
5254 * Helper types
5355 * ------------------------------------------------------------------------- */
@@ -122,22 +124,23 @@ export function setupVersionSelector(
122124 return EMPTY
123125 }
124126 ev . preventDefault ( )
125- return of ( url )
127+ return of ( new URL ( url ) )
126128 }
127129 }
128130 return EMPTY
129131 } ) ,
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+ )
141144 } )
142145 )
143146 )
You can’t perform that action at this time.
0 commit comments