Skip to content

Commit 3b5acdb

Browse files
committed
language manual
1 parent 100fb4c commit 3b5acdb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+135
-76
lines changed

app/root.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ let default = () => {
3333
<head>
3434
// This is to prevent FOUC (flash of unstyled content)
3535
// This line has to be above everything else
36-
<style> {React.string("html{opacity: 0;}")} </style>
36+
// <style> {React.string("html{opacity: 0;}")} </style>
3737

3838
<link rel="icon" href="data:image/x-icon;base64,AA" />
3939
</head>

app/routes/MdxRoute.res

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,72 @@ let components = {
4242
"Warn": Markdown.Warn.make,
4343
}
4444

45-
// the loadAllMdx function logs out all of the file contents as it reads them, which is noisy and not useful.
45+
// The loadAllMdx function logs out all of the file contents as it reads them, which is noisy and not useful.
4646
// We can suppress that logging with this helper function.
4747
let allMdx = await Shims.runWithoutLogging(() => loadAllMdx())
4848

49+
let sortSection = mdxPages =>
50+
Array.toSorted(mdxPages, (a: Mdx.attributes, b: Mdx.attributes) =>
51+
switch (a.order, b.order) {
52+
| (Some(a), Some(b)) => a > b ? 1.0 : -1.0
53+
| _ => -1.0
54+
}
55+
)
56+
57+
let groupBySection = mdxPages =>
58+
Array.reduce(mdxPages, (Dict.make() :> Dict.t<array<Mdx.attributes>>), (acc, item) => {
59+
let section = item.section->Option.flatMap(Dict.get(acc, _))
60+
switch section {
61+
// If the section already exists, add this item to it
62+
| Some(section) => section->Array.push(item)
63+
// otherwise create a new section with this item
64+
| None => item.section->Option.forEach(section => acc->Dict.set(section, [item]))
65+
}
66+
acc
67+
})
68+
69+
let convertToNavItems = items =>
70+
Array.map(items, (item): SidebarLayout.Sidebar.NavItem.t => {
71+
{
72+
name: item.title,
73+
href: item.canonical, // TODO: RR7 - canonical works for now, but we should make this more robust so that it's not required
74+
}
75+
})
76+
77+
let filterMdxPages = (mdxPages, path) =>
78+
Array.filter(mdxPages, mdx => (mdx.path :> string)->String.includes(path))
79+
80+
// These are the pages for the language manual, sorted by their "order" field in the frontmatter
81+
let manualTableOfContents = () => {
82+
let groups =
83+
allMdx
84+
->filterMdxPages("docs/manual")
85+
->groupBySection
86+
->Dict.mapValues(values => values->sortSection->convertToNavItems)
87+
88+
// Console.log(groups)
89+
90+
// these are the categories that appear in the sidebar
91+
let categories: array<SidebarLayout.Sidebar.Category.t> = [
92+
{name: "Overview", items: groups->Dict.getUnsafe("Overview")},
93+
{name: "Guides", items: groups->Dict.getUnsafe("Guides")},
94+
{name: "Language Features", items: groups->Dict.getUnsafe("Language Features")},
95+
{name: "JavaScript Interop", items: groups->Dict.getUnsafe("JavaScript Interop")},
96+
{name: "Build System", items: groups->Dict.getUnsafe("Build System")},
97+
{name: "Advanced Features", items: groups->Dict.getUnsafe("Advanced Features")},
98+
]
99+
categories
100+
}
101+
49102
let loader: Loader.t<loaderData> = async ({request}) => {
103+
let {pathname} = WebAPI.URL.make(~url=request.url)
104+
50105
let mdx = await loadMdx(request)
51106

107+
let categories = manualTableOfContents()
108+
52109
let fileContents = await allMdx
53-
->Array.filter(mdx => (mdx.path :> string)->String.includes("docs/manual/introduction"))
110+
->Array.filter(mdx => (mdx.path :> string)->String.includes(pathname))
54111
->Array.get(0)
55112
->Option.map(mdx => mdx.path)
56113
->Option.map(path => Node.Fs.readFile((path :> string), "utf-8"))
@@ -70,18 +127,13 @@ let loader: Loader.t<loaderData> = async ({request}) => {
70127
header,
71128
href: (url :> string),
72129
})
73-
->Array.slice(~start=2) // skip first two entries which are "Introduction" and "Getting Started"
130+
->Array.slice(~start=2) // skip first two entries which are the document entry and the H1 title for the page, we just want the h2 sections
74131

75132
let res: loaderData = {
76133
__raw: mdx.__raw,
77134
attributes: mdx.attributes,
78135
entries,
79-
categories: [
80-
{
81-
name: "overview",
82-
items: [{name: "Introduction", href: #"/docs/manual/introduction"}],
83-
},
84-
],
136+
categories,
85137
}
86138
res
87139
}

docs/manual/array-and-list.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Array & List"
33
description: "Arrays and List data structures"
4-
canonical: "/docs/manual/v12.0.0/array-and-list"
4+
canonical: "/docs/manual/array-and-list"
55
section: "Language Features"
66
order: 12
77
---

docs/manual/async-await.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Async / Await"
33
description: "Async / await for asynchronous operations"
4-
canonical: "/docs/manual/v12.0.0/async-await"
4+
canonical: "/docs/manual/async-await"
55
section: "Language Features"
66
order: 22
77
---

docs/manual/attribute.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Attribute (Decorator)"
33
description: "Annotations in ReScript"
4-
canonical: "/docs/manual/v12.0.0/attribute"
4+
canonical: "/docs/manual/attribute"
55
section: "Language Features"
66
order: 26
77
---

docs/manual/bind-to-global-js-values.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Bind to Global JS Values"
33
description: "JS interop with global JS values in ReScript"
4-
canonical: "/docs/manual/v12.0.0/bind-to-global-js-values"
4+
canonical: "/docs/manual/bind-to-global-js-values"
55
section: "JavaScript Interop"
66
order: 8
77
---

docs/manual/bind-to-js-function.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Bind to JS Function"
33
description: "JS interop with functions in ReScript"
4-
canonical: "/docs/manual/v12.0.0/bind-to-js-function"
4+
canonical: "/docs/manual/bind-to-js-function"
55
section: "JavaScript Interop"
66
order: 6
77
---

docs/manual/bind-to-js-object.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Bind to JS Object"
33
description: "Interop with JS objects in ReScript"
4-
canonical: "/docs/manual/v12.0.0/bind-to-js-object"
4+
canonical: "/docs/manual/bind-to-js-object"
55
section: "JavaScript Interop"
66
order: 5
77
---

docs/manual/browser-support-polyfills.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Browser Support & Polyfills"
33
description: "Note on browser support in ReScript"
4-
canonical: "/docs/manual/v12.0.0/browser-support-polyfills"
4+
canonical: "/docs/manual/browser-support-polyfills"
55
section: "JavaScript Interop"
66
order: 13
77
---

docs/manual/build-configuration-schema.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Configuration Schema"
33
metaTitle: "Build System Configuration Schema"
44
description: "Schema exploration widget for the ReScript configuration file"
5-
canonical: "/docs/manual/v12.0.0/build-configuration-schema"
5+
canonical: "/docs/manual/build-configuration-schema"
66
section: "Build System"
77
order: 3
88
---

0 commit comments

Comments
 (0)