-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathMdxFile.res
More file actions
43 lines (39 loc) · 1.21 KB
/
MdxFile.res
File metadata and controls
43 lines (39 loc) · 1.21 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
type fileData = {
content: string,
frontmatter: JSON.t,
}
let resolveFilePath = (pathname, ~dir, ~alias) => {
let path = if pathname->String.startsWith("/") {
pathname->String.slice(~start=1, ~end=String.length(pathname))
} else {
pathname
}
let relativePath = path->String.replace(alias, dir)
relativePath ++ ".mdx"
}
let loadFile = async filePath => {
let raw = await Node.Fs.readFile(filePath, "utf-8")
let {frontmatter, content}: MarkdownParser.result = MarkdownParser.parseSync(raw)
{content, frontmatter}
}
// Recursively scan a directory for .mdx files
let rec scanDir = (baseDir, currentDir) => {
let entries = Node.Fs.readdirSync(currentDir)
entries->Array.flatMap(entry => {
let fullPath = Node.Path.join2(currentDir, entry)
if Node.Fs.statSync(fullPath)["isDirectory"]() {
scanDir(baseDir, fullPath)
} else if Node.Path.extname(entry) === ".mdx" {
// Get the relative path from baseDir
let relativePath = fullPath->String.replace(baseDir ++ "/", "")->String.replace(".mdx", "")
[relativePath]
} else {
[]
}
})
}
let scanPaths = (~dir, ~alias) => {
scanDir(dir, dir)->Array.map(relativePath => {
alias ++ "/" ++ relativePath
})
}