-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate-api-docs-index.ts
86 lines (64 loc) · 2.13 KB
/
update-api-docs-index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import fs from 'fs'
import path from 'path'
/**
* Interface for API documentation item
*/
interface ApiDoc {
title: string
slug: string
}
/**
* Script to update the API documentation index file
* Organizes API documentation links by Admin group
*/
const updateApiDocsIndex = async () => {
try {
console.log('Updating API documentation index...')
const apisDir = path.resolve(process.cwd(), 'content/apis')
if (!fs.existsSync(apisDir)) {
console.error('APIs directory does not exist')
process.exit(1)
}
const files = fs.readdirSync(apisDir).filter((file) => file.endsWith('.mdx') && file !== 'index.mdx')
const apisByGroup: Record<string, ApiDoc[]> = {}
for (const file of files) {
const filePath = path.join(apisDir, file)
const content = fs.readFileSync(filePath, 'utf-8')
const titleMatch = content.match(/title:\s*([^\n]+)/)
const groupMatch = content.match(/group:\s*([^\n]+)/)
const title = titleMatch ? titleMatch[1].trim() : path.basename(file, '.mdx')
const group = groupMatch ? groupMatch[1].trim() : 'Uncategorized'
if (!apisByGroup[group]) {
apisByGroup[group] = []
}
apisByGroup[group].push({
title,
slug: path.basename(file, '.mdx'),
})
}
const sortedGroups = Object.keys(apisByGroup).sort()
let indexContent = `---
title: API Reference
sidebarTitle: APIs
asIndexPage: true
---
# API Reference
This section contains API documentation for all collections in the system, organized by category.
`
for (const group of sortedGroups) {
indexContent += `## ${group}\n\n`
const sortedApis = apisByGroup[group].sort((a, b) => a.title.localeCompare(b.title))
for (const api of sortedApis) {
indexContent += `- [${api.title}](/${api.slug})\n`
}
indexContent += '\n'
}
const indexPath = path.join(apisDir, 'index.mdx')
fs.writeFileSync(indexPath, indexContent)
console.log('API documentation index updated successfully')
} catch (error) {
console.error('Error updating API documentation index:', error)
process.exit(1)
}
}
updateApiDocsIndex()