| 176 | } |
| 177 | |
| 178 | export function buildDocTree(files: { relativePath: string }[]): DocSection[] { |
| 179 | const sections: Map<string, DocSection> = new Map() |
| 180 | |
| 181 | for (const file of files) { |
| 182 | const parts = file.relativePath.split(/[/\\]/) |
| 183 | if (parts.length < 2) continue |
| 184 | |
| 185 | const topLevelDir = parts[0] |
| 186 | |
| 187 | if (!sections.has(topLevelDir)) { |
| 188 | sections.set(topLevelDir, { |
| 189 | name: topLevelDir, |
| 190 | files: [], |
| 191 | subsections: [], |
| 192 | }) |
| 193 | } |
| 194 | |
| 195 | const section = sections.get(topLevelDir)! |
| 196 | |
| 197 | if (parts.length === 2) { |
| 198 | section.files.push({ relativePath: file.relativePath }) |
| 199 | } else { |
| 200 | const subsectionDir = parts[1] |
| 201 | let subsection = section.subsections.find((s) => s.name === subsectionDir) |
| 202 | |
| 203 | if (!subsection) { |
| 204 | subsection = { name: subsectionDir, files: [], subsections: [] } |
| 205 | section.subsections.push(subsection) |
| 206 | } |
| 207 | |
| 208 | if (parts.length === 3) { |
| 209 | subsection.files.push({ relativePath: file.relativePath }) |
| 210 | } else { |
| 211 | const subSubDir = parts[2] |
| 212 | let subSubsection = subsection.subsections.find( |
| 213 | (s) => s.name === subSubDir |
| 214 | ) |
| 215 | |
| 216 | if (!subSubsection) { |
| 217 | subSubsection = { name: subSubDir, files: [], subsections: [] } |
| 218 | subsection.subsections.push(subSubsection) |
| 219 | } |
| 220 | |
| 221 | subSubsection.files.push({ relativePath: file.relativePath }) |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | const sortedSections = Array.from(sections.values()).sort((a, b) => |
| 227 | a.name.localeCompare(b.name) |
| 228 | ) |
| 229 | |
| 230 | for (const section of sortedSections) { |
| 231 | section.files.sort((a, b) => a.relativePath.localeCompare(b.relativePath)) |
| 232 | section.subsections.sort((a, b) => a.name.localeCompare(b.name)) |
| 233 | for (const subsection of section.subsections) { |
| 234 | subsection.files.sort((a, b) => |
| 235 | a.relativePath.localeCompare(b.relativePath) |