Skip to content

Commit 00a9390

Browse files
authored
Add routes for docs/guidelines MDX pages (#1224)
1 parent 1c5c7a4 commit 00a9390

3 files changed

Lines changed: 107 additions & 0 deletions

File tree

app/routes.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ let blogArticleRoutes =
3333
route(path, "./routes/BlogArticleRoute.jsx", ~options={id: path})
3434
)
3535

36+
let docsGuidelinesRoutes =
37+
MdxFile.scanPaths(
38+
~dir="markdown-pages/docs/guidelines",
39+
~alias="docs/guidelines",
40+
)->Array.map(path => route(path, "./routes/DocsGuidelinesRoute.jsx", ~options={id: path}))
3641
let communityRoutes =
3742
MdxFile.scanPaths(~dir="markdown-pages/community", ~alias="community")->Array.map(path =>
3843
route(path, "./routes/CommunityRoute.jsx", ~options={id: path})
@@ -44,6 +49,8 @@ let mdxRoutes = mdxRoutes("./routes/MdxRoute.jsx")->Array.filter(r =>
4449
->Option.map(path =>
4550
path === "blog" ||
4651
String.startsWith(path, "blog/") ||
52+
path === "docs/guidelines" ||
53+
String.startsWith(path, "docs/guidelines/") ||
4754
path === "community" ||
4855
String.startsWith(path, "community/")
4956
)
@@ -66,6 +73,7 @@ let default = [
6673
...stdlibRoutes,
6774
...beltRoutes,
6875
...blogArticleRoutes,
76+
...docsGuidelinesRoutes,
6977
...communityRoutes,
7078
...mdxRoutes,
7179
route("*", "./routes/NotFoundRoute.jsx"),

app/routes/DocsGuidelinesRoute.res

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
type loaderData = {
2+
compiledMdx: CompiledMdx.t,
3+
entries: array<TableOfContents.entry>,
4+
title: string,
5+
description: string,
6+
filePath: string,
7+
}
8+
9+
let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => {
10+
let {pathname} = WebAPI.URL.make(~url=request.url)
11+
let filePath = MdxFile.resolveFilePath(
12+
(pathname :> string),
13+
~dir="markdown-pages/docs/guidelines",
14+
~alias="docs/guidelines",
15+
)
16+
17+
let raw = await Node.Fs.readFile(filePath, "utf-8")
18+
let {frontmatter}: MarkdownParser.result = MarkdownParser.parseSync(raw)
19+
20+
let description = switch frontmatter {
21+
| Object(dict) =>
22+
switch dict->Dict.get("description") {
23+
| Some(String(s)) => s
24+
| _ => ""
25+
}
26+
| _ => ""
27+
}
28+
29+
let title = switch frontmatter {
30+
| Object(dict) =>
31+
switch dict->Dict.get("title") {
32+
| Some(String(s)) => s
33+
| _ => ""
34+
}
35+
| _ => ""
36+
}
37+
38+
let compiledMdx = await MdxFile.compileMdx(raw, ~filePath, ~remarkPlugins=Mdx.plugins)
39+
40+
// Build table of contents entries from markdown headings
41+
let markdownTree = Mdast.fromMarkdown(raw)
42+
let tocResult = Mdast.toc(markdownTree, {maxDepth: 2})
43+
44+
let headers = Dict.make()
45+
Mdast.reduceHeaders(tocResult.map, headers)
46+
47+
let entries =
48+
headers
49+
->Dict.toArray
50+
->Array.map(((header, url)): TableOfContents.entry => {
51+
header,
52+
href: (url :> string),
53+
})
54+
->Array.slice(~start=2) // skip document entry and H1 title, keep h2 sections
55+
56+
{
57+
compiledMdx,
58+
entries,
59+
title: `${title} | ReScript Guidelines`,
60+
description,
61+
filePath,
62+
}
63+
}
64+
65+
let default = () => {
66+
let {compiledMdx, entries, title, description, filePath} = ReactRouter.useLoaderData()
67+
68+
let editHref = `https://github.com/rescript-lang/rescript-lang.org/blob/master/${filePath}`
69+
70+
let categories: array<SidebarLayout.Sidebar.Category.t> = []
71+
72+
<>
73+
<Meta title description />
74+
<NavbarSecondary />
75+
<NavbarTertiary>
76+
<a
77+
href=editHref className="inline text-14 hover:underline text-fire" rel="noopener noreferrer"
78+
>
79+
{React.string("Edit")}
80+
</a>
81+
</NavbarTertiary>
82+
<DocsLayout categories activeToc={title, entries}>
83+
<div className="markdown-body">
84+
<MdxContent compiledMdx />
85+
</div>
86+
</DocsLayout>
87+
</>
88+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type loaderData = {
2+
compiledMdx: CompiledMdx.t,
3+
entries: array<TableOfContents.entry>,
4+
title: string,
5+
description: string,
6+
filePath: string,
7+
}
8+
9+
let loader: ReactRouter.Loader.t<loaderData>
10+
11+
let default: unit => React.element

0 commit comments

Comments
 (0)