Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/routes.res
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ let docsGuidelinesRoutes =
~alias="docs/guidelines",
)->Array.map(path => route(path, "./routes/DocsGuidelinesRoute.jsx", ~options={id: path}))

let communityRoutes =
MdxFile.scanPaths(~dir="markdown-pages/community", ~alias="community")->Array.map(path =>
route(path, "./routes/CommunityRoute.jsx", ~options={id: path})
)

let mdxRoutes = mdxRoutes("./routes/MdxRoute.jsx")->Array.filter(r =>
!(
r.path
Expand All @@ -60,7 +65,9 @@ let mdxRoutes = mdxRoutes("./routes/MdxRoute.jsx")->Array.filter(r =>
path === "docs/react" ||
String.startsWith(path, "docs/react/") ||
path === "docs/guidelines" ||
String.startsWith(path, "docs/guidelines/")
String.startsWith(path, "docs/guidelines/") ||
path === "community" ||
String.startsWith(path, "community/")
)
->Option.getOr(false)
)
Expand All @@ -84,6 +91,7 @@ let default = [
...docsManualRoutes,
...docsReactRoutes,
...docsGuidelinesRoutes,
...communityRoutes,
...mdxRoutes,
route("*", "./routes/NotFoundRoute.jsx"),
]
97 changes: 97 additions & 0 deletions app/routes/CommunityRoute.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
type loaderData = {
compiledMdx: CompiledMdx.t,
entries: array<TableOfContents.entry>,
title: string,
description: string,
filePath: string,
categories: array<SidebarLayout.Sidebar.Category.t>,
}

let communityTableOfContents = async () => {
let groups =
(await MdxFile.loadAllAttributes(~dir="markdown-pages/community"))
->Mdx.filterMdxPages("community")
->Mdx.groupBySection
->Dict.mapValues(values =>
values->Mdx.sortSection->SidebarHelpers.convertToNavItems("/community")
)

SidebarHelpers.getAllGroups(groups, ["Resources"])
}

let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => {
let {pathname} = WebAPI.URL.make(~url=request.url)
let filePath = MdxFile.resolveFilePath(
(pathname :> string),
~dir="markdown-pages/community",
~alias="community",
)

let raw = await Node.Fs.readFile(filePath, "utf-8")
let {frontmatter}: MarkdownParser.result = MarkdownParser.parseSync(raw)

let description = switch frontmatter {
| Object(dict) =>
switch dict->Dict.get("description") {
| Some(String(s)) => s
| _ => ""
}
| _ => ""
}

let title = switch frontmatter {
| Object(dict) =>
switch dict->Dict.get("title") {
| Some(String(s)) => s
| _ => ""
}
| _ => ""
}

let compiledMdx = await MdxFile.compileMdx(raw, ~filePath, ~remarkPlugins=Mdx.plugins)

let markdownTree = Mdast.fromMarkdown(raw)
let tocResult = Mdast.toc(markdownTree, {maxDepth: 2})

let headers = Dict.make()
Mdast.reduceHeaders(tocResult.map, headers)

let entries =
headers
->Dict.toArray
->Array.map(((header, url)): TableOfContents.entry => {
header,
href: (url :> string),
})
->Array.slice(~start=2)

let categories = await communityTableOfContents()

{
compiledMdx,
entries,
title: `${title} | ReScript Community`,
description,
filePath,
categories,
}
}

let default = () => {
let {compiledMdx, entries, filePath, categories} = ReactRouter.useLoaderData()

let editHref = `https://github.com/rescript-lang/rescript-lang.org/blob/master/${filePath}`

<>
<CommunityLayout categories entries>
<div className="markdown-body">
<MdxContent compiledMdx />
</div>
<a
href=editHref className="inline text-14 hover:underline text-fire" rel="noopener noreferrer"
>
{React.string("Edit")}
</a>
</CommunityLayout>
</>
}
24 changes: 1 addition & 23 deletions app/routes/MdxRoute.res
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,6 @@ let manualTableOfContents = async () => {
categories
}

let communityTableOfContents = async () => {
let groups =
(await allMdx(~filterByPaths=["markdown-pages/community"]))
->filterMdxPages("community")
->groupBySection
->Dict.mapValues(values => values->sortSection->convertToNavItems("/community"))

// these are the categories that appear in the sidebar
let categories: array<SidebarLayout.Sidebar.Category.t> = getAllGroups(groups, ["Resources"])

categories
}

let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => {
let {pathname} = WebAPI.URL.make(~url=request.url)

Expand Down Expand Up @@ -147,8 +134,6 @@ let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => {
[]
} else if pathname->String.includes("docs/manual") {
await manualTableOfContents()
} else if pathname->String.includes("community") {
await communityTableOfContents()
} else {
[]
}
Expand Down Expand Up @@ -212,8 +197,6 @@ let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => {
"ReScript API"
} else if path->String.includes("docs/manual") {
"ReScript Language Manual"
} else if path->String.includes("community") {
"ReScript Community"
} else {
"ReScript"
}
Expand Down Expand Up @@ -294,8 +277,7 @@ let default = () => {
</>
} else if (
(pathname :> string)->String.includes("docs/manual") ||
(pathname :> string)->String.includes("docs/react") ||
(pathname :> string)->String.includes("docs/guidelines")
(pathname :> string)->String.includes("docs/react")
) {
<>
<Meta title=title description={attributes.description->Nullable.getOr("")} />
Expand Down Expand Up @@ -372,10 +354,6 @@ let default = () => {
</>
}
</>
} else if (pathname :> string)->String.includes("community") {
<CommunityLayout categories entries>
<div className="markdown-body"> {component()} </div>
</CommunityLayout>
} else {
switch loaderData.mdxSources {
| Some(mdxSources) =>
Expand Down
Loading