Skip to content

Commit 21d2da3

Browse files
committed
docs: typedoc for vitepress
1 parent f6d79c6 commit 21d2da3

File tree

27 files changed

+116
-1784
lines changed

27 files changed

+116
-1784
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ temp
1010

1111
docs/.vitepress/dist
1212
docs/.vitepress/cache
13+
1314
docs/reference/api
15+
docs/zh-CN/reference/api

.vscode/settings.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
"editor.formatOnSave": true,
33
"cSpell.words": ["tsdown"],
44
"files.readonlyInclude": {
5-
"docs/**/config-options.md": true,
6-
"docs/**/workspace.md": true,
7-
"docs/**/type-aliases/*.md": true
5+
"docs/**/reference/api/**": true
86
},
97
"typescript.tsdk": "node_modules/typescript/lib"
108
}

docs/.vitepress/config/theme.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1+
import { existsSync } from 'node:fs'
2+
import { readFile } from 'node:fs/promises'
3+
import path from 'node:path'
14
import { createTranslate } from '../i18n/utils'
25
import type { DefaultTheme, HeadConfig, LocaleConfig } from 'vitepress'
36

7+
async function getTypedocSidebar() {
8+
const filepath = path.resolve(
9+
import.meta.dirname,
10+
'../../reference/api/typedoc-sidebar.json',
11+
)
12+
if (!existsSync(filepath)) return []
13+
14+
try {
15+
return JSON.parse(
16+
await readFile(filepath, 'utf8'),
17+
) as DefaultTheme.SidebarItem[]
18+
} catch {
19+
return []
20+
}
21+
}
22+
23+
const typedocSidebar = await getTypedocSidebar()
24+
425
export function getLocaleConfig(lang: string) {
526
const t = createTranslate(lang)
627

@@ -96,8 +117,18 @@ export function getLocaleConfig(lang: string) {
96117
text: t('API Reference'),
97118
base: `${urlPrefix}/reference`,
98119
items: [
99-
{ text: t('Config Options'), link: '/config-options.md' },
100120
{ text: t('Command Line Interface'), link: '/cli.md' },
121+
{
122+
text: t('Config Options'),
123+
link: '/api/Interface.Options.md',
124+
},
125+
{
126+
text: t('Type Definitions'),
127+
items: typedocSidebar
128+
.flatMap((i) => i.items!)
129+
.filter((i) => i.text !== 'Options'),
130+
collapsed: true,
131+
},
101132
],
102133
},
103134
]

docs/.vitepress/i18n/translate-map.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ export const zhCN = {
3939
Benchmark: '性能基准',
4040
FAQ: '常见问题',
4141

42-
'Config Options': '配置选项',
4342
'Command Line Interface': '命令行接口',
43+
'Config Options': '配置选项',
44+
'Type Definitions': '类型定义',
4445

4546
'The Elegant Bundler for Libraries': '优雅的库打包器',
4647

docs/.vitepress/scripts/docs-generate.ts

Lines changed: 13 additions & 241 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
import {
2-
copyFileSync,
3-
cpSync,
4-
existsSync,
5-
mkdirSync,
6-
readFileSync,
7-
rmSync,
8-
writeFileSync,
9-
} from 'node:fs'
10-
import { dirname } from 'node:path'
1+
import { cp, rm } from 'node:fs/promises'
112
import * as typedoc from 'typedoc'
123

4+
const LANGUAGES = ['zh-CN']
5+
136
/**
147
* Run TypeDoc with the specified tsconfig
158
*/
16-
async function runTypedoc(tsconfigPath: string): Promise<void> {
9+
async function runTypedoc(tsconfig: string): Promise<void> {
1710
// Bootstrap TypeDoc with plugins
1811
const app = await typedoc.Application.bootstrapWithPlugins({
19-
tsconfig: tsconfigPath,
12+
tsconfig,
2013
})
2114

2215
// May be undefined if errors are encountered.
@@ -30,199 +23,6 @@ async function runTypedoc(tsconfigPath: string): Promise<void> {
3023
}
3124
}
3225

33-
/**
34-
* Type definitions for file operations
35-
*/
36-
type FileOperation = {
37-
type: 'delete-lines' | 'replace' | 'prepend' | 'append'
38-
lineStart?: number
39-
lineEnd?: number
40-
pattern?: string | RegExp
41-
replacement?: string
42-
}
43-
44-
type FileMapping =
45-
| {
46-
type: 'file'
47-
source: string
48-
destination: string
49-
modifications?: FileOperation[]
50-
}
51-
| {
52-
type: 'folder'
53-
sourceDir: string
54-
destDir: string
55-
files: string[]
56-
modifications?: FileOperation[]
57-
}
58-
59-
/**
60-
* Class to handle file operations based on mapping configurations
61-
*/
62-
class FileMapper {
63-
private mappings: FileMapping[]
64-
65-
constructor(mappings: FileMapping[]) {
66-
this.mappings = mappings
67-
}
68-
69-
/**
70-
* Modify file content with various operations
71-
*/
72-
private modifyFile(filePath: string, operations: FileOperation[]): void {
73-
let content = readFileSync(filePath, 'utf-8')
74-
75-
for (const op of operations) {
76-
if (
77-
op.type === 'delete-lines' &&
78-
op.lineStart !== undefined &&
79-
op.lineEnd !== undefined
80-
) {
81-
const lines = content.split('\n')
82-
const newLines = [
83-
...lines.slice(0, op.lineStart - 1),
84-
...lines.slice(op.lineEnd),
85-
]
86-
content = newLines.join('\n')
87-
} else if (
88-
op.type === 'replace' &&
89-
op.pattern &&
90-
op.replacement !== undefined
91-
) {
92-
content = content.replaceAll(
93-
new RegExp(op.pattern, 'g'),
94-
op.replacement,
95-
)
96-
} else if (op.type === 'prepend' && op.replacement !== undefined) {
97-
content = `${op.replacement}\n${content}`
98-
} else if (op.type === 'append' && op.replacement !== undefined) {
99-
content = `${content}\n${op.replacement}\n`
100-
}
101-
}
102-
103-
writeFileSync(filePath, content)
104-
}
105-
106-
/**
107-
* Ensure a directory exists, creating it if necessary
108-
*/
109-
private ensureDir(dirPath: string): void {
110-
if (!existsSync(dirPath)) {
111-
mkdirSync(dirPath, { recursive: true })
112-
}
113-
}
114-
115-
/**
116-
* Remove a file or directory if it exists
117-
*/
118-
private remove(
119-
path: string,
120-
options = { recursive: true, force: true },
121-
): void {
122-
if (existsSync(path)) {
123-
rmSync(path, options)
124-
}
125-
}
126-
127-
/**
128-
* Move a file from source to destination
129-
* Creates destination directory if it doesn't exist
130-
*/
131-
private moveFile(source: string, destination: string): void {
132-
this.ensureDir(dirname(destination))
133-
copyFileSync(source, destination)
134-
}
135-
136-
/**
137-
* Copy a directory recursively
138-
*/
139-
private copyDir(source: string, destination: string): void {
140-
this.remove(destination)
141-
this.ensureDir(destination)
142-
cpSync(source, destination, { recursive: true })
143-
}
144-
145-
/**
146-
* Apply standard modifications to markdown files
147-
*/
148-
private standardizeMarkdown(
149-
filePath: string,
150-
additionalOps: FileOperation[] = [],
151-
): void {
152-
// Common operation: remove first 6 lines
153-
const operations: FileOperation[] = [
154-
{ type: 'delete-lines', lineStart: 1, lineEnd: 6 },
155-
{
156-
type: 'prepend',
157-
replacement: '<!-- prettier-ignore-start -->',
158-
},
159-
{
160-
type: 'append',
161-
replacement: '<!-- prettier-ignore-end -->',
162-
},
163-
...additionalOps,
164-
]
165-
this.modifyFile(filePath, operations)
166-
}
167-
168-
/**
169-
* Process a single file mapping
170-
*/
171-
private processFileMapping(mapping: FileMapping): void {
172-
if (mapping.type === 'file') {
173-
this.moveFile(mapping.source, mapping.destination)
174-
this.standardizeMarkdown(mapping.destination, mapping.modifications)
175-
} else if (mapping.type === 'folder') {
176-
this.ensureDir(mapping.destDir)
177-
for (const file of mapping.files) {
178-
const source = `${mapping.sourceDir}/${file}.md`
179-
const destination = `${mapping.destDir}/${file}.md`
180-
this.moveFile(source, destination)
181-
this.standardizeMarkdown(destination, mapping.modifications)
182-
}
183-
}
184-
}
185-
186-
/**
187-
* Copy all processed files to locale directories
188-
*/
189-
private copyToLocales(locales: string[]): void {
190-
for (const locale of locales) {
191-
const localeRefDir = `./docs/${locale}/reference`
192-
this.ensureDir(localeRefDir)
193-
194-
// Copy all files to each locale
195-
for (const mapping of this.mappings) {
196-
if (mapping.type === 'file') {
197-
const destFile = `${localeRefDir}/${mapping.destination.split('/').pop()}`
198-
this.moveFile(mapping.destination, destFile)
199-
} else if (mapping.type === 'folder') {
200-
const localeDestDir = `${localeRefDir}/${mapping.destDir.split('/').pop()}`
201-
this.copyDir(mapping.destDir, localeDestDir)
202-
}
203-
}
204-
}
205-
}
206-
207-
/**
208-
* Process all file mappings
209-
*/
210-
process(locales: string[] = []): void {
211-
// Process each mapping
212-
for (const mapping of this.mappings) {
213-
this.processFileMapping(mapping)
214-
}
215-
216-
// Remove the original api folder
217-
this.remove('./docs/reference/api')
218-
219-
// Copy to locales if specified
220-
if (locales.length > 0) {
221-
this.copyToLocales(locales)
222-
}
223-
}
224-
}
225-
22626
/**
22727
* Main function to generate API reference documentation
22828
*/
@@ -234,43 +34,15 @@ async function generateApiReference() {
23434
console.log('✅ Reference generated successfully!')
23535
console.log('📚 Beautifying reference structure...')
23636

237-
// Define file mappings
238-
const fileMappings: FileMapping[] = [
239-
{
240-
type: 'file',
241-
source: './docs/reference/api/interfaces/Options.md',
242-
destination: './docs/reference/config-options.md',
243-
modifications: [
244-
{
245-
type: 'replace',
246-
pattern: String.raw`\.\.\/type-aliases`,
247-
replacement: './type-aliases',
248-
},
249-
{
250-
type: 'replace',
251-
pattern: `Workspace.md`,
252-
replacement: 'workspace.md',
253-
},
254-
],
255-
},
256-
{
257-
type: 'file',
258-
source: './docs/reference/api/interfaces/Workspace.md',
259-
destination: './docs/reference/workspace.md',
260-
},
261-
{
262-
type: 'folder',
263-
sourceDir: './docs/reference/api/type-aliases',
264-
destDir: './docs/reference/type-aliases',
265-
files: ['Sourcemap', 'Format', 'ModuleTypes'],
266-
},
267-
]
37+
await rm('docs/reference/api/index.md', { force: true })
38+
await rm('docs/reference/api/_media', { recursive: true, force: true })
26839

269-
// Create a file mapper and process all mappings
270-
const fileMapper = new FileMapper(fileMappings)
271-
fileMapper.process(['zh-CN'])
272-
273-
console.log('✅ Reference structure beautified successfully!')
40+
for (const language of LANGUAGES) {
41+
await cp(`docs/reference/api`, `docs/${language}/reference/api`, {
42+
recursive: true,
43+
force: true,
44+
})
45+
}
27446
}
27547

27648
// Execute the main function

docs/advanced/programmatic-usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ await build({
1616
})
1717
```
1818

19-
All CLI options are available as properties in the options object. See [Config Options](../reference/config-options.md) for the full list.
19+
All CLI options are available as properties in the options object. See [Config Options](../reference/api/Interface.Options.md) for the full list.

docs/options/config-file.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,4 @@ tsdown --from-vite vitest # Load vitest.config.*
8181
8282
## Reference
8383

84-
For a full list of available configuration options, refer to the [Config Options Reference](../reference/config-options.md). This includes detailed explanations of all supported fields and their usage.
84+
For a full list of available configuration options, refer to the [Config Options Reference](../reference/api/Interface.Options.md). This includes detailed explanations of all supported fields and their usage.

0 commit comments

Comments
 (0)