Skip to content

Experiment with hiding sections without a matching site space #3318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getSectionURL, getSiteSpaceURL } from '@/lib/sites';
import type { SiteSection, SiteSectionGroup } from '@gitbook/api';
import type { SiteSection, SiteSectionGroup, SiteSpace } from '@gitbook/api';
import type { GitBookSiteContext, SiteSections } from '@v2/lib/context';

export type ClientSiteSections = {
Expand Down Expand Up @@ -33,10 +33,14 @@ export function encodeClientSiteSections(context: GitBookSiteContext, sections:
title: item.title,
icon: item.icon,
object: item.object,
sections: item.sections.map((section) => encodeSection(context, section)),
sections: item.sections
.filter((section) => shouldIncludeSection(context, section))
.map((section) => encodeSection(context, section)),
});
} else {
clientSections.push(encodeSection(context, item));
if (shouldIncludeSection(context, item)) {
clientSections.push(encodeSection(context, item));
}
}
}

Expand All @@ -57,6 +61,33 @@ function encodeSection(context: GitBookSiteContext, section: SiteSection) {
};
}

/**
* Test if a section should be included in the list of sections.
*/
function shouldIncludeSection(context: GitBookSiteContext, section: SiteSection) {
if (context.site.id !== 'site_JOVzv') {
return true;
}

// Testing for a new mode of navigation where the multi-variants section are hidden
// if they do not include an equivalent of the current site space.

// TODO: replace with a proper flag on the section
const withNavigateOnlyIfEquivalent = section.id === 'sitesc_4jvEm';

if (!withNavigateOnlyIfEquivalent) {
return true;
}

const { siteSpace: currentSiteSpace } = context;
if (section.siteSpaces.length === 1) {
return true;
}
return section.siteSpaces.some((siteSpace) =>
areSiteSpacesEquivalent(siteSpace, currentSiteSpace)
);
}

/**
* Find the best default site space to navigate to for a givent section:
* 1. If we are on the default, continue on the default.
Expand All @@ -70,12 +101,19 @@ function findBestTargetURL(context: GitBookSiteContext, section: SiteSection) {
return getSectionURL(context, section);
}

const bestMatch = section.siteSpaces.find(
(siteSpace) => siteSpace.path === currentSiteSpace.path
const bestMatch = section.siteSpaces.find((siteSpace) =>
areSiteSpacesEquivalent(siteSpace, currentSiteSpace)
);
if (bestMatch) {
return getSiteSpaceURL(context, bestMatch);
}

return getSectionURL(context, section);
}

/**
* Test if 2 site spaces are equivalent.
*/
function areSiteSpacesEquivalent(siteSpace1: SiteSpace, siteSpace2: SiteSpace) {
return siteSpace1.path === siteSpace2.path;
}
Loading