Skip to content

Commit bfc905a

Browse files
committed
Add frontmatter parsing support and enable frontmatter based Meta injection in v8.0.0 pages
1 parent 029abbb commit bfc905a

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

common/App.re

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ type props = {
4242
"pageProps": pageProps,
4343
};
4444

45+
[@bs.get] external frontmatter: React.component(Js.t({.})) => Js.Json.t = "frontmatter";
46+
4547
let default = (props: props): React.element => {
4648
let component = props##"Component";
4749
let pageProps = props##pageProps;
@@ -59,7 +61,7 @@ let default = (props: props): React.element => {
5961
| Latest =>
6062
<ManualDocsLayout.Prose> content </ManualDocsLayout.Prose>
6163
| Version("v8.0.0") =>
62-
<ManualDocsLayout8_0_0.Prose> content </ManualDocsLayout8_0_0.Prose>
64+
<ManualDocsLayout8_0_0.Prose frontmatter=component->frontmatter> content </ManualDocsLayout8_0_0.Prose>
6365
| _ =>
6466
React.null
6567
}

common/DocFrontmatter.re

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type t = {
2+
title: string,
3+
description: Js.null(string),
4+
canonical: Js.null(string),
5+
};
6+
7+
let decode = (json): result(t, string) => {
8+
open! Json.Decode;
9+
try(
10+
Ok({
11+
title: field("title", string, json),
12+
description:
13+
optional(field("description", string), json)->Js.Null.fromOption,
14+
canonical:
15+
optional(field("canonical", string), json)->Js.Null.fromOption,
16+
})
17+
) {
18+
| DecodeError(errMsg) => Error(errMsg)
19+
};
20+
};

layouts/DocsLayout.re

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ let make =
309309
(
310310
~breadcrumbs: option(list(UrlPath.breadcrumb))=?,
311311
~title: string,
312+
~frontmatter: option(Js.Json.t)=?,
312313
~version: option(string)=?,
313314
~activeToc: option(Toc.t)=?,
314315
~categories: array(Category.t),
@@ -368,13 +369,28 @@ let make =
368369

369370
let metaTitle = title ++ " | ReScript Documentation";
370371

372+
let metaElement =
373+
switch (frontmatter) {
374+
| Some(frontmatter) =>
375+
switch (DocFrontmatter.decode(frontmatter)) {
376+
| Ok(fm) =>
377+
let canonical = Js.Null.toOption(fm.canonical);
378+
let description = Js.Null.toOption(fm.description);
379+
let title = fm.title ++ " | ReScript Language Manual";
380+
<Meta title ?description ?canonical />;
381+
| Error(_) => React.null
382+
}
383+
| None => React.null
384+
};
385+
371386
<SidebarLayout
372387
metaTitle
373388
theme
374389
components
375390
sidebarState=(isSidebarOpen, setSidebarOpen)
376391
sidebar
377392
?breadcrumbs>
393+
metaElement
378394
children
379395
</SidebarLayout>;
380396
};

layouts/ManualDocsLayout8_0_0.re

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ let categories = [|
118118

119119
module Docs = {
120120
[@react.component]
121-
let make = (~components=Markdown.default, ~children) => {
121+
let make = (~frontmatter: option(Js.Json.t)=?, ~components=Markdown.default, ~children) => {
122122
let router = Next.Router.useRouter();
123123
let route = router.route;
124124

@@ -161,6 +161,7 @@ module Docs = {
161161
components
162162
categories
163163
version
164+
?frontmatter
164165
title
165166
?activeToc
166167
?breadcrumbs>
@@ -171,8 +172,8 @@ module Docs = {
171172

172173
module Prose = {
173174
[@react.component]
174-
let make = (~children) => {
175-
<Docs components=Markdown.default>
175+
let make = (~frontmatter: option(Js.Json.t)=?, ~children) => {
176+
<Docs ?frontmatter components=Markdown.default>
176177
children
177178
</Docs>;
178179
};

0 commit comments

Comments
 (0)