Skip to content

Commit 6540c52

Browse files
committed
fix(docs): preserve localized navigation parity
1 parent 7838c6a commit 6540c52

4 files changed

Lines changed: 164 additions & 7 deletions

File tree

‎docs/.i18n/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ Generated locale trees and live translation memory now live in the publish repo:
4040
## Files in this folder
4141

4242
- `glossary.<lang>.json` — preferred term mappings used as prompt guidance.
43-
- `zh-Hans-navigation.json` — curated zh-Hans Mintlify locale navigation reinserted into the publish repo during sync.
44-
- `ar-navigation.json`, `de-navigation.json`, `es-navigation.json`, `fr-navigation.json`, `id-navigation.json`, `it-navigation.json`, `ja-navigation.json`, `ko-navigation.json`, `pl-navigation.json`, `pt-BR-navigation.json`, and `tr-navigation.json` — starter locale metadata kept alongside the source repo, but the publish sync now clones the full English nav tree for clone-en locales so translated pages are visible in Mintlify without hand-maintaining per-locale nav JSON.
43+
- `zh-Hans-navigation.json` — curated zh-Hans tab and group labels overlaid onto the current English navigation tree during publish sync.
44+
- `ar-navigation.json`, `de-navigation.json`, `es-navigation.json`, `fr-navigation.json`, `id-navigation.json`, `it-navigation.json`, `ja-navigation.json`, `ko-navigation.json`, `pl-navigation.json`, `pt-BR-navigation.json`, and `tr-navigation.json` — starter locale labels kept alongside the source repo. Publish sync clones the full English navigation tree, prefixes locale routes, and overlays translated labels by matching shared page anchors.
4545
- `<lang>.tm.jsonl` — translation memory keyed by workflow + model + text hash.
4646

4747
In this repo, generated locale TM files such as `docs/.i18n/zh-CN.tm.jsonl`, `docs/.i18n/zh-TW.tm.jsonl`, `docs/.i18n/ja-JP.tm.jsonl`, `docs/.i18n/es.tm.jsonl`, `docs/.i18n/pt-BR.tm.jsonl`, `docs/.i18n/ko.tm.jsonl`, `docs/.i18n/de.tm.jsonl`, `docs/.i18n/fr.tm.jsonl`, `docs/.i18n/ar.tm.jsonl`, `docs/.i18n/it.tm.jsonl`, `docs/.i18n/vi.tm.jsonl`, `docs/.i18n/nl.tm.jsonl`, `docs/.i18n/fa.tm.jsonl`, `docs/.i18n/tr.tm.jsonl`, `docs/.i18n/uk.tm.jsonl`, `docs/.i18n/id.tm.jsonl`, `docs/.i18n/pl.tm.jsonl`, and `docs/.i18n/th.tm.jsonl` are intentionally no longer committed.

‎scripts/docs-sync-publish.d.mts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ export function parseArgs(argv: unknown): {
1313
export function resolveClawHubRepoPath(value?: string, options?: Record<string, unknown>): string;
1414
/** Reports locale pages whose canonical source page no longer exists without deleting them. */
1515
export function reportOrphanLocaleDocs(targetDocsDir: string): number;
16+
/** Applies translated tab and group labels without replacing canonical page routes. */
17+
export function applyLocaleNavLabelOverlay(
18+
fullNav: Record<string, unknown>,
19+
labelOverlay: Record<string, unknown>,
20+
): Record<string, unknown>;
21+
/** Composes the publish docs configuration with generated locale navigation. */
22+
export function composeDocsConfig(): Record<string, unknown>;
1623
/**
1724
* Mirrors ClawHub docs into the target docs tree.
1825
*/

‎scripts/docs-sync-publish.mjs‎

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,101 @@ function cloneEnglishLanguageNav(englishNav, locale) {
400400
};
401401
}
402402

403+
function collectNavPages(entry, pages = new Set()) {
404+
if (typeof entry === "string") {
405+
pages.add(entry);
406+
return pages;
407+
}
408+
if (Array.isArray(entry)) {
409+
for (const item of entry) {
410+
collectNavPages(item, pages);
411+
}
412+
return pages;
413+
}
414+
if (!entry || typeof entry !== "object") {
415+
return pages;
416+
}
417+
if (typeof entry.page === "string") {
418+
pages.add(entry.page);
419+
}
420+
collectNavPages(entry.pages, pages);
421+
collectNavPages(entry.groups, pages);
422+
collectNavPages(entry.tabs, pages);
423+
return pages;
424+
}
425+
426+
function findBestNavMatchIndex(candidates, overlayEntry, excludedIndexes = new Set()) {
427+
const overlayPages = collectNavPages(overlayEntry);
428+
let bestIndex = -1;
429+
let bestScore = 0;
430+
for (const [index, candidate] of candidates.entries()) {
431+
if (excludedIndexes.has(index)) {
432+
continue;
433+
}
434+
const candidatePages = collectNavPages(candidate);
435+
let score = 0;
436+
for (const page of overlayPages) {
437+
if (candidatePages.has(page)) {
438+
score += 1;
439+
}
440+
}
441+
if (score > bestScore) {
442+
bestIndex = index;
443+
bestScore = score;
444+
}
445+
}
446+
return bestIndex;
447+
}
448+
449+
export function applyLocaleNavLabelOverlay(fullNav, labelOverlay) {
450+
const tabs = Array.isArray(fullNav.tabs)
451+
? fullNav.tabs.map((tab) => ({
452+
...tab,
453+
groups: Array.isArray(tab.groups) ? tab.groups.map((group) => ({ ...group })) : tab.groups,
454+
}))
455+
: fullNav.tabs;
456+
const composed = { ...fullNav, tabs };
457+
if (!Array.isArray(tabs) || !Array.isArray(labelOverlay?.tabs)) {
458+
return composed;
459+
}
460+
461+
for (const overlayTab of labelOverlay.tabs) {
462+
const tabIndex = findBestNavMatchIndex(tabs, overlayTab);
463+
if (tabIndex < 0) {
464+
continue;
465+
}
466+
const tab = tabs[tabIndex];
467+
if (typeof overlayTab.tab === "string") {
468+
tab.tab = overlayTab.tab;
469+
}
470+
if (!Array.isArray(tab.groups) || !Array.isArray(overlayTab.groups)) {
471+
continue;
472+
}
473+
const matchedGroupIndexes = new Set();
474+
for (const overlayGroup of overlayTab.groups) {
475+
const groupIndex = findBestNavMatchIndex(tab.groups, overlayGroup, matchedGroupIndexes);
476+
if (groupIndex >= 0 && typeof overlayGroup.group === "string") {
477+
tab.groups[groupIndex].group = overlayGroup.group;
478+
matchedGroupIndexes.add(groupIndex);
479+
}
480+
}
481+
}
482+
return composed;
483+
}
484+
403485
function composeLocaleNav(locale, englishNav) {
404-
if (locale.navMode === "clone-en") {
405-
return cloneEnglishLanguageNav(englishNav, locale);
486+
const cloned = cloneEnglishLanguageNav(englishNav, locale);
487+
if (!locale.navFile) {
488+
return cloned;
489+
}
490+
const overlayPath = path.join(SOURCE_DOCS_DIR, ".i18n", locale.navFile);
491+
if (!fs.existsSync(overlayPath)) {
492+
return cloned;
406493
}
407-
return readJson(path.join(SOURCE_DOCS_DIR, ".i18n", locale.navFile));
494+
return applyLocaleNavLabelOverlay(cloned, readJson(overlayPath));
408495
}
409496

410-
function composeDocsConfig() {
497+
export function composeDocsConfig() {
411498
const sourceConfig = readJson(SOURCE_CONFIG_PATH);
412499
const languages = sourceConfig?.navigation?.languages;
413500

‎test/scripts/docs-sync-publish.test.ts‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,35 @@ import fs from "node:fs";
22
import os from "node:os";
33
import path from "node:path";
44
import { describe, expect, it } from "vitest";
5-
import { parseArgs, reportOrphanLocaleDocs } from "../../scripts/docs-sync-publish.mjs";
5+
import {
6+
composeDocsConfig,
7+
parseArgs,
8+
reportOrphanLocaleDocs,
9+
} from "../../scripts/docs-sync-publish.mjs";
10+
11+
function collectPages(entry: unknown, pages: string[] = []): string[] {
12+
if (typeof entry === "string") {
13+
pages.push(entry);
14+
return pages;
15+
}
16+
if (Array.isArray(entry)) {
17+
for (const item of entry) {
18+
collectPages(item, pages);
19+
}
20+
return pages;
21+
}
22+
if (!entry || typeof entry !== "object") {
23+
return pages;
24+
}
25+
const record = entry as Record<string, unknown>;
26+
if (typeof record.page === "string") {
27+
pages.push(record.page);
28+
}
29+
collectPages(record.pages, pages);
30+
collectPages(record.groups, pages);
31+
collectPages(record.tabs, pages);
32+
return pages;
33+
}
634

735
describe("docs-sync-publish", () => {
836
it("parses docs sync provenance args", () => {
@@ -68,4 +96,39 @@ describe("docs-sync-publish", () => {
6896
fs.rmSync(docsDir, { recursive: true, force: true });
6997
}
7098
});
99+
100+
it("keeps generated locale navigation aligned with English routes", () => {
101+
const config = composeDocsConfig() as {
102+
navigation: {
103+
languages: Array<{
104+
language: string;
105+
tabs: Array<{ tab: string; groups?: Array<{ group: string }> }>;
106+
}>;
107+
};
108+
};
109+
const english = config.navigation.languages.find((entry) => entry.language === "en");
110+
const simplifiedChinese = config.navigation.languages.find(
111+
(entry) => entry.language === "zh-Hans",
112+
);
113+
const german = config.navigation.languages.find((entry) => entry.language === "de");
114+
115+
expect(english).toBeDefined();
116+
expect(simplifiedChinese).toBeDefined();
117+
expect(german).toBeDefined();
118+
119+
const englishWithoutClawHub = {
120+
...english,
121+
tabs: english!.tabs.filter((tab) => tab.tab !== "ClawHub"),
122+
};
123+
const expectedZhPages = collectPages(englishWithoutClawHub)
124+
.map((page) => `zh-CN/${page}`)
125+
.toSorted();
126+
expect(collectPages(simplifiedChinese).toSorted()).toEqual(expectedZhPages);
127+
expect(simplifiedChinese!.tabs[0]?.tab).toBe("快速开始");
128+
expect(simplifiedChinese!.tabs[0]?.groups?.[0]?.group).toBe("首页");
129+
130+
expect(collectPages(german)).toHaveLength(collectPages(englishWithoutClawHub).length);
131+
expect(german!.tabs[0]?.tab).toBe("Loslegen");
132+
expect(german!.tabs[0]?.groups?.[0]?.group).toBe("Überblick");
133+
});
71134
});

0 commit comments

Comments
 (0)