|
| 1 | +type loaderData = { |
| 2 | + compiledMdx: CompiledMdx.t, |
| 3 | + entries: array<TableOfContents.entry>, |
| 4 | + title: string, |
| 5 | + description: string, |
| 6 | + filePath: string, |
| 7 | + categories: array<SidebarLayout.Sidebar.Category.t>, |
| 8 | +} |
| 9 | + |
| 10 | +let convertToNavItems = (items, rootPath) => |
| 11 | + Array.map(items, (item): SidebarLayout.Sidebar.NavItem.t => { |
| 12 | + let href = switch item.Mdx.slug { |
| 13 | + | Some(slug) => `${rootPath}/${slug}` |
| 14 | + | None => rootPath |
| 15 | + } |
| 16 | + { |
| 17 | + name: item.title, |
| 18 | + href, |
| 19 | + } |
| 20 | + }) |
| 21 | + |
| 22 | +let getGroup = (groups, groupName): SidebarLayout.Sidebar.Category.t => { |
| 23 | + { |
| 24 | + name: groupName, |
| 25 | + items: groups |
| 26 | + ->Dict.get(groupName) |
| 27 | + ->Option.getOr([]), |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +let getAllGroups = (groups, groupNames): array<SidebarLayout.Sidebar.Category.t> => |
| 32 | + groupNames->Array.map(item => getGroup(groups, item)) |
| 33 | + |
| 34 | +let communityTableOfContents = async () => { |
| 35 | + let groups = |
| 36 | + (await Mdx.allMdx(~filterByPaths=["markdown-pages/community"])) |
| 37 | + ->Mdx.filterMdxPages("community") |
| 38 | + ->Mdx.groupBySection |
| 39 | + ->Dict.mapValues(values => values->Mdx.sortSection->convertToNavItems("/community")) |
| 40 | + |
| 41 | + getAllGroups(groups, ["Resources"]) |
| 42 | +} |
| 43 | + |
| 44 | +let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => { |
| 45 | + let {pathname} = WebAPI.URL.make(~url=request.url) |
| 46 | + let filePath = MdxFile.resolveFilePath( |
| 47 | + (pathname :> string), |
| 48 | + ~dir="markdown-pages/community", |
| 49 | + ~alias="community", |
| 50 | + ) |
| 51 | + |
| 52 | + let raw = await Node.Fs.readFile(filePath, "utf-8") |
| 53 | + let {frontmatter}: MarkdownParser.result = MarkdownParser.parseSync(raw) |
| 54 | + |
| 55 | + let description = switch frontmatter { |
| 56 | + | Object(dict) => |
| 57 | + switch dict->Dict.get("description") { |
| 58 | + | Some(String(s)) => s |
| 59 | + | _ => "" |
| 60 | + } |
| 61 | + | _ => "" |
| 62 | + } |
| 63 | + |
| 64 | + let title = switch frontmatter { |
| 65 | + | Object(dict) => |
| 66 | + switch dict->Dict.get("title") { |
| 67 | + | Some(String(s)) => s |
| 68 | + | _ => "" |
| 69 | + } |
| 70 | + | _ => "" |
| 71 | + } |
| 72 | + |
| 73 | + let compiledMdx = await MdxFile.compileMdx(raw, ~filePath, ~remarkPlugins=Mdx.plugins) |
| 74 | + |
| 75 | + let markdownTree = Mdast.fromMarkdown(raw) |
| 76 | + let tocResult = Mdast.toc(markdownTree, {maxDepth: 2}) |
| 77 | + |
| 78 | + let headers = Dict.make() |
| 79 | + Mdast.reduceHeaders(tocResult.map, headers) |
| 80 | + |
| 81 | + let entries = |
| 82 | + headers |
| 83 | + ->Dict.toArray |
| 84 | + ->Array.map(((header, url)): TableOfContents.entry => { |
| 85 | + header, |
| 86 | + href: (url :> string), |
| 87 | + }) |
| 88 | + ->Array.slice(~start=2) |
| 89 | + |
| 90 | + let categories = await communityTableOfContents() |
| 91 | + |
| 92 | + { |
| 93 | + compiledMdx, |
| 94 | + entries, |
| 95 | + title: `${title} | ReScript Community`, |
| 96 | + description, |
| 97 | + filePath, |
| 98 | + categories, |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +let default = () => { |
| 103 | + let {compiledMdx, entries, filePath, categories} = ReactRouter.useLoaderData() |
| 104 | + |
| 105 | + let editHref = `https://github.com/rescript-lang/rescript-lang.org/blob/master/${filePath}` |
| 106 | + |
| 107 | + <> |
| 108 | + <CommunityLayout categories entries> |
| 109 | + <div className="markdown-body"> |
| 110 | + <MdxContent compiledMdx /> |
| 111 | + </div> |
| 112 | + <a |
| 113 | + href=editHref className="inline text-14 hover:underline text-fire" rel="noopener noreferrer" |
| 114 | + > |
| 115 | + {React.string("Edit")} |
| 116 | + </a> |
| 117 | + </CommunityLayout> |
| 118 | + </> |
| 119 | +} |
0 commit comments