|
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import { fileURLToPath } from 'node:url'; |
| 3 | +import { dirname, resolve } from 'node:path'; |
| 4 | +import type { DefaultTheme } from 'vitepress'; |
| 5 | + |
| 6 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 7 | +const SUMMARY_PATH = resolve(__dirname, '../gitbook/SUMMARY.md'); |
| 8 | + |
| 9 | +interface RawItem { |
| 10 | + text: string; |
| 11 | + target: string; |
| 12 | + depth: number; |
| 13 | + children: RawItem[]; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Converts a GitBook SUMMARY link target into a VitePress link. |
| 18 | + * |
| 19 | + * External URLs are returned untouched. Local `.md` targets are turned into |
| 20 | + * clean, extension-less absolute links. `README.md` files map to their |
| 21 | + * containing directory to match the `rewrites` configured in `config.mts`. |
| 22 | + */ |
| 23 | +function toLink(target: string): string { |
| 24 | + const cleaned = target.replace(/^<(.*)>$/, '$1').trim(); |
| 25 | + if (/^https?:\/\//.test(cleaned)) { |
| 26 | + return cleaned; |
| 27 | + } |
| 28 | + |
| 29 | + let link = cleaned.replace(/\.md$/i, ''); |
| 30 | + link = link.replace(/(^|\/)README$/i, '$1'); |
| 31 | + if (!link.startsWith('/')) { |
| 32 | + link = `/${link}`; |
| 33 | + } |
| 34 | + return link; |
| 35 | +} |
| 36 | + |
| 37 | +const LINK_RE = /^(\s*)-\s*\[([^\]]+)\]\(([^)]+)\)\s*$/; |
| 38 | + |
| 39 | +/** |
| 40 | + * Parses the GitBook `SUMMARY.md` table of contents into VitePress `nav` and |
| 41 | + * `sidebar` structures, so the documentation navigation stays in sync with a |
| 42 | + * single source of truth. |
| 43 | + */ |
| 44 | +export function loadNavigation(): { |
| 45 | + nav: DefaultTheme.NavItem[]; |
| 46 | + sidebar: DefaultTheme.SidebarItem[]; |
| 47 | +} { |
| 48 | + const content = readFileSync(SUMMARY_PATH, 'utf-8'); |
| 49 | + const lines = content.split('\n'); |
| 50 | + |
| 51 | + const sections: { title: string; items: RawItem[] }[] = []; |
| 52 | + let current: { title: string; items: RawItem[] } = { |
| 53 | + title: 'Overview', |
| 54 | + items: [], |
| 55 | + }; |
| 56 | + sections.push(current); |
| 57 | + |
| 58 | + for (const line of lines) { |
| 59 | + const sectionMatch = /^##\s+(.+?)\s*$/.exec(line); |
| 60 | + if (sectionMatch) { |
| 61 | + current = { title: sectionMatch[1], items: [] }; |
| 62 | + sections.push(current); |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + const linkMatch = LINK_RE.exec(line); |
| 67 | + if (!linkMatch) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + const [, indent, text, target] = linkMatch; |
| 72 | + const depth = Math.floor(indent.length / 2); |
| 73 | + const item: RawItem = { text, target, depth, children: [] }; |
| 74 | + |
| 75 | + // Attach to the last item at depth - 1, otherwise it is a top-level item. |
| 76 | + let parent: RawItem | undefined; |
| 77 | + const stack = current.items; |
| 78 | + if (depth > 0) { |
| 79 | + let candidates: RawItem[] = stack; |
| 80 | + for (let level = 0; level < depth - 1; level++) { |
| 81 | + // Descend into the most recently added item's children. If it has none |
| 82 | + // yet (malformed indentation), keep the current level as a fallback. |
| 83 | + candidates = candidates[candidates.length - 1]?.children ?? candidates; |
| 84 | + } |
| 85 | + parent = candidates[candidates.length - 1]; |
| 86 | + } |
| 87 | + |
| 88 | + if (parent) { |
| 89 | + parent.children.push(item); |
| 90 | + } else { |
| 91 | + current.items.push(item); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + const toSidebarItem = (item: RawItem): DefaultTheme.SidebarItem => { |
| 96 | + const node: DefaultTheme.SidebarItem = { text: item.text }; |
| 97 | + node.link = toLink(item.target); |
| 98 | + if (item.children.length > 0) { |
| 99 | + node.collapsed = true; |
| 100 | + node.items = item.children.map(toSidebarItem); |
| 101 | + } |
| 102 | + return node; |
| 103 | + }; |
| 104 | + |
| 105 | + const sidebar: DefaultTheme.SidebarItem[] = sections |
| 106 | + .filter(section => section.items.length > 0) |
| 107 | + .map(section => ({ |
| 108 | + text: section.title, |
| 109 | + collapsed: section.title !== 'Overview' && section.title !== 'Guide', |
| 110 | + items: section.items.map(toSidebarItem), |
| 111 | + })); |
| 112 | + |
| 113 | + // Build a compact top navigation from the main sections. |
| 114 | + const sectionLink = (title: string): string | undefined => { |
| 115 | + const section = sections.find(s => s.title === title); |
| 116 | + const first = section?.items.find(i => !/^https?:\/\//.test(i.target)); |
| 117 | + return first ? toLink(first.target) : undefined; |
| 118 | + }; |
| 119 | + |
| 120 | + const nav: DefaultTheme.NavItem[] = []; |
| 121 | + const guideLink = sectionLink('Guide'); |
| 122 | + if (guideLink) { |
| 123 | + nav.push({ text: 'Guide', link: guideLink }); |
| 124 | + } |
| 125 | + const patternsLink = sectionLink('Patterns'); |
| 126 | + if (patternsLink) { |
| 127 | + nav.push({ text: 'Patterns', link: patternsLink }); |
| 128 | + } |
| 129 | + |
| 130 | + const languageDropdown: DefaultTheme.NavItemWithChildren = { |
| 131 | + text: 'Bindings', |
| 132 | + items: [], |
| 133 | + }; |
| 134 | + for (const title of ['Python', 'Rust', 'Elixir', 'PHP']) { |
| 135 | + const link = sectionLink(title); |
| 136 | + if (link) { |
| 137 | + languageDropdown.items.push({ text: title, link }); |
| 138 | + } |
| 139 | + } |
| 140 | + if (languageDropdown.items.length > 0) { |
| 141 | + nav.push(languageDropdown); |
| 142 | + } |
| 143 | + |
| 144 | + const proLink = sectionLink('BullMQ Pro'); |
| 145 | + if (proLink) { |
| 146 | + nav.push({ text: 'Pro', link: proLink }); |
| 147 | + } |
| 148 | + |
| 149 | + nav.push({ text: 'API Reference', link: 'https://api.docs.bullmq.io' }); |
| 150 | + |
| 151 | + return { nav, sidebar }; |
| 152 | +} |
0 commit comments