-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestructure.mjs
More file actions
197 lines (166 loc) · 6.77 KB
/
Copy pathrestructure.mjs
File metadata and controls
197 lines (166 loc) · 6.77 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Restructure help articles from flat help/article/ into grouped subdirectories
* based on docs.json navigation, so Nextra creates collapsible sidebar groups.
*/
import fs from 'fs'
import path from 'path'
const SRC = '../dub-docs-mintlify'
const APP = './app'
const docsJson = JSON.parse(fs.readFileSync(path.join(SRC, 'docs.json'), 'utf-8'))
// Find Help Center tab
const helpTab = docsJson.navigation.tabs.find(t => t.tab === 'Help Center')
function slugify(str) {
return str.trim().toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '')
}
function extractTitle(filePath) {
if (!fs.existsSync(filePath)) return null
const content = fs.readFileSync(filePath, 'utf-8')
const m = content.match(/sidebarTitle:\s*["']?(.*?)["']?\s*$/m)
|| content.match(/title:\s*["']?(.*?)["']?\s*$/m)
return m ? m[1] : null
}
// First, clean out old help/article structure
const oldArticleDir = path.join(APP, 'help', 'article')
// Collect all existing article files before restructuring
const existingArticles = new Map()
if (fs.existsSync(oldArticleDir)) {
for (const entry of fs.readdirSync(oldArticleDir, { withFileTypes: true })) {
if (entry.isDirectory()) {
const pagePath = path.join(oldArticleDir, entry.name, 'page.mdx')
if (fs.existsSync(pagePath)) {
existingArticles.set(entry.name, fs.readFileSync(pagePath, 'utf-8'))
}
}
}
}
console.log(`Found ${existingArticles.size} existing articles`)
// Remove old article directory
fs.rmSync(oldArticleDir, { recursive: true, force: true })
// Process Help Center groups
function processGroup(group, parentDir, metaEntries) {
if (group.group && group.group.trim() === '') return // Skip empty group (root "help" page)
const pages = group.pages || []
for (const page of pages) {
if (typeof page === 'string') {
// Direct page reference
const slug = page.split('/').pop()
// Skip non-help articles (like docs/partners/embedded-referrals)
if (!page.startsWith('help/')) continue
const destDir = path.join(parentDir, slug)
fs.mkdirSync(destDir, { recursive: true })
if (existingArticles.has(slug)) {
fs.writeFileSync(path.join(destDir, 'page.mdx'), existingArticles.get(slug))
const title = extractTitle(path.join(destDir, 'page.mdx'))
metaEntries[slug] = title || slug
} else {
console.warn(` MISSING: ${slug}`)
}
} else if (typeof page === 'object' && page.group) {
// Nested group — create a subdirectory
const groupSlug = slugify(page.group)
const groupDir = path.join(parentDir, groupSlug)
fs.mkdirSync(groupDir, { recursive: true })
const subMeta = {}
processGroup(page, groupDir, subMeta)
// Write _meta.js for this subgroup
if (Object.keys(subMeta).length > 0) {
fs.writeFileSync(
path.join(groupDir, '_meta.js'),
`export default ${JSON.stringify(subMeta, null, 2)}\n`
)
}
// Add group to parent meta
metaEntries[groupSlug] = page.group
}
}
}
// Process each top-level group in Help Center
const helpDir = path.join(APP, 'help')
const helpMeta = { index: 'Help Center' }
for (const group of helpTab.groups) {
if (group.group && group.group.trim() === '') continue // Skip root
const groupName = group.group
const groupSlug = slugify(groupName)
const groupDir = path.join(helpDir, groupSlug)
fs.mkdirSync(groupDir, { recursive: true })
const groupMeta = {}
// Process pages in this top-level group
for (const page of group.pages) {
if (typeof page === 'string') {
const slug = page.split('/').pop()
if (!page.startsWith('help/')) continue
const destDir = path.join(groupDir, slug)
fs.mkdirSync(destDir, { recursive: true })
if (existingArticles.has(slug)) {
fs.writeFileSync(path.join(destDir, 'page.mdx'), existingArticles.get(slug))
const title = extractTitle(path.join(destDir, 'page.mdx'))
groupMeta[slug] = title || slug
} else {
console.warn(` MISSING: ${slug}`)
}
} else if (typeof page === 'object' && page.group) {
const subGroupSlug = slugify(page.group)
const subGroupDir = path.join(groupDir, subGroupSlug)
fs.mkdirSync(subGroupDir, { recursive: true })
const subMeta = {}
for (const subPage of (page.pages || [])) {
if (typeof subPage === 'string') {
const slug = subPage.split('/').pop()
if (!subPage.startsWith('help/')) continue
const destDir = path.join(subGroupDir, slug)
fs.mkdirSync(destDir, { recursive: true })
if (existingArticles.has(slug)) {
fs.writeFileSync(path.join(destDir, 'page.mdx'), existingArticles.get(slug))
const title = extractTitle(path.join(destDir, 'page.mdx'))
subMeta[slug] = title || slug
} else {
console.warn(` MISSING: ${slug}`)
}
} else if (typeof subPage === 'object' && subPage.group) {
// 3rd level nesting (e.g. "Filtering analytics")
const subSubSlug = slugify(subPage.group)
const subSubDir = path.join(subGroupDir, subSubSlug)
fs.mkdirSync(subSubDir, { recursive: true })
const subSubMeta = {}
for (const ssPage of (subPage.pages || [])) {
if (typeof ssPage === 'string') {
const slug = ssPage.split('/').pop()
if (!ssPage.startsWith('help/')) continue
const destDir = path.join(subSubDir, slug)
fs.mkdirSync(destDir, { recursive: true })
if (existingArticles.has(slug)) {
fs.writeFileSync(path.join(destDir, 'page.mdx'), existingArticles.get(slug))
const title = extractTitle(path.join(destDir, 'page.mdx'))
subSubMeta[slug] = title || slug
}
}
}
if (Object.keys(subSubMeta).length > 0) {
fs.writeFileSync(path.join(subSubDir, '_meta.js'), `export default ${JSON.stringify(subSubMeta, null, 2)}\n`)
}
subMeta[subSubSlug] = subPage.group
}
}
if (Object.keys(subMeta).length > 0) {
fs.writeFileSync(path.join(subGroupDir, '_meta.js'), `export default ${JSON.stringify(subMeta, null, 2)}\n`)
}
groupMeta[subGroupSlug] = page.group
}
}
// Write _meta.js for this top-level group
if (Object.keys(groupMeta).length > 0) {
fs.writeFileSync(
path.join(groupDir, '_meta.js'),
`export default ${JSON.stringify(groupMeta, null, 2)}\n`
)
}
helpMeta[groupSlug] = groupName
}
// Write help/_meta.js
fs.writeFileSync(
path.join(helpDir, '_meta.js'),
`export default ${JSON.stringify(helpMeta, null, 2)}\n`
)
console.log('\nGenerated structure:')
console.log(JSON.stringify(helpMeta, null, 2))
console.log('\n✓ Restructuring complete!')