Skip to content

Commit e64dbf3

Browse files
committed
Add new layout for the React docs, prepare work for more generic DocsLayout
1 parent 90893fe commit e64dbf3

File tree

9 files changed

+242
-20
lines changed

9 files changed

+242
-20
lines changed

common/App.re

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ let default = (props: props): React.element => {
110110
}
111111
| {base: [|"docs", "reason-compiler"|], version: Latest} =>
112112
<ReasonCompilerDocsLayout> content </ReasonCompilerDocsLayout>
113+
| {base: [|"docs", "react"|], version: Latest} =>
114+
<ReactDocsLayout frontmatter={component->frontmatter}>
115+
content
116+
</ReactDocsLayout>
113117
| {base: [|"docs", "gentype"|], version: Latest} =>
114118
<GenTypeDocsLayout> content </GenTypeDocsLayout>
115119
// common routes
@@ -123,12 +127,17 @@ let default = (props: props): React.element => {
123127
// to keep the frontmatter parsing etc in one place
124128
content
125129
| _ =>
130+
let title =
131+
switch (url) {
132+
| {base: [|"docs"|]} => Some("Overview | ReScript Documentation")
133+
| _ => None
134+
};
126135
<MainLayout>
127-
<Meta />
136+
<Meta ?title />
128137
<div className="flex justify-center">
129138
<div className="max-w-705 w-full"> content </div>
130139
</div>
131-
</MainLayout>
140+
</MainLayout>;
132141
}
133142
};
134143
};

components/VersionSelect.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let make =
1717
},
1818
);
1919
<select
20-
className="text-14 border border-fire inline-block rounded px-4 py-1 font-semibold "
20+
className="text-14 border border-fire inline-block rounded px-4 py-1 font-semibold "
2121
name="versionSelection"
2222
value=version
2323
onChange>

layouts/CommunityLayout.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ let make = (~components=Markdown.default, ~children) => {
6161

6262
let title = "Community";
6363

64-
<DocsLayout theme=`Reason components categories title ?activeToc breadcrumbs>
64+
<DocsLayout theme=`Reason components metaTitleCategory="ReScript Documentation" categories title ?activeToc breadcrumbs>
6565
children
6666
</DocsLayout>;
6767
};

layouts/DocsLayout.re

Lines changed: 103 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module Category = Sidebar.Category;
1111

1212
let makeBreadcrumbsFromPaths =
1313
(~basePath: string, paths: array(string)): list(Url.breadcrumb) => {
14-
Js.log(paths);
1514
let (_, rest) =
1615
Belt.Array.reduce(
1716
paths,
@@ -21,8 +20,7 @@ let makeBreadcrumbsFromPaths =
2120

2221
let href = baseHref ++ "/" ++ path;
2322

24-
Js.Array2.push(ret, Url.{name: prettyString(path), href})
25-
->ignore;
23+
Js.Array2.push(ret, Url.{name: prettyString(path), href})->ignore;
2624
(href, ret);
2725
},
2826
);
@@ -42,8 +40,7 @@ let makeBreadcrumbs =
4240

4341
let href = baseHref ++ "/" ++ path;
4442

45-
Js.Array2.push(ret,Url.{name: prettyString(path), href})
46-
->ignore;
43+
Js.Array2.push(ret, Url.{name: prettyString(path), href})->ignore;
4744
(href, ret);
4845
},
4946
);
@@ -55,6 +52,7 @@ let make =
5552
(
5653
~breadcrumbs: option(list(Url.breadcrumb))=?,
5754
~title: string,
55+
~metaTitleCategory: option(string)=?, // e.g. Introduction | My Meta Title Category
5856
~frontmatter: option(Js.Json.t)=?,
5957
~version: option(string)=?,
6058
~availableVersions: option(array(string))=?,
@@ -139,7 +137,12 @@ let make =
139137
route
140138
/>;
141139

142-
let metaTitle = title ++ " | ReScript Documentation";
140+
let metaTitle =
141+
switch (metaTitleCategory) {
142+
| Some(titleCategory) =>
143+
titleCategory ++ " | " ++ "ReScript Documentation"
144+
| None => title
145+
};
143146

144147
let metaElement =
145148
switch (frontmatter) {
@@ -148,7 +151,11 @@ let make =
148151
| Ok(fm) =>
149152
let canonical = Js.Null.toOption(fm.canonical);
150153
let description = Js.Null.toOption(fm.description);
151-
let title = fm.title ++ " | ReScript Language Manual";
154+
let title =
155+
switch (metaTitleCategory) {
156+
| Some(titleCategory) => fm.title ++ " | " ++ titleCategory
157+
| None => title
158+
};
152159
<Meta title ?description ?canonical />;
153160
| Error(_) => React.null
154161
}
@@ -166,3 +173,92 @@ let make =
166173
children
167174
</SidebarLayout>;
168175
};
176+
177+
module type StaticContent = {
178+
/*let categories: array(SidebarLayout.Sidebar.Category.t);*/
179+
let tocData: SidebarLayout.Toc.raw;
180+
};
181+
182+
module Make = (Content: StaticContent) => {
183+
[@react.component]
184+
let make =
185+
(
186+
~breadcrumbs: option(list(Url.breadcrumb))=?,
187+
~title: string,
188+
~metaTitleCategory: option(string)=?,
189+
~frontmatter: option(Js.Json.t)=?,
190+
~version: option(string)=?,
191+
~availableVersions: option(array(string))=?,
192+
~latestVersionLabel: option(string)=?,
193+
/*~activeToc: option(SidebarLayout.Toc.t)=?,*/
194+
~components: option(Mdx.Components.t)=?,
195+
~theme: option(ColorTheme.t)=?,
196+
~children: React.element,
197+
) => {
198+
let router = Next.Router.useRouter();
199+
let route = router.route;
200+
201+
let activeToc: option(SidebarLayout.Toc.t) =
202+
Belt.Option.(
203+
Js.Dict.get(Content.tocData, route)
204+
->map(data => {
205+
open SidebarLayout.Toc;
206+
let title = data##title;
207+
let entries =
208+
Belt.Array.map(data##headers, header =>
209+
{header: header##name, href: "#" ++ header##href}
210+
);
211+
{title, entries};
212+
})
213+
);
214+
215+
let categories = {
216+
let groups =
217+
Js.Dict.entries(Content.tocData)
218+
->Belt.Array.reduce(
219+
Js.Dict.empty(),
220+
(acc, next) => {
221+
let (_, value) = next;
222+
switch (Js.Nullable.toOption(value##category)) {
223+
| Some(category) =>
224+
switch (acc->Js.Dict.get(category)) {
225+
| None => acc->Js.Dict.set(category, [|next|])
226+
| Some(arr) =>
227+
Js.Array2.push(arr, next)->ignore;
228+
acc->Js.Dict.set(category, arr);
229+
}
230+
| None =>
231+
Js.log2("has NO category", next);
232+
()
233+
};
234+
acc;
235+
},
236+
);
237+
Js.Dict.entries(groups)
238+
->Belt.Array.map(((name, values)) => {
239+
Category.{
240+
name,
241+
items:
242+
Belt.Array.map(values, ((href, value)) => {
243+
{NavItem.name: value##title, href}
244+
}),
245+
}
246+
});
247+
};
248+
249+
make({
250+
"breadcrumbs": breadcrumbs,
251+
"title": title,
252+
"metaTitleCategory": metaTitleCategory,
253+
"frontmatter": frontmatter,
254+
"version": version,
255+
"availableVersions": availableVersions,
256+
"latestVersionLabel": latestVersionLabel,
257+
"activeToc": activeToc,
258+
"categories": categories,
259+
"components": components,
260+
"theme": theme,
261+
"children": children,
262+
});
263+
};
264+
};

layouts/ManualDocsLayout.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ module Docs = {
200200
categories
201201
version
202202
title
203+
metaTitleCategory="ReScript Language Manual"
203204
availableVersions=allManualVersions
204205
latestVersionLabel
205206
?frontmatter

layouts/ManualDocsLayout8_0_0.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ module Docs = {
220220
latestVersionLabel=ManualDocsLayout.latestVersionLabel
221221
?frontmatter
222222
title
223+
metaTitleCategory="ReScript Language Manual"
223224
?activeToc
224225
breadcrumbs>
225226
warnBanner

layouts/ReactDocsLayout.re

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
module Link = Next.Link;
2+
3+
module NavItem = SidebarLayout.Sidebar.NavItem;
4+
module Category = SidebarLayout.Sidebar.Category;
5+
module Toc = SidebarLayout.Toc;
6+
7+
let overviewNavs = [|
8+
{NavItem.name: "Introduction", href: "/docs/react/latest/introduction"},
9+
|];
10+
11+
module CategoryDocsLayout =
12+
// Structure defined by `scripts/extract-tocs.js`
13+
DocsLayout.Make({
14+
/*let categories = [|Category.{name: "Overview", items: overviewNavs}|];*/
15+
let tocData: SidebarLayout.Toc.raw = [%raw
16+
"require('../index_data/react_latest_toc.json')"
17+
];
18+
});
19+
20+
[@react.component]
21+
let make = (~frontmatter: option(Js.Json.t)=?, ~components=Markdown.default, ~children) => {
22+
let router = Next.Router.useRouter();
23+
let route = router.route;
24+
25+
let url = route->Url.parse;
26+
27+
let version =
28+
switch (url.version) {
29+
| Version(version) => version
30+
| NoVersion => "latest"
31+
| Latest => "latest"
32+
};
33+
34+
let prefix = [
35+
Url.{name: "Docs", href: "/docs/latest"},
36+
Url.{
37+
name: "ReasonReact",
38+
href: "/docs/react/" ++ version ++ "/introduction",
39+
},
40+
];
41+
42+
let breadcrumbs =
43+
Belt.List.concat(
44+
prefix,
45+
DocsLayout.makeBreadcrumbs(
46+
~basePath="/docs/gentype/" ++ version,
47+
route,
48+
),
49+
);
50+
51+
let title = "ReasonReact";
52+
53+
let availableVersions = [|"latest"|];
54+
let latestVersionLabel = "v0.9";
55+
let version = "latest";
56+
57+
<CategoryDocsLayout
58+
theme=`Reason
59+
components
60+
metaTitleCategory="React"
61+
availableVersions
62+
latestVersionLabel
63+
version
64+
title
65+
breadcrumbs
66+
?frontmatter>
67+
children
68+
</CategoryDocsLayout>;
69+
};

layouts/SidebarLayout.re

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ open Util.ReactStuff;
88
module Link = Next.Link;
99

1010
module Toc = {
11+
type raw =
12+
Js.Dict.t({
13+
.
14+
"title": string,
15+
"category": Js.Nullable.t(string),
16+
"headers":
17+
array({
18+
.
19+
"name": string,
20+
"href": string,
21+
}),
22+
});
23+
1124
type entry = {
1225
header: string,
1326
href: string,
@@ -123,7 +136,6 @@ module Sidebar = {
123136
};
124137
};
125138

126-
127139
// subitems: list of functions inside given module (defined by route)
128140
[@react.component]
129141
let make =

0 commit comments

Comments
 (0)