Skip to content
Merged
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: 5 additions & 0 deletions .changeset/wide-kiwis-write.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gitbook": patch
---

Add support for localized site section titles
4 changes: 2 additions & 2 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"catalog": {
"@tsconfig/strictest": "^2.0.6",
"@tsconfig/node20": "^20.1.6",
"@gitbook/api": "0.165.0",
"@gitbook/api": "0.166.0",
"@scalar/api-client-react": "^1.3.46",
"@types/react": "^19.0.0",
"@types/react-dom": "^19.0.0",
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
27 changes: 26 additions & 1 deletion packages/gitbook/src/lib/sites.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { GitBookSiteContext } from '@/lib/context';
import type { SiteSection, SiteSectionGroup, SiteSpace, SiteStructure } from '@gitbook/api';
import type {
SiteSection,
SiteSectionGroup,
SiteSpace,
SiteStructure,
TranslationLanguage,
} from '@gitbook/api';
import { joinPath } from './paths';
import { flattenSectionsFromGroup } from './utils';

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

/**
* Get the appropriate title for a siteSection.
* Uses localizedTitle if available and current language is provided, otherwise uses title.
*/
export function getSiteSectionTitle(
siteSection: SiteSection,
currentLanguage: TranslationLanguage | undefined
): string {
if (
siteSection.localizedTitle &&
currentLanguage &&
siteSection.localizedTitle[currentLanguage]
) {
return siteSection.localizedTitle[currentLanguage];
}

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