Skip to content

Commit aea5eb1

Browse files
authored
Persist language choice across sections (#3633)
1 parent 1165a81 commit aea5eb1

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
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: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ export function encodeClientSiteSections(context: GitBookSiteContext, sections:
3030
for (const item of list) {
3131
switch (item.object) {
3232
case 'site-section-group': {
33-
const sections = item.sections
34-
.filter((section) => shouldIncludeSection(context, section))
35-
.map((section) => encodeSection(context, section));
33+
const sections = item.sections.map((section) => encodeSection(context, section));
3634

3735
// Skip empty groups
3836
if (sections.length === 0) {
@@ -74,38 +72,13 @@ function encodeSection(context: GitBookSiteContext, section: SiteSection) {
7472
};
7573
}
7674

77-
/**
78-
* Test if a section should be included in the list of sections.
79-
*/
80-
function shouldIncludeSection(context: GitBookSiteContext, section: SiteSection) {
81-
if (context.site.id !== 'site_JOVzv') {
82-
return true;
83-
}
84-
85-
// Testing for a new mode of navigation where the multi-variants section are hidden
86-
// if they do not include an equivalent of the current site space.
87-
88-
// TODO: replace with a proper flag on the section
89-
const withNavigateOnlyIfEquivalent = section.id === 'sitesc_4jvEm';
90-
91-
if (!withNavigateOnlyIfEquivalent) {
92-
return true;
93-
}
94-
95-
const { siteSpace: currentSiteSpace } = context;
96-
if (section.siteSpaces.length === 1) {
97-
return true;
98-
}
99-
return section.siteSpaces.some((siteSpace) =>
100-
areSiteSpacesEquivalent(siteSpace, currentSiteSpace)
101-
);
102-
}
103-
10475
/**
10576
* Find the best default site space to navigate to for a givent section:
10677
* 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.
78+
* 2. If there are site spaces with the same language as the current, filter by language.
79+
* 3. If a site space has the same path as the current one, return it.
80+
* 4. Otherwise, return the default first language match.
81+
* 5. Otherwise, return the default one.
10982
*/
11083
function findBestTargetURL(context: GitBookSiteContext, section: SiteSection) {
11184
const { siteSpace: currentSiteSpace } = context;
@@ -114,9 +87,15 @@ function findBestTargetURL(context: GitBookSiteContext, section: SiteSection) {
11487
return getSectionURL(context, section);
11588
}
11689

117-
const bestMatch = section.siteSpaces.find((siteSpace) =>
118-
areSiteSpacesEquivalent(siteSpace, currentSiteSpace)
119-
);
90+
const possibleMatches =
91+
section.siteSpaces.filter((siteSpace) =>
92+
areSiteSpacesSameLanguage(siteSpace, currentSiteSpace)
93+
) ?? section.siteSpaces;
94+
95+
const bestMatch =
96+
possibleMatches.find((siteSpace) => areSiteSpacesEquivalent(siteSpace, currentSiteSpace)) ??
97+
possibleMatches[0];
98+
12099
if (bestMatch) {
121100
return getSiteSpaceURL(context, bestMatch);
122101
}
@@ -130,3 +109,7 @@ function findBestTargetURL(context: GitBookSiteContext, section: SiteSection) {
130109
function areSiteSpacesEquivalent(siteSpace1: SiteSpace, siteSpace2: SiteSpace) {
131110
return siteSpace1.path === siteSpace2.path;
132111
}
112+
113+
function areSiteSpacesSameLanguage(siteSpace1: SiteSpace, siteSpace2: SiteSpace) {
114+
return siteSpace1.space.language === siteSpace2.space.language;
115+
}

0 commit comments

Comments
 (0)