|
| 1 | +type loaderData = { |
| 2 | + compiledMdx: CompiledMdx.t, |
| 3 | + entries: array<TableOfContents.entry>, |
| 4 | + title: string, |
| 5 | + description: string, |
| 6 | + filePath: string, |
| 7 | +} |
| 8 | + |
| 9 | +let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => { |
| 10 | + let {pathname} = WebAPI.URL.make(~url=request.url) |
| 11 | + let filePath = MdxFile.resolveFilePath( |
| 12 | + (pathname :> string), |
| 13 | + ~dir="markdown-pages/docs/guidelines", |
| 14 | + ~alias="docs/guidelines", |
| 15 | + ) |
| 16 | + |
| 17 | + let raw = await Node.Fs.readFile(filePath, "utf-8") |
| 18 | + let {frontmatter}: MarkdownParser.result = MarkdownParser.parseSync(raw) |
| 19 | + |
| 20 | + let description = switch frontmatter { |
| 21 | + | Object(dict) => |
| 22 | + switch dict->Dict.get("description") { |
| 23 | + | Some(String(s)) => s |
| 24 | + | _ => "" |
| 25 | + } |
| 26 | + | _ => "" |
| 27 | + } |
| 28 | + |
| 29 | + let title = switch frontmatter { |
| 30 | + | Object(dict) => |
| 31 | + switch dict->Dict.get("title") { |
| 32 | + | Some(String(s)) => s |
| 33 | + | _ => "" |
| 34 | + } |
| 35 | + | _ => "" |
| 36 | + } |
| 37 | + |
| 38 | + let compiledMdx = await MdxFile.compileMdx(raw, ~filePath, ~remarkPlugins=Mdx.plugins) |
| 39 | + |
| 40 | + // Build table of contents entries from markdown headings |
| 41 | + let markdownTree = Mdast.fromMarkdown(raw) |
| 42 | + let tocResult = Mdast.toc(markdownTree, {maxDepth: 2}) |
| 43 | + |
| 44 | + let headers = Dict.make() |
| 45 | + Mdast.reduceHeaders(tocResult.map, headers) |
| 46 | + |
| 47 | + let entries = |
| 48 | + headers |
| 49 | + ->Dict.toArray |
| 50 | + ->Array.map(((header, url)): TableOfContents.entry => { |
| 51 | + header, |
| 52 | + href: (url :> string), |
| 53 | + }) |
| 54 | + ->Array.slice(~start=2) // skip document entry and H1 title, keep h2 sections |
| 55 | + |
| 56 | + { |
| 57 | + compiledMdx, |
| 58 | + entries, |
| 59 | + title: `${title} | ReScript Guidelines`, |
| 60 | + description, |
| 61 | + filePath, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +let default = () => { |
| 66 | + let {compiledMdx, entries, title, description, filePath} = ReactRouter.useLoaderData() |
| 67 | + |
| 68 | + let editHref = `https://github.com/rescript-lang/rescript-lang.org/blob/master/${filePath}` |
| 69 | + |
| 70 | + let categories: array<SidebarLayout.Sidebar.Category.t> = [] |
| 71 | + |
| 72 | + <> |
| 73 | + <Meta title description /> |
| 74 | + <NavbarSecondary /> |
| 75 | + <NavbarTertiary> |
| 76 | + <a |
| 77 | + href=editHref className="inline text-14 hover:underline text-fire" rel="noopener noreferrer" |
| 78 | + > |
| 79 | + {React.string("Edit")} |
| 80 | + </a> |
| 81 | + </NavbarTertiary> |
| 82 | + <DocsLayout categories activeToc={title, entries}> |
| 83 | + <div className="markdown-body"> |
| 84 | + <MdxContent compiledMdx /> |
| 85 | + </div> |
| 86 | + </DocsLayout> |
| 87 | + </> |
| 88 | +} |
0 commit comments