Skip to content
Draft
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions packages/gitbook/src/components/Search/server-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type { GitBookBaseContext, GitBookSiteContext } from '@/lib/context';
import { resolvePageId } from '@/lib/pages';
import { fetchServerActionSiteContext, getServerActionBaseContext } from '@/lib/server-actions';
import { findSiteSpaceBy } from '@/lib/sites';
import { findSiteSpaceBy, getSiteSectionTitle } from '@/lib/sites';
import { filterOutNullable } from '@/lib/typescript';
import type {
Revision,
Expand Down Expand Up @@ -350,6 +350,7 @@ async function transformSitePageResult(
): Promise<OrderedComputedResult[]> {
const { pageItem, spaceItem, spaceURL, siteSection, siteSectionGroup, siteSpace } = args;
const { linker } = context;
const currentLanguage = siteSpace?.space.language;

const page: ComputedPageResult = {
type: 'page',
Expand All @@ -367,7 +368,7 @@ async function transformSitePageResult(
},
siteSection && {
icon: siteSection?.icon as IconName,
label: siteSection.title,
label: getSiteSectionTitle(siteSection, currentLanguage),
},
(siteSection?.siteSpaces?.filter(
// If a space is the only one in its langauge, it's a translation variant and we don't want to show it.
Expand Down
4 changes: 3 additions & 1 deletion packages/gitbook/src/components/SitePage/SitePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { isPageIndexable, isSiteIndexable } from '@/lib/seo';

import { getResizedImageURL } from '@/lib/images';
import { resolveContentRef } from '@/lib/references';
import { getSiteSectionTitle } from '@/lib/sites';
import { tcls } from '@/lib/tailwind';
import { getPageRSSURL } from '@/routes/rss';
import { PageContextProvider } from '../PageContext';
Expand Down Expand Up @@ -125,14 +126,15 @@ export async function generateSitePageViewport(context: GitBookSiteContext): Pro
*/
function getSiteStructureTitle(context: GitBookSiteContext): string | null {
const { visibleSections: sections, siteSpace, visibleSiteSpaces: siteSpaces } = context;
const currentLanguage = siteSpace.space.language;

const title = [];
if (
sections &&
sections.current.default === false && // Only if the current section is not the default one
sections.list.filter((section) => section.object === 'site-section').length > 1 // Only if there are multiple sections
) {
title.push(sections.current.title);
title.push(getSiteSectionTitle(sections.current, currentLanguage));
}
if (
siteSpaces.length > 1 && // Only if there are multiple variants
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { GitBookSiteContext, SiteSections } from '@/lib/context';
import { getSectionURL, getSiteSpaceURL } from '@/lib/sites';
import { getSectionURL, getSiteSectionTitle, getSiteSpaceURL } from '@/lib/sites';
import type { SiteSection, SiteSectionGroup, SiteSpace } from '@gitbook/api';
import assertNever from 'assert-never';

Expand Down Expand Up @@ -99,9 +99,10 @@ function encodeChildren(
}

function encodeSection(context: GitBookSiteContext, section: SiteSection) {
const currentLanguage = context.siteSpace.space.language;
return {
id: section.id,
title: section.title,
title: getSiteSectionTitle(section, currentLanguage),
description: section.description,
icon: section.icon,
object: section.object,
Expand Down
48 changes: 47 additions & 1 deletion packages/gitbook/src/lib/sites.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import type { GitBookSiteContext } from '@/lib/context';
import type { SiteSection, SiteSectionGroup, SiteSpace, SiteStructure } from '@gitbook/api';
import { filterOutNullable } from '@/lib/typescript';
import {
type SiteSection,
type SiteSectionGroup,
type SiteSpace,
type SiteStructure,
TranslationLanguage,
} from '@gitbook/api';
import { joinPath } from './paths';
import { flattenSectionsFromGroup } from './utils';

Expand Down Expand Up @@ -177,3 +184,42 @@ function findSiteSpaceByIdInSiteSpaces(
): SiteSpace | null {
return siteSpaces.find(predicate) ?? null;
}

/**
* Check if a siteSection has variants with multiple languages.
*/
function hasMultipleLanguagesInVariants(siteSection: SiteSection): boolean {
if (!siteSection.siteSpaces || siteSection.siteSpaces.length < 2) {
return false;
}

const languages = new Set(
siteSection.siteSpaces.map((space) => space.space.language).filter(filterOutNullable)
);

return languages.size > 1;
}

/**
* Get the appropriate title for a siteSection.
* Uses localizedTitle if the section has variants with multiple languages, otherwise uses title.
*/
export function getSiteSectionTitle(
siteSection: SiteSection,
currentLanguage: TranslationLanguage | undefined
): string {
const hasMultipleLanguages = hasMultipleLanguagesInVariants(siteSection);

if (hasMultipleLanguages && siteSection.localizedTitle) {
// Use localizedTitle if available and section has multiple languages
if (currentLanguage && siteSection.localizedTitle[currentLanguage]) {
return siteSection.localizedTitle[currentLanguage];
}
// Fallback to 'en' if current language not found, or to title if 'en' not available
if (siteSection.localizedTitle[TranslationLanguage.En]) {
return siteSection.localizedTitle[TranslationLanguage.En];
}
}

return siteSection.title;
}
7 changes: 5 additions & 2 deletions packages/gitbook/src/routes/llms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { throwIfDataError } from '@/lib/data';
import type { GitBookLinker } from '@/lib/links';
import { joinPath } from '@/lib/paths';
import { type FlatPageEntry, getIndexablePages } from '@/lib/sitemap';
import { getSiteStructureSections } from '@/lib/sites';
import { getSiteSectionTitle, getSiteStructureSections } from '@/lib/sites';
import type { SiteSection, SiteSpace } from '@gitbook/api';
import assertNever from 'assert-never';
import type { ListItem, Paragraph, Root, RootContent } from 'mdast';
Expand Down Expand Up @@ -89,6 +89,7 @@ async function getNodesFromSections(
withMarkdownPages: boolean;
}
): Promise<RootContent[]> {
const currentLanguage = context.siteSpace.space.language;
const all = await Promise.all(
siteSections.map(async (siteSection): Promise<RootContent[]> => {
const siteSpaceNodes = await getNodesFromSiteSpaces(context, siteSection.siteSpaces, {
Expand All @@ -99,7 +100,9 @@ async function getNodesFromSections(
{
type: 'heading',
depth: 2,
children: [{ type: 'text', value: siteSection.title }],
children: [
{ type: 'text', value: getSiteSectionTitle(siteSection, currentLanguage) },
],
},
...siteSpaceNodes,
];
Expand Down
Loading