Skip to content

Commit 6b47977

Browse files
committed
Persist language choice across sections
1 parent 61d1a01 commit 6b47977

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

.changeset/red-phones-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook": patch
3+
---
4+
5+
Persist language choice across sections if possible

packages/gitbook/src/components/SiteSections/encodeClientSiteSections.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@ function shouldIncludeSection(context: GitBookSiteContext, section: SiteSection)
104104
/**
105105
* Find the best default site space to navigate to for a givent section:
106106
* 1. If we are on the default, continue on the default.
107-
* 2. If a site space has the same path as the current one, return it.
108-
* 3. Otherwise, return the default one.
107+
* 2. If there are site spaces with the same language as the current, filter by language.
108+
* 3. If a site space has the same path as the current one, return it.
109+
* 4. Otherwise, return the default first language match.
110+
* 5. Otherwise, return the default one.
109111
*/
110112
function findBestTargetURL(context: GitBookSiteContext, section: SiteSection) {
111113
const { siteSpace: currentSiteSpace } = context;
@@ -114,9 +116,15 @@ function findBestTargetURL(context: GitBookSiteContext, section: SiteSection) {
114116
return getSectionURL(context, section);
115117
}
116118

117-
const bestMatch = section.siteSpaces.find((siteSpace) =>
118-
areSiteSpacesEquivalent(siteSpace, currentSiteSpace)
119-
);
119+
const possibleMatches =
120+
section.siteSpaces.filter((siteSpace) =>
121+
areSiteSpacesSameLanguage(siteSpace, currentSiteSpace)
122+
) ?? section.siteSpaces;
123+
124+
const bestMatch =
125+
possibleMatches.find((siteSpace) => areSiteSpacesEquivalent(siteSpace, currentSiteSpace)) ??
126+
possibleMatches[0];
127+
120128
if (bestMatch) {
121129
return getSiteSpaceURL(context, bestMatch);
122130
}
@@ -130,3 +138,7 @@ function findBestTargetURL(context: GitBookSiteContext, section: SiteSection) {
130138
function areSiteSpacesEquivalent(siteSpace1: SiteSpace, siteSpace2: SiteSpace) {
131139
return siteSpace1.path === siteSpace2.path;
132140
}
141+
142+
function areSiteSpacesSameLanguage(siteSpace1: SiteSpace, siteSpace2: SiteSpace) {
143+
return siteSpace1.space.language === siteSpace2.space.language;
144+
}

0 commit comments

Comments
 (0)