|
1
|
import { readdirSync } from "fs" |
|
2
|
import path from "path" |
|
3
|
import { getFileSlugSync } from "docs-utils" |
|
4
|
|
|
5
|
type Options = { |
|
6
|
basePath: string |
|
7
|
} |
|
8
|
|
|
9
|
export function retrieveMdxPages({ basePath }: Options): string[] { |
|
10
|
function retrieveMdxFilesInPath(dir: string): string[] { |
|
11
|
const urls = [] |
|
12
|
const files = readdirSync(dir, { |
|
13
|
withFileTypes: true, |
|
14
|
}) |
|
15
|
|
|
16
|
for (const file of files) { |
|
17
|
const filePath = path.join(dir, file.name) |
|
18
|
if (file.isDirectory()) { |
|
19
|
if (!file.name.startsWith("_")) { |
|
20
|
urls.push(...retrieveMdxFilesInPath(filePath)) |
|
21
|
} |
|
22
|
continue |
|
23
|
} else if (file.name !== "page.mdx") { |
|
24
|
continue |
|
25
|
} |
|
26
|
|
|
27
|
const slug = getFileSlugSync(filePath) |
|
28
|
|
|
29
|
const url = (slug || filePath.replace(basePath, "")) |
|
30
|
.replace(file.name, "") |
|
31
|
.replace(/\/$/, "") |
|
32
|
|
|
33
|
urls.push(url) |
|
34
|
} |
|
35
|
|
|
36
|
return urls |
|
37
|
} |
|
38
|
|
|
39
|
return retrieveMdxFilesInPath(basePath) |
|
40
|
} |