-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract function for core logic of "staying on the same page when swi…
…tching versions" We will add unit tests for this functions in the next commit. The function gets its own file because I was unable to get the test runner ("mocha") to work otherwise. See the child commit's description for more details.
- Loading branch information
Showing
2 changed files
with
52 additions
and
12 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/templates/assets/javascripts/integrations/version/correspondingPage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// This is a separate file so that `mocha` can load it without needing | ||
// DOM shims. | ||
|
||
import { Sitemap } from "../sitemap" | ||
|
||
type CorrespondingURLParams = { | ||
selectedVersionSitemap: Sitemap | ||
selectedVersionBaseURL: URL | ||
currentLocation: URL | ||
currentBaseURL: string | ||
} | ||
|
||
/** | ||
* Choose a URL to navigate to when the user chooses a version in the version | ||
* selector. | ||
* | ||
* @param selectedVersionSitemap | ||
* @param selectedVersionBaseURL | ||
* @param currentLocation | ||
* @param currentBaseURL | ||
* @returns the URL to navigate to or null if we can't be sure that the | ||
* corresponding page to the current page exists in the selected version | ||
*/ | ||
export function selectedVersionCorrespondingURL( | ||
{selectedVersionSitemap, | ||
selectedVersionBaseURL, | ||
currentLocation, | ||
currentBaseURL}: CorrespondingURLParams | ||
): URL | undefined { | ||
const result = currentLocation.href.replace( | ||
currentBaseURL, | ||
selectedVersionBaseURL.href, | ||
) | ||
return selectedVersionSitemap.has(result.split("#")[0]) | ||
? new URL(result) | ||
: undefined | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters