Repository navigation
Expand file tree
/
Copy pathMdxFile.res
More file actions
103 lines (92 loc) · 3.53 KB
/
Copy pathMdxFile.res
File metadata and controls
103 lines (92 loc) · 3.53 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
type compileInput = {value: string, path: string}
type compileOptions = {
outputFormat: string,
remarkPlugins: array<Mdx.remarkPlugin>,
}
@module("@mdx-js/mdx")
external compile: (compileInput, compileOptions) => promise<CompiledMdx.compileResult> = "compile"
@module("remark-frontmatter") external remarkFrontmatter: Mdx.remarkPlugin = "default"
let compileMdx = async (content, ~filePath, ~remarkPlugins=[]) => {
let compiled = await compile(
{value: content, path: filePath},
{
outputFormat: "function-body",
remarkPlugins: [remarkFrontmatter, ...remarkPlugins],
},
)
compiled->CompiledMdx.fromCompileResult
}
let normalizePathname = pathname => {
let pathname =
pathname->String.endsWith("/_.data")
? pathname->String.slice(~start=0, ~end=String.length(pathname) - String.length("/_.data"))
: pathname->String.endsWith(".data")
? pathname->String.slice(~start=0, ~end=String.length(pathname) - String.length(".data"))
: pathname
pathname !== "/" && pathname->String.endsWith("/")
? pathname->String.slice(~start=0, ~end=String.length(pathname) - 1)
: pathname
}
let resolveFilePath = (pathname, ~dir, ~alias) => {
let pathname = pathname->normalizePathname
let path = if pathname->String.startsWith("/") {
pathname->String.slice(~start=1, ~end=String.length(pathname))
} else {
pathname
}
let relativePath = if path->String.startsWith(alias ++ "/") {
let rest = path->String.slice(~start=String.length(alias) + 1, ~end=String.length(path))
Node.Path.join2(dir, rest)
} else if path->String.startsWith(alias) {
let rest = path->String.slice(~start=String.length(alias), ~end=String.length(path))
Node.Path.join2(dir, rest)
} else {
path
}
relativePath ++ ".mdx"
}
// 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.replaceAll("\\", "/")
->String.replace(baseDir->String.replaceAll("\\", "/") ++ "/", "")
->String.replace(".mdx", "")
[relativePath]
} else {
[]
}
})
}
// Convert frontmatter JSON dict to Mdx.attributes
// This is the same unsafe approach as react-router-mdx — frontmatter YAML
// becomes a JS object that we type as Mdx.attributes. Fields not present
// in the frontmatter (e.g. blog-specific `author`, `date`) are undefined at
// runtime, which is fine because docs/community code never accesses them.
external dictToAttributes: Dict.t<JSON.t> => Mdx.attributes = "%identity"
let loadAllAttributes = async (~dir) => {
let files = scanDir(dir, dir)
await Promise.all(
files->Array.map(async relativePath => {
let fullPath = Node.Path.join2(dir, relativePath ++ ".mdx")->String.replaceAll("\\", "/")
let raw = await Node.Fs.readFile(fullPath, "utf-8")
let {frontmatter}: MarkdownParser.result = MarkdownParser.parseSync(raw)
let dict = switch frontmatter {
| Object(dict) => dict
| _ => Dict.make()
}
// Add path and slug fields (same as react-router-mdx does)
dict->Dict.set("path", JSON.String(fullPath))
let slug = Node.Path.basename(relativePath)
dict->Dict.set("slug", JSON.String(slug))
dictToAttributes(dict)
}),
)
}