diff --git a/generate-llms.ts b/generate-llms.ts index d779bf8..e197db7 100644 --- a/generate-llms.ts +++ b/generate-llms.ts @@ -101,40 +101,41 @@ const linkToSiteUrl = (link: string): string => /** * Развернуть sidebar в плоский список с сохранением группы и - * опционального префикса вложенной группы. + * накопленного префикса вложенных групп. Поддерживает произвольную + * глубину вложенности — префиксы подгрупп склеиваются через `: `. */ const flattenSidebar = (sidebar: SidebarItem[]): Entry[] => { const entries: Entry[] = []; - for (const top of sidebar) { - const section = top.text; + const walk = ( + items: SidebarItem[], + section: string, + prefix: string | null, + ): void => { + for (const item of items) { + const hasChildren = !!item.items && item.items.length > 0; - if (top.link && !top.items) { - entries.push({ section, prefix: null, text: top.text, link: top.link }); + if (item.link) { + entries.push({ section, prefix, text: item.text, link: item.link }); + } + + if (hasChildren) { + const nextPrefix = prefix ? `${prefix}: ${item.text}` : item.text; + walk(item.items!, section, nextPrefix); + } + } + }; + + for (const top of sidebar) { + const hasChildren = !!top.items && top.items.length > 0; + + if (top.link && !hasChildren) { + entries.push({ section: top.text, prefix: null, text: top.text, link: top.link }); continue; } - if (!top.items) continue; - - for (const item of top.items) { - if (item.items) { - for (const sub of item.items) { - if (!sub.link) continue; - entries.push({ - section, - prefix: item.text, - text: sub.text, - link: sub.link, - }); - } - } else if (item.link) { - entries.push({ - section, - prefix: null, - text: item.text, - link: item.link, - }); - } + if (hasChildren) { + walk(top.items!, top.text, null); } }