UNPKG

@mintlify/common

Version:

Commonly shared code within Mintlify

36 lines (35 loc) 1.52 kB
import { divisions, } from '@mintlify/validation'; import { optionallyRemoveLeadingSlash } from '../optionallyRemoveLeadingSlash.js'; export function generatePathToLanguageDict(nav) { const pathToLanguageDict = new Map(); traverseNavigation(pathToLanguageDict, nav, undefined); return pathToLanguageDict; } function traverseNavigation(pathToLanguageDict, nav, nearestLanguage) { const currentLanguage = 'language' in nav ? nav.language : nearestLanguage; if ('pages' in nav) { nav.pages.forEach((page) => traverseGroup(pathToLanguageDict, page, currentLanguage)); return; } for (const division of ['groups', ...divisions]) { if (division in nav && Array.isArray(nav[division])) { const items = nav[division]; items.forEach((item) => traverseNavigation(pathToLanguageDict, item, currentLanguage)); } } } function traverseGroup(pathToLanguageDict, entry, nearestLanguage) { if ('pages' in entry) { entry.pages.forEach((page) => traverseGroup(pathToLanguageDict, page, nearestLanguage)); } else if ('href' in entry) { const key = optionallyRemoveLeadingSlash(entry.href); const existingLanguage = pathToLanguageDict.get(key); if (!pathToLanguageDict.has(key)) { pathToLanguageDict.set(key, nearestLanguage); } else if (existingLanguage !== nearestLanguage && existingLanguage !== undefined) { pathToLanguageDict.set(key, undefined); } } }