|
| 1 | +import { existsSync, readdirSync } from 'node:fs'; |
| 2 | +import { join, parse } from 'node:path'; |
| 3 | + |
1 | 4 | import type { DefaultTheme } from 'vitepress'; |
2 | 5 |
|
3 | 6 | export const guide: DefaultTheme.NavItem[] = [ |
@@ -100,17 +103,48 @@ export const guide: DefaultTheme.NavItem[] = [ |
100 | 103 | export const reference: DefaultTheme.NavItem[] = [ |
101 | 104 | { |
102 | 105 | text: 'API Reference', |
103 | | - items: [ |
104 | | - { text: '@blocksuite/store', link: 'api/@blocksuite/store/index' }, |
105 | | - { |
106 | | - text: '@blocksuite/block-std', |
107 | | - link: 'api/@blocksuite/block-std/index', |
108 | | - }, |
109 | | - { text: '@blocksuite/inline', link: 'api/@blocksuite/inline/index' }, |
110 | | - ], |
| 106 | + items: getApiReferenceItems(), |
111 | 107 | }, |
112 | 108 | ]; |
113 | 109 |
|
| 110 | +function getApiReferenceItems(): DefaultTheme.NavItem[] { |
| 111 | + const apiDir = join(process.cwd(), 'api', '@blocksuite'); |
| 112 | + |
| 113 | + if (!existsSync(apiDir)) { |
| 114 | + return [ |
| 115 | + { text: '@blocksuite/store', link: 'api/@blocksuite/store' }, |
| 116 | + { text: '@blocksuite/std', link: 'api/@blocksuite/std/index' }, |
| 117 | + { text: '@blocksuite/affine', link: 'api/@blocksuite/affine' }, |
| 118 | + ]; |
| 119 | + } |
| 120 | + |
| 121 | + return readdirSync(apiDir, { withFileTypes: true }) |
| 122 | + .flatMap(entry => { |
| 123 | + if (entry.isFile() && entry.name.endsWith('.md')) { |
| 124 | + const name = parse(entry.name).name; |
| 125 | + return [ |
| 126 | + { text: `@blocksuite/${name}`, link: `api/@blocksuite/${name}` }, |
| 127 | + ]; |
| 128 | + } |
| 129 | + |
| 130 | + if (entry.isDirectory()) { |
| 131 | + const indexPath = join(apiDir, entry.name, 'index.md'); |
| 132 | + |
| 133 | + if (existsSync(indexPath)) { |
| 134 | + return [ |
| 135 | + { |
| 136 | + text: `@blocksuite/${entry.name}`, |
| 137 | + link: `api/@blocksuite/${entry.name}/index`, |
| 138 | + }, |
| 139 | + ]; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return []; |
| 144 | + }) |
| 145 | + .sort((a, b) => a.text.localeCompare(b.text)); |
| 146 | +} |
| 147 | + |
114 | 148 | export const components: DefaultTheme.NavItem[] = [ |
115 | 149 | { |
116 | 150 | text: 'Introduction', |
|
0 commit comments