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
11 changes: 10 additions & 1 deletion app/routes.res
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ let docsReactRoutes =
route(path, "./routes/DocsReactRoute.jsx", ~options={id: path})
)

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

let mdxRoutes = mdxRoutes("./routes/MdxRoute.jsx")->Array.filter(r =>
!(
r.path
Expand All @@ -52,7 +58,9 @@ let mdxRoutes = mdxRoutes("./routes/MdxRoute.jsx")->Array.filter(r =>
path === "docs/manual" ||
String.startsWith(path, "docs/manual/") ||
path === "docs/react" ||
String.startsWith(path, "docs/react/")
String.startsWith(path, "docs/react/") ||
path === "docs/guidelines" ||
String.startsWith(path, "docs/guidelines/")
)
->Option.getOr(false)
)
Expand All @@ -75,6 +83,7 @@ let default = [
...blogArticleRoutes,
...docsManualRoutes,
...docsReactRoutes,
...docsGuidelinesRoutes,
...mdxRoutes,
route("*", "./routes/NotFoundRoute.jsx"),
]
88 changes: 88 additions & 0 deletions app/routes/DocsGuidelinesRoute.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
type loaderData = {
compiledMdx: CompiledMdx.t,
entries: array<TableOfContents.entry>,
title: string,
description: string,
filePath: string,
}

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/docs/guidelines",
~alias="docs/guidelines",
)

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)

// Build table of contents entries from markdown headings
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) // skip document entry and H1 title, keep h2 sections

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

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

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

let categories: array<SidebarLayout.Sidebar.Category.t> = []

<>
<Meta title description />
<NavbarSecondary />
<NavbarTertiary>
<a
href=editHref className="inline text-14 hover:underline text-fire" rel="noopener noreferrer"
>
{React.string("Edit")}
</a>
</NavbarTertiary>
<DocsLayout categories activeToc={title, entries}>
<div className="markdown-body">
<MdxContent compiledMdx />
</div>
</DocsLayout>
</>
}
11 changes: 11 additions & 0 deletions app/routes/DocsGuidelinesRoute.resi
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type loaderData = {
compiledMdx: CompiledMdx.t,
entries: array<TableOfContents.entry>,
title: string,
description: string,
filePath: string,
}

let loader: ReactRouter.Loader.t<loaderData>

let default: unit => React.element
3 changes: 2 additions & 1 deletion app/routes/MdxRoute.res
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ let default = () => {
</>
} else if (
(pathname :> string)->String.includes("docs/manual") ||
(pathname :> string)->String.includes("docs/guidelines")
(pathname :> string)->String.includes("docs/react") ||
(pathname :> string)->String.includes("docs/guidelines")
) {
<>
<Meta title=title description={attributes.description->Nullable.getOr("")} />
Expand Down
Loading