Skip to content

Commit bc74250

Browse files
committed
api nav is working
1 parent 924e84a commit bc74250

File tree

13 files changed

+1235
-294
lines changed

13 files changed

+1235
-294
lines changed

app/routes.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ let default = [
2323
route("community/overview", "./routes/CommunityRoute.mjs", ~options={id: "overview"}),
2424
route("syntax-lookup", "./routes/SyntaxLookupRoute.mjs"),
2525
route("blog", "./routes/BlogRoute.mjs"),
26+
// TODO RR7 get the api index to work with the same template
27+
// route("docs/manual/api", "./routes/ApiRoute.mjs", ~options={id: "api-index"}),
2628
route("docs/manual/api/stdlib", "./routes/ApiRoute.mjs", ~options={id: "api-stdlib"}),
2729
route("docs/manual/api/introduction", "./routes/ApiRoute.mjs", ~options={id: "api-intro"}),
2830
route("docs/manual/api/belt", "./routes/ApiRoute.mjs", ~options={id: "api-belt"}),

app/routes/ApiRoute.res

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,89 @@ let rec rawApiItemToNode = (apiItem: apiItem): ApiDocs.node => {
1212
{
1313
name: apiItem.name,
1414
path: apiItem.id
15-
->String.toLowerCase
1615
->String.split(".")
17-
->Array.filter(segment => segment !== "stdlib"),
18-
children: apiItem.items->Option.map(Array.map(_, rawApiItemToNode))->Option.getOr([]),
16+
->Array.filter(segment => segment !== "Stdlib" && segment !== "Belt" && segment !== "Js"),
17+
children: apiItem.items
18+
->Option.map(items =>
19+
Array.filter(items, item =>
20+
item.id
21+
->String.split(".")
22+
->Array.length > 3
23+
)->Array.map(rawApiItemToNode)
24+
)
25+
->Option.getOr([]),
1926
}
2027
}
2128

2229
@scope("JSON") @val
2330
external parseApi: string => Dict.t<apiItem> = "parse"
2431

32+
let groupItems = apiDocs => {
33+
let parsedItems =
34+
apiDocs
35+
->Dict.keysToArray
36+
->Array.map(key => Dict.getUnsafe(apiDocs, key))
37+
->Array.map(rawApiItemToNode)
38+
39+
// Root items are the main submodules like Array, String, etc
40+
let rootItems = []
41+
42+
// Child items are submodules to root items, such as Error.URIError, Error.TypeError, etc
43+
let childItems = []
44+
45+
// a few children have their own children, e.g. Intl.NumberFormat
46+
// If we ever get 4 children deep this will need to be refactored
47+
let childrenOfChildren = []
48+
49+
parsedItems->Array.forEach(node => {
50+
if node.path->Array.length < 2 {
51+
rootItems->Array.push(node)
52+
} else if node.path->Array.length > 2 {
53+
childrenOfChildren->Array.push(node)
54+
} else {
55+
childItems->Array.push(node)
56+
}
57+
})
58+
59+
// attach the child items to their respective parents
60+
childItems->Array.forEach(node => {
61+
let parent = node.path[0]
62+
switch parent {
63+
| Some(parent) =>
64+
rootItems
65+
->Array.find(item => item.name === parent)
66+
->Option.forEach(parentNode => {
67+
parentNode.children->Array.push({...node, children: []})
68+
})
69+
| None => ()
70+
}
71+
})
72+
73+
// attach the children of children to their respective parents
74+
childrenOfChildren->Array.forEach(node => {
75+
let parent = node.path[1]
76+
77+
parent->Option.forEach(parentName => {
78+
let parentNode =
79+
rootItems->Array.find(item => item.children->Array.some(node => node.name === parentName))
80+
81+
// TODO: this can probably be refactored
82+
parentNode->Option.forEach(
83+
parentNode =>
84+
parentNode.children->Array.forEach(
85+
child => {
86+
if child.name === parentName {
87+
child.children->Array.push({...node, children: []})
88+
}
89+
},
90+
),
91+
)
92+
})
93+
})
94+
95+
rootItems
96+
}
97+
2598
let loader: ReactRouter.Loader.t<loaderData> = async args => {
2699
let path =
27100
WebAPI.URL.make(~url=args.request.url).pathname
@@ -32,16 +105,17 @@ let loader: ReactRouter.Loader.t<loaderData> = async args => {
32105

33106
let stdlibToc = apiDocs->Dict.get("stdlib")
34107

35-
let toctree =
36-
apiDocs
37-
->Dict.keysToArray
38-
->Array.map(key => Dict.getUnsafe(apiDocs, key))
39-
->Array.map(rawApiItemToNode)
108+
let toctree = groupItems(apiDocs)
40109

41110
let data = {
42111
// TODO RR7: refactor this function to only return the module and not the toctree
43112
// or move the toc logic to this function
44-
await ApiDocs.getStaticProps(path)
113+
// TODO move the loader function to its own file
114+
try {
115+
await ApiDocs.getStaticProps(path)
116+
} catch {
117+
| err => {"props": Error(JSON.stringifyAny(err)->Option.getOr("Error loading API data"))}
118+
}
45119
}
46120

47121
data["props"]->Result.map((item): ApiDocs.api => {

0 commit comments

Comments
 (0)