Skip to content

Commit 4801dfe

Browse files
committed
feat: split community pages out of MdxRoute into CommunityRoute
- Create CommunityRoute.res with dedicated loader and community sidebar - Register communityRoutes in routes.res, filter community from mdxRoutes - Remove communityTableOfContents, community branches from MdxRoute
1 parent 7a704d4 commit 4801dfe

3 files changed

Lines changed: 107 additions & 24 deletions

File tree

app/routes.res

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ let docsGuidelinesRoutes =
4949
~alias="docs/guidelines",
5050
)->Array.map(path => route(path, "./routes/DocsGuidelinesRoute.jsx", ~options={id: path}))
5151

52+
let communityRoutes =
53+
MdxFile.scanPaths(~dir="markdown-pages/community", ~alias="community")->Array.map(path =>
54+
route(path, "./routes/CommunityRoute.jsx", ~options={id: path})
55+
)
56+
5257
let mdxRoutes = mdxRoutes("./routes/MdxRoute.jsx")->Array.filter(r =>
5358
!(
5459
r.path
@@ -60,7 +65,9 @@ let mdxRoutes = mdxRoutes("./routes/MdxRoute.jsx")->Array.filter(r =>
6065
path === "docs/react" ||
6166
String.startsWith(path, "docs/react/") ||
6267
path === "docs/guidelines" ||
63-
String.startsWith(path, "docs/guidelines/")
68+
String.startsWith(path, "docs/guidelines/") ||
69+
path === "community" ||
70+
String.startsWith(path, "community/")
6471
)
6572
->Option.getOr(false)
6673
)
@@ -84,6 +91,7 @@ let default = [
8491
...docsManualRoutes,
8592
...docsReactRoutes,
8693
...docsGuidelinesRoutes,
94+
...communityRoutes,
8795
...mdxRoutes,
8896
route("*", "./routes/NotFoundRoute.jsx"),
8997
]

app/routes/CommunityRoute.res

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

app/routes/MdxRoute.res

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,6 @@ let manualTableOfContents = async () => {
9999
categories
100100
}
101101

102-
let communityTableOfContents = async () => {
103-
let groups =
104-
(await allMdx(~filterByPaths=["markdown-pages/community"]))
105-
->filterMdxPages("community")
106-
->groupBySection
107-
->Dict.mapValues(values => values->sortSection->convertToNavItems("/community"))
108-
109-
// these are the categories that appear in the sidebar
110-
let categories: array<SidebarLayout.Sidebar.Category.t> = getAllGroups(groups, ["Resources"])
111-
112-
categories
113-
}
114-
115102
let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => {
116103
let {pathname} = WebAPI.URL.make(~url=request.url)
117104

@@ -147,8 +134,6 @@ let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => {
147134
[]
148135
} else if pathname->String.includes("docs/manual") {
149136
await manualTableOfContents()
150-
} else if pathname->String.includes("community") {
151-
await communityTableOfContents()
152137
} else {
153138
[]
154139
}
@@ -212,8 +197,6 @@ let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => {
212197
"ReScript API"
213198
} else if path->String.includes("docs/manual") {
214199
"ReScript Language Manual"
215-
} else if path->String.includes("community") {
216-
"ReScript Community"
217200
} else {
218201
"ReScript"
219202
}
@@ -294,8 +277,7 @@ let default = () => {
294277
</>
295278
} else if (
296279
(pathname :> string)->String.includes("docs/manual") ||
297-
(pathname :> string)->String.includes("docs/react") ||
298-
(pathname :> string)->String.includes("docs/guidelines")
280+
(pathname :> string)->String.includes("docs/react")
299281
) {
300282
<>
301283
<Meta title=title description={attributes.description->Nullable.getOr("")} />
@@ -372,10 +354,6 @@ let default = () => {
372354
</>
373355
}
374356
</>
375-
} else if (pathname :> string)->String.includes("community") {
376-
<CommunityLayout categories entries>
377-
<div className="markdown-body"> {component()} </div>
378-
</CommunityLayout>
379357
} else {
380358
switch loaderData.mdxSources {
381359
| Some(mdxSources) =>

0 commit comments

Comments
 (0)