diff --git a/app/routes.res b/app/routes.res index 674cc3357..b5252ddc0 100644 --- a/app/routes.res +++ b/app/routes.res @@ -33,6 +33,11 @@ let blogArticleRoutes = route(path, "./routes/BlogArticleRoute.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 communityRoutes = MdxFile.scanPaths(~dir="markdown-pages/community", ~alias="community")->Array.map(path => route(path, "./routes/CommunityRoute.jsx", ~options={id: path}) @@ -44,6 +49,8 @@ let mdxRoutes = mdxRoutes("./routes/MdxRoute.jsx")->Array.filter(r => ->Option.map(path => path === "blog" || String.startsWith(path, "blog/") || + path === "docs/guidelines" || + String.startsWith(path, "docs/guidelines/") || path === "community" || String.startsWith(path, "community/") ) @@ -66,6 +73,7 @@ let default = [ ...stdlibRoutes, ...beltRoutes, ...blogArticleRoutes, + ...docsGuidelinesRoutes, ...communityRoutes, ...mdxRoutes, route("*", "./routes/NotFoundRoute.jsx"), diff --git a/app/routes/DocsGuidelinesRoute.res b/app/routes/DocsGuidelinesRoute.res new file mode 100644 index 000000000..8cec83187 --- /dev/null +++ b/app/routes/DocsGuidelinesRoute.res @@ -0,0 +1,88 @@ +type loaderData = { + compiledMdx: CompiledMdx.t, + entries: array, + title: string, + description: string, + filePath: string, +} + +let loader: ReactRouter.Loader.t = 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 = [] + + <> + + + + + {React.string("Edit")} + + + +
+ +
+
+ +} diff --git a/app/routes/DocsGuidelinesRoute.resi b/app/routes/DocsGuidelinesRoute.resi new file mode 100644 index 000000000..307767dcb --- /dev/null +++ b/app/routes/DocsGuidelinesRoute.resi @@ -0,0 +1,11 @@ +type loaderData = { + compiledMdx: CompiledMdx.t, + entries: array, + title: string, + description: string, + filePath: string, +} + +let loader: ReactRouter.Loader.t + +let default: unit => React.element