Skip to content

Commit f566bec

Browse files
committed
working on api table of contents
1 parent 778f521 commit f566bec

File tree

3 files changed

+13206
-15
lines changed

3 files changed

+13206
-15
lines changed

app/routes/ApiRoute.res

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,59 @@
11
type loaderData = ApiDocs.props
22

3+
type rec apiItem = {
4+
id: string,
5+
kind: string,
6+
name: string,
7+
items?: array<apiItem>,
8+
docStrings: array<string>,
9+
}
10+
11+
let rec rawApiItemToNode = (apiItem: apiItem): ApiDocs.node => {
12+
{
13+
name: apiItem.name,
14+
path: apiItem.id
15+
->String.toLowerCase
16+
->String.split(".")
17+
->Array.filter(segment => segment !== "stdlib"),
18+
children: apiItem.items->Option.map(Array.map(_, rawApiItemToNode))->Option.getOr([]),
19+
}
20+
}
21+
22+
@scope("JSON") @val
23+
external parseApi: string => Dict.t<apiItem> = "parse"
24+
325
let loader: ReactRouter.Loader.t<loaderData> = async args => {
4-
let pathname =
5-
WebAPI.URL.make(~url=args.request.url).pathname->String.replace("/docs/manual/api/", "")
6-
Console.log(pathname)
26+
let path =
27+
WebAPI.URL.make(~url=args.request.url).pathname
28+
->String.replace("/docs/manual/api/", "")
29+
->String.split("/")
30+
31+
let apiDocs = parseApi(await Node.Fs.readFile("./docs/api/stdlib.json", "utf-8"))
32+
33+
let stdlibToc = apiDocs->Dict.get("stdlib")
34+
35+
let toctree =
36+
apiDocs
37+
->Dict.keysToArray
38+
->Array.map(key => Dict.getUnsafe(apiDocs, key))
39+
->Array.map(rawApiItemToNode)
740

841
let data = {
9-
await ApiDocs.getStaticProps(["stdlib", "bigint"])
42+
// TODO RR7: refactor this function to only return the module and not the toctree
43+
// or move the toc logic to this function
44+
await ApiDocs.getStaticProps(path)
1045
}
1146

12-
data["props"]
47+
data["props"]->Result.map((item): ApiDocs.api => {
48+
{
49+
module_: item.module_,
50+
toctree: {
51+
name: "Stdlib",
52+
path: [],
53+
children: toctree,
54+
},
55+
}
56+
})
1357
}
1458

1559
let default = () => {

src/ApiDocs.res

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,17 @@ module SidebarTree = {
9797
// let router = Next.Router.useRouter() // Remove Next.js router usage if not needed
9898
open ReactRouter
9999

100+
let location = useLocation()
101+
102+
Console.log(location)
103+
100104
// Use ReactRouter's useLocation if needed, or refactor to not use router
101105
// let location = ReactRouter.useLocation()
102106
// let url = ""
103107
// let url = location.pathname->Url.parse
104108

105109
let moduleRoute =
106-
WebAPI.URL.make(~url="file://" ++ "" /* TODO: location.pathname */).pathname
110+
(location.pathname :> string)
107111
->String.replace(`/docs/manual/api/`, "")
108112
->String.split("/")
109113

@@ -456,15 +460,10 @@ let processStaticProps = (~slug: array<string>) => {
456460
let {items, docstrings, deprecated, name} = Docgen.decodeFromJson(json)
457461

458462
let id = switch json {
459-
| Object(dict) => {
460-
Console.log(dict)
461-
switch Dict.get(dict, "id") {
462-
| Some(String(s)) => {
463-
Console.log2(100, s)
464-
s
465-
}
466-
| _ => ""
467-
}
463+
| Object(dict) =>
464+
switch Dict.get(dict, "id") {
465+
| Some(String(s)) => s
466+
| _ => ""
468467
}
469468
| _ => ""
470469
}

0 commit comments

Comments
 (0)