Skip to content

Commit a41b05a

Browse files
authored
Merge pull request #95 from reason-association/improve-layouting
Extracted infra changes from the new React docs working branch
2 parents bf4f3e7 + 6c77b95 commit a41b05a

File tree

12 files changed

+182
-72
lines changed

12 files changed

+182
-72
lines changed

common/App.re

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
let res = require('../plugins/res-syntax-highlightjs');
1919
let bash = require('highlight.js/lib/languages/bash');
2020
let json = require('highlight.js/lib/languages/json');
21+
let html = require('highlight.js/lib/languages/xml');
2122
let ts = require('highlight.js/lib/languages/typescript');
2223
let text = require('highlight.js/lib/languages/plaintext');
2324
let diff = require('highlight.js/lib/languages/diff');
@@ -30,6 +31,7 @@
3031
hljs.registerLanguage('sh', bash);
3132
hljs.registerLanguage('json', json);
3233
hljs.registerLanguage('text', text);
34+
hljs.registerLanguage('html', html);
3335
hljs.registerLanguage('diff', diff);
3436
|};
3537

@@ -123,12 +125,17 @@ let default = (props: props): React.element => {
123125
// to keep the frontmatter parsing etc in one place
124126
content
125127
| _ =>
128+
let title =
129+
switch (url) {
130+
| {base: [|"docs"|]} => Some("Overview | ReScript Documentation")
131+
| _ => None
132+
};
126133
<MainLayout>
127-
<Meta />
134+
<Meta ?title />
128135
<div className="flex justify-center">
129136
<div className="max-w-705 w-full"> content </div>
130137
</div>
131-
</MainLayout>
138+
</MainLayout>;
132139
}
133140
};
134141
};

common/HighlightJs.re

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ let renderHLJS =
2525
Js.String2.split(highlighted, "\n")
2626
->Belt.Array.mapWithIndex((i, line) =>
2727
if (Js.Array2.find(highlightedLines, lnum => lnum === i + 1) !== None) {
28-
"<span class=\"hljs-line-highlight\">" ++ line ++ "</span>";
28+
let content = line === "" ? "&nbsp;" : line;
29+
"<span class=\"inline-block\">" ++ content ++ "</span>";
2930
} else {
30-
line;
31+
"<span class=\"inline-block text-inherit opacity-50\">"
32+
++ line
33+
++ "</span>";
3134
}
3235
)
3336
->Js.Array2.joinWith("\n");
@@ -37,15 +40,8 @@ let renderHLJS =
3740

3841
let dark = darkmode ? "dark" : "";
3942

40-
ReactDOMRe.createElementVariadic(
41-
"code",
42-
~props=
43-
ReactDOMRe.objToDOMProps({
44-
"className": "hljs lang-" ++ lang ++ " " ++ dark,
45-
"dangerouslySetInnerHTML": {
46-
"__html": highlighted,
47-
},
48-
}),
49-
[||],
50-
);
43+
<code
44+
className={"hljs lang-" ++ lang ++ " " ++ dark}
45+
dangerouslySetInnerHTML={"__html": highlighted}
46+
/>;
5147
};

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/SidebarLayout.re

Lines changed: 23 additions & 19 deletions
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,
@@ -20,25 +33,16 @@ module Toc = {
2033

2134
[@react.component]
2235
let make = (~entries: array(entry)) => {
23-
let router = Next.Router.useRouter();
2436
<ul className="mt-2 py-1 mb-4 border-l border-primary">
25-
{Belt.Array.map(
26-
entries,
27-
({header, href}) => {
28-
let isActive = router.pathname ++ href == router.asPath;
29-
let activeClassName = isActive ? "text-fire" : "";
30-
<li key=header className="pl-2 mt-3 first:mt-1">
31-
<Link href>
32-
<a
33-
className={
34-
"font-medium block text-sm text-night-light leading-tight tracking-tight hover:text-primary "
35-
++ activeClassName
36-
}>
37-
header->s
38-
</a>
39-
</Link>
40-
</li>;
41-
},
37+
{Belt.Array.map(entries, ({header, href}) =>
38+
<li key=header className="pl-2 mt-3 first:mt-1">
39+
<Link href>
40+
<a
41+
className="font-medium block text-sm text-night-light leading-tight tracking-tight hover:text-primary">
42+
header->s
43+
</a>
44+
</Link>
45+
</li>
4246
)
4347
->ate}
4448
</ul>;
@@ -324,4 +328,4 @@ let make =
324328
</div>
325329
</div>
326330
</>;
327-
};
331+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"react": "^16.12.0",
2828
"react-dom": "^16.12.0",
2929
"reason-promise": "^1.0.2",
30-
"reason-react": "^0.7.0",
30+
"reason-react": "^0.9.1",
3131
"remark-parse": "^7.0.1",
3232
"remark-slug": "^5.1.2",
3333
"remark-stringify": "^7.0.3",

0 commit comments

Comments
 (0)