-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathorganize-api-docs.ts
133 lines (100 loc) · 3.54 KB
/
organize-api-docs.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import fs from 'fs'
import path from 'path'
/**
* Interface for API documentation item
*/
interface ApiDoc {
title: string
slug: string
group: string
label?: string
}
/**
* Script to organize API documentation into subfolders by admin group
* and update frontmatter to use label for title
*/
const organizeApiDocs = async () => {
try {
console.log('Organizing API documentation into subfolders by admin group...')
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[]> = {}
const fileContents: Record<string, string> = {}
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 labelMatch = content.match(/label:\s*([^\n]+)/)
const groupMatch = content.match(/group:\s*([^\n]+)/)
const slug = path.basename(file, '.mdx')
const title = titleMatch ? titleMatch[1].trim() : slug
const label = labelMatch ? labelMatch[1].trim() : title
const group = groupMatch ? groupMatch[1].trim() : 'Uncategorized'
if (!apisByGroup[group]) {
apisByGroup[group] = []
}
apisByGroup[group].push({
title,
slug,
group,
label,
})
fileContents[slug] = content
}
for (const group of Object.keys(apisByGroup)) {
const groupDir = path.join(apisDir, group.toLowerCase().replace(/\s+/g, '-'))
if (!fs.existsSync(groupDir)) {
fs.mkdirSync(groupDir, { recursive: true })
}
for (const api of apisByGroup[group]) {
const oldPath = path.join(apisDir, `${api.slug}.mdx`)
const newPath = path.join(groupDir, `${api.slug}.mdx`)
let content = fileContents[api.slug]
if (api.label && api.label !== api.title) {
content = content.replace(/title:\s*([^\n]+)/, `title: ${api.label}`)
}
fs.writeFileSync(newPath, content)
if (fs.existsSync(oldPath)) {
fs.unlinkSync(oldPath)
}
console.log(`Moved ${api.slug} to ${group} group directory`)
}
}
updateIndexFile(apisDir, apisByGroup)
console.log('API documentation organization complete')
} catch (error) {
console.error('Error organizing API documentation:', error)
process.exit(1)
}
}
/**
* Updates the index.mdx file with links to files in group directories
*/
const updateIndexFile = (apisDir: string, apisByGroup: Record<string, ApiDoc[]>) => {
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) {
const groupPath = group.toLowerCase().replace(/\s+/g, '-')
indexContent += `- [${api.title}](/${groupPath}/${api.slug})\n`
}
indexContent += '\n'
}
const indexPath = path.join(apisDir, 'index.mdx')
fs.writeFileSync(indexPath, indexContent)
console.log('Updated index.mdx with links to organized API documentation')
}
organizeApiDocs()