Skip to content

Commit 2aae712

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 5226348 commit 2aae712

File tree

3 files changed

+131
-22
lines changed

3 files changed

+131
-22
lines changed

app/routes.res

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,20 @@ let blogArticleRoutes =
3333
route(path, "./routes/BlogArticleRoute.jsx", ~options={id: path})
3434
)
3535

36+
let communityRoutes =
37+
MdxFile.scanPaths(~dir="markdown-pages/community", ~alias="community")->Array.map(path =>
38+
route(path, "./routes/CommunityRoute.jsx", ~options={id: path})
39+
)
40+
3641
let mdxRoutes = mdxRoutes("./routes/MdxRoute.jsx")->Array.filter(r =>
3742
!(
3843
r.path
39-
->Option.map(path => path === "blog" || String.startsWith(path, "blog/"))
44+
->Option.map(path =>
45+
path === "blog" ||
46+
String.startsWith(path, "blog/") ||
47+
path === "community" ||
48+
String.startsWith(path, "community/")
49+
)
4050
->Option.getOr(false)
4151
)
4252
)
@@ -56,6 +66,7 @@ let default = [
5666
...stdlibRoutes,
5767
...beltRoutes,
5868
...blogArticleRoutes,
69+
...communityRoutes,
5970
...mdxRoutes,
6071
route("*", "./routes/NotFoundRoute.jsx"),
6172
]

app/routes/CommunityRoute.res

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

app/routes/MdxRoute.res

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,6 @@ let reactTableOfContents = async () => {
115115
categories
116116
}
117117

118-
let communityTableOfContents = async () => {
119-
let groups =
120-
(await allMdx(~filterByPaths=["markdown-pages/community"]))
121-
->filterMdxPages("community")
122-
->groupBySection
123-
->Dict.mapValues(values => values->sortSection->convertToNavItems("/community"))
124-
125-
// these are the categories that appear in the sidebar
126-
let categories: array<SidebarLayout.Sidebar.Category.t> = getAllGroups(groups, ["Resources"])
127-
128-
categories
129-
}
130-
131118
let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => {
132119
let {pathname} = WebAPI.URL.make(~url=request.url)
133120

@@ -165,8 +152,6 @@ let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => {
165152
await manualTableOfContents()
166153
} else if pathname->String.includes("docs/react") {
167154
await reactTableOfContents()
168-
} else if pathname->String.includes("community") {
169-
await communityTableOfContents()
170155
} else {
171156
[]
172157
}
@@ -240,8 +225,6 @@ let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => {
240225
"ReScript API"
241226
} else if path->String.includes("docs/manual") {
242227
"ReScript Language Manual"
243-
} else if path->String.includes("community") {
244-
"ReScript Community"
245228
} else {
246229
"ReScript"
247230
}
@@ -402,10 +385,6 @@ let default = () => {
402385
</>
403386
}
404387
</>
405-
} else if (pathname :> string)->String.includes("community") {
406-
<CommunityLayout categories entries>
407-
<div className="markdown-body"> {component()} </div>
408-
</CommunityLayout>
409388
} else {
410389
switch loaderData.mdxSources {
411390
| Some(mdxSources) =>

0 commit comments

Comments
 (0)