|
| 1 | +type loaderData = { |
| 2 | + compiledMdx: CompiledMdx.t, |
| 3 | + categories: array<SidebarLayout.Sidebar.Category.t>, |
| 4 | + entries: array<TableOfContents.entry>, |
| 5 | + title: string, |
| 6 | + description: string, |
| 7 | + filePath: string, |
| 8 | +} |
| 9 | + |
| 10 | +// Build sidebar categories from all manual docs, sorted by their "order" field in frontmatter |
| 11 | +let manualTableOfContents = async () => { |
| 12 | + let groups = |
| 13 | + (await MdxFile.loadAllAttributes(~dir="markdown-pages/docs")) |
| 14 | + ->Mdx.filterMdxPages("docs/manual") |
| 15 | + ->Mdx.groupBySection |
| 16 | + ->Dict.mapValues(values => |
| 17 | + values->Mdx.sortSection->SidebarHelpers.convertToNavItems("/docs/manual") |
| 18 | + ) |
| 19 | + |
| 20 | + SidebarHelpers.getAllGroups( |
| 21 | + groups, |
| 22 | + [ |
| 23 | + "Overview", |
| 24 | + "Guides", |
| 25 | + "Language Features", |
| 26 | + "JavaScript Interop", |
| 27 | + "Build System", |
| 28 | + "Advanced Features", |
| 29 | + ], |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => { |
| 34 | + let {pathname} = WebAPI.URL.make(~url=request.url) |
| 35 | + let filePath = MdxFile.resolveFilePath( |
| 36 | + (pathname :> string), |
| 37 | + ~dir="markdown-pages/docs/manual", |
| 38 | + ~alias="docs/manual", |
| 39 | + ) |
| 40 | + |
| 41 | + let raw = await Node.Fs.readFile(filePath, "utf-8") |
| 42 | + let {frontmatter}: MarkdownParser.result = MarkdownParser.parseSync(raw) |
| 43 | + |
| 44 | + let description = switch frontmatter { |
| 45 | + | Object(dict) => |
| 46 | + switch dict->Dict.get("description") { |
| 47 | + | Some(String(s)) => s |
| 48 | + | _ => "" |
| 49 | + } |
| 50 | + | _ => "" |
| 51 | + } |
| 52 | + |
| 53 | + let title = switch frontmatter { |
| 54 | + | Object(dict) => |
| 55 | + switch dict->Dict.get("title") { |
| 56 | + | Some(String(s)) => s |
| 57 | + | _ => "" |
| 58 | + } |
| 59 | + | _ => "" |
| 60 | + } |
| 61 | + |
| 62 | + let categories = await manualTableOfContents() |
| 63 | + |
| 64 | + let compiledMdx = await MdxFile.compileMdx(raw, ~filePath, ~remarkPlugins=Mdx.plugins) |
| 65 | + |
| 66 | + // Build table of contents entries from markdown headings |
| 67 | + let markdownTree = Mdast.fromMarkdown(raw) |
| 68 | + let tocResult = Mdast.toc(markdownTree, {maxDepth: 2}) |
| 69 | + |
| 70 | + let headers = Dict.make() |
| 71 | + Mdast.reduceHeaders(tocResult.map, headers) |
| 72 | + |
| 73 | + let entries = |
| 74 | + headers |
| 75 | + ->Dict.toArray |
| 76 | + ->Array.map(((header, url)): TableOfContents.entry => { |
| 77 | + header, |
| 78 | + href: (url :> string), |
| 79 | + }) |
| 80 | + ->Array.slice(~start=2) // skip document entry and H1 title, keep h2 sections |
| 81 | + |
| 82 | + { |
| 83 | + compiledMdx, |
| 84 | + categories, |
| 85 | + entries, |
| 86 | + title: `${title} | ReScript Language Manual`, |
| 87 | + description, |
| 88 | + filePath, |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +let default = () => { |
| 93 | + let {pathname} = ReactRouter.useLocation() |
| 94 | + let {compiledMdx, categories, entries, title, description, filePath} = ReactRouter.useLoaderData() |
| 95 | + |
| 96 | + let breadcrumbs = list{ |
| 97 | + {Url.name: "Docs", href: "/docs/manual/introduction"}, |
| 98 | + { |
| 99 | + Url.name: "Language Manual", |
| 100 | + href: "/docs/manual/introduction", |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + let editHref = `https://github.com/rescript-lang/rescript-lang.org/blob/master/${filePath}` |
| 105 | + |
| 106 | + let sidebarContent = |
| 107 | + <aside className="px-4 w-full block"> |
| 108 | + <div className="flex justify-between items-baseline"> |
| 109 | + <div className="flex flex-col text-fire font-medium"> |
| 110 | + <VersionSelect /> |
| 111 | + </div> |
| 112 | + <button |
| 113 | + className="flex items-center" onClick={_ => NavbarUtils.closeMobileTertiaryDrawer()} |
| 114 | + > |
| 115 | + <Icon.Close /> |
| 116 | + </button> |
| 117 | + </div> |
| 118 | + <div className="mb-56"> |
| 119 | + {categories |
| 120 | + ->Array.map(category => { |
| 121 | + let isItemActive = (navItem: SidebarLayout.Sidebar.NavItem.t) => |
| 122 | + navItem.href === (pathname :> string) |
| 123 | + let getActiveToc = (navItem: SidebarLayout.Sidebar.NavItem.t) => |
| 124 | + if navItem.href === (pathname :> string) { |
| 125 | + Some({TableOfContents.title, entries}) |
| 126 | + } else { |
| 127 | + None |
| 128 | + } |
| 129 | + <div key=category.name> |
| 130 | + <SidebarLayout.Sidebar.Category |
| 131 | + isItemActive |
| 132 | + getActiveToc |
| 133 | + category |
| 134 | + onClick={_ => NavbarUtils.closeMobileTertiaryDrawer()} |
| 135 | + /> |
| 136 | + </div> |
| 137 | + }) |
| 138 | + ->React.array} |
| 139 | + </div> |
| 140 | + </aside> |
| 141 | + |
| 142 | + <> |
| 143 | + <Meta title description /> |
| 144 | + <NavbarSecondary /> |
| 145 | + <NavbarTertiary sidebar=sidebarContent> |
| 146 | + <SidebarLayout.BreadCrumbs crumbs=breadcrumbs /> |
| 147 | + <a |
| 148 | + href=editHref className="inline text-14 hover:underline text-fire" rel="noopener noreferrer" |
| 149 | + > |
| 150 | + {React.string("Edit")} |
| 151 | + </a> |
| 152 | + </NavbarTertiary> |
| 153 | + <DocsLayout categories activeToc={title, entries}> |
| 154 | + <div className="markdown-body"> |
| 155 | + <MdxContent compiledMdx /> |
| 156 | + </div> |
| 157 | + </DocsLayout> |
| 158 | + </> |
| 159 | +} |
0 commit comments