Skip to content

Commit eed9347

Browse files
committed
blog post is rendering
1 parent a599198 commit eed9347

File tree

13 files changed

+214
-295
lines changed

13 files changed

+214
-295
lines changed

_blogposts/2022-08-25-release-10-0-0.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
author: rescript-team
33
date: "2022-08-25"
4-
previewImg: static/blog/grid_0.jpeg
4+
previewImg: /blog/grid_0.jpeg
55
title: ReScript 10.0
66
badge: release
77
description: |

app/routes.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
open ReactRouter.Routes
2-
open ReactRouter.Mdx
2+
open Mdx
33

44
let stdlibPaths = {
55
let rawFile = await Node.Fs.readFile("./docs/api/stdlib.json", "utf-8")
@@ -30,5 +30,5 @@ let default = [
3030
route("docs/manual/api/belt", "./routes/ApiRoute.mjs", ~options={id: "api-belt"}),
3131
route("docs/manual/api/dom", "./routes/ApiRoute.mjs", ~options={id: "api-dom"}),
3232
...stdlibRoutes,
33-
...routes("./routes/MdxRoute.mjs"),
33+
...mdxRoutes("./routes/MdxRoute.mjs"),
3434
]

app/routes/BlogRoute.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
type loaderData = {posts: array<BlogApi.post>, category: Blog.category}
22

33
let loader: ReactRouter.Loader.t<loaderData> = async ({request}) => {
4-
let posts: array<BlogApi.post> = BlogLoader.posts()
4+
let posts: array<BlogApi.post> = MdxRoute.posts()
55
let data = {posts, category: All}
66
data
77
}

app/routes/MdxRoute.res

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type loaderData = {
55
...Mdx.t,
66
categories: array<SidebarLayout.Sidebar.Category.t>,
77
entries: array<TableOfContents.entry>,
8+
blogPost?: BlogApi.post,
89
}
910

1011
/**
@@ -43,30 +44,6 @@ let components = {
4344
"Warn": Markdown.Warn.make,
4445
}
4546

46-
// The loadAllMdx function logs out all of the file contents as it reads them, which is noisy and not useful.
47-
// We can suppress that logging with this helper function.
48-
let allMdx = await Shims.runWithoutLogging(() => loadAllMdx())
49-
50-
let sortSection = mdxPages =>
51-
Array.toSorted(mdxPages, (a: Mdx.attributes, b: Mdx.attributes) =>
52-
switch (a.order, b.order) {
53-
| (Some(a), Some(b)) => a > b ? 1.0 : -1.0
54-
| _ => -1.0
55-
}
56-
)
57-
58-
let groupBySection = mdxPages =>
59-
Array.reduce(mdxPages, (Dict.make() :> Dict.t<array<Mdx.attributes>>), (acc, item) => {
60-
let section = item.section->Option.flatMap(Dict.get(acc, _))
61-
switch section {
62-
// If the section already exists, add this item to it
63-
| Some(section) => section->Array.push(item)
64-
// otherwise create a new section with this item
65-
| None => item.section->Option.forEach(section => acc->Dict.set(section, [item]))
66-
}
67-
acc
68-
})
69-
7047
let convertToNavItems = (items, rootPath) =>
7148
Array.map(items, (item): SidebarLayout.Sidebar.NavItem.t => {
7249
{
@@ -75,9 +52,6 @@ let convertToNavItems = (items, rootPath) =>
7552
}
7653
})
7754

78-
let filterMdxPages = (mdxPages, path) =>
79-
Array.filter(mdxPages, mdx => (mdx.path :> string)->String.includes(path))
80-
8155
let getGroup = (groups, groupName): SidebarLayout.Sidebar.Category.t => {
8256
{
8357
name: groupName,
@@ -134,6 +108,8 @@ let reactTableOfContents = () => {
134108
categories
135109
}
136110

111+
let posts = () => allMdx->filterMdxPages("blog")->Array.map(BlogLoader.transform)
112+
137113
let loader: Loader.t<loaderData> = async ({request}) => {
138114
let {pathname} = WebAPI.URL.make(~url=request.url)
139115

@@ -143,13 +119,12 @@ let loader: Loader.t<loaderData> = async ({request}) => {
143119
if pathname->String.includes("blog") {
144120
let posts = blogPosts()
145121

146-
Console.log2("posts", posts)
147-
148122
let res: loaderData = {
149123
__raw: mdx.__raw,
150124
attributes: mdx.attributes,
151125
entries: [],
152126
categories: [],
127+
blogPost: mdx.attributes->BlogLoader.transform,
153128
}
154129

155130
res
@@ -207,8 +182,11 @@ let default = () => {
207182
let component = useMdxComponent(~components)
208183
let attributes = useMdxAttributes()
209184

210-
let {categories, entries} = useLoaderData()
185+
let loaderData: loaderData = useLoaderData()
186+
187+
let {entries, categories} = loaderData
211188

189+
// TODO: get actual meta categories working
212190
let metaTitleCategory =
213191
(pathname :> string)->String.includes("docs/manual")
214192
? "ReScript Language Manual"
@@ -226,8 +204,13 @@ let default = () => {
226204
<div className="markdown-body"> {component()} </div>
227205
</DocsLayout>
228206
} else {
229-
// TODO Handle blog pages
230-
React.null
207+
switch loaderData.blogPost {
208+
| Some({frontmatter, archived, path}) => {
209+
Console.log(frontmatter)
210+
<BlogArticle frontmatter isArchived=archived path> {component()} </BlogArticle>
211+
}
212+
| None => React.null // TODO: RR7 show an error
213+
}
231214
}}
232215
</>
233216

src/ApiDocs.res

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,6 @@ let processStaticProps = (~slug: array<string>) => {
440440

441441
let _content = content
442442

443-
// Console.log(content)
444-
445443
switch content {
446444
| Some(json) =>
447445
let {items, docstrings, deprecated, name} = Docgen.decodeFromJson(json)

src/BlogArticle.res

Lines changed: 14 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ module Params = {
2323
type t = {slug: string}
2424
}
2525

26-
type props = {mdxSource: MdxRemote.output, isArchived: bool, path: string}
26+
type props = {
27+
children: React.element,
28+
isArchived: bool,
29+
path: string,
30+
frontmatter: BlogFrontmatter.t,
31+
}
2732

2833
module Line = {
2934
@react.component
@@ -69,6 +74,8 @@ module BlogHeader = {
6974

7075
let authors = Array.concat([author], co_authors)
7176

77+
Console.log2("authors", authors)
78+
7279
<div className="flex flex-col items-center">
7380
<div className="w-full max-w-740">
7481
<div className="text-gray-60 body-sm mb-5">
@@ -114,18 +121,8 @@ module BlogHeader = {
114121
}
115122
}
116123

117-
let default = (props: props) => {
118-
let {mdxSource, isArchived, path} = props
119-
120-
let children =
121-
<MdxRemote
122-
frontmatter={mdxSource.frontmatter}
123-
compiledSource={mdxSource.compiledSource}
124-
scope={mdxSource.scope}
125-
components={MarkdownComponents.default}
126-
/>
127-
128-
let fm = mdxSource.frontmatter->BlogFrontmatter.decode
124+
let make = (props: props) => {
125+
let {children, isArchived, path, frontmatter} = props
129126

130127
let archivedNote = isArchived
131128
? {
@@ -143,8 +140,9 @@ let default = (props: props) => {
143140
}
144141
: React.null
145142

146-
let content = switch fm {
147-
| Ok({date, author, co_authors, title, description, articleImg, previewImg}) =>
143+
let {date, author, co_authors, title, description, articleImg, previewImg} = frontmatter
144+
145+
<MainLayout>
148146
<div className="w-full">
149147
<Meta
150148
siteName="ReScript Blog"
@@ -182,60 +180,5 @@ let default = (props: props) => {
182180
</div>
183181
</div>
184182
</div>
185-
186-
| Error(msg) =>
187-
<div>
188-
<Markdown.Warn>
189-
<h2 className="font-bold text-gray-80 text-24 mb-2">
190-
{React.string("Could not parse file '_blogposts/" ++ (path ++ ".mdx'"))}
191-
</h2>
192-
<p>
193-
{React.string("The content of this blog post will be displayed as soon as all
194-
required frontmatter data has been added.")}
195-
</p>
196-
<p className="font-bold mt-4"> {React.string("Errors:")} </p>
197-
{React.string(msg)}
198-
</Markdown.Warn>
199-
</div>
200-
}
201-
<MainLayout> content </MainLayout>
202-
}
203-
204-
let getStaticProps: Next.GetStaticProps.t<props, Params.t> = async ctx => {
205-
open Next.GetStaticProps
206-
let {params} = ctx
207-
208-
let path = switch BlogApi.getAllPosts()->Array.find(({path}) =>
209-
BlogApi.blogPathToSlug(path) == params.slug
210-
) {
211-
| None => params.slug
212-
| Some({path}) => path
213-
}
214-
215-
let filePath = Node.Path.resolve("_blogposts", path)
216-
217-
let isArchived = String.startsWith(path, "archive/")
218-
219-
let source = filePath->Node.Fs.readFileSync
220-
221-
let mdxSource = await MdxRemote.serialize(
222-
source,
223-
{parseFrontmatter: true, mdxOptions: MdxRemote.defaultMdxOptions},
224-
)
225-
226-
let props = {mdxSource, isArchived, path}
227-
228-
{"props": props}
229-
}
230-
231-
let getStaticPaths: Next.GetStaticPaths.t<Params.t> = async () => {
232-
open Next.GetStaticPaths
233-
234-
let paths = BlogApi.getAllPosts()->Array.map(postData => {
235-
params: {
236-
Params.slug: BlogApi.blogPathToSlug(postData.path),
237-
},
238-
})
239-
240-
{paths, fallback: false}
183+
</MainLayout>
241184
}

src/BlogArticle.resi

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ module Params: {
22
type t = {slug: string}
33
}
44

5-
type props = {mdxSource: MdxRemote.output, isArchived: bool, path: string}
6-
7-
let default: props => React.element
5+
type props = {
6+
children: React.element,
7+
isArchived: bool,
8+
path: string,
9+
frontmatter: BlogFrontmatter.t,
10+
}
811

9-
let getStaticProps: Next.GetStaticProps.t<props, Params.t>
10-
let getStaticPaths: Next.GetStaticPaths.t<Params.t>
12+
let make: props => React.element

src/BlogLoader.res

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
let transform = (mdx: ReactRouter.Mdx.attributes): BlogApi.post => {
1+
let transform = (mdx: Mdx.attributes): BlogApi.post => {
22
let archived = switch mdx.archived {
33
| Nullable.Value(archived) => archived
44
| _ => false
55
}
6-
Console.log(mdx)
76
{
8-
path: mdx.path,
7+
path: `${archived ? "archived/" : ""}${mdx.slug}`,
98
archived,
109
frontmatter: {
1110
author: BlogFrontmatter.authors
1211
->Array.find(author => author.username->String.includes(mdx.author))
1312
->Option.getOrThrow, // TODO: this is probably unsafe and needs to be fixed
14-
co_authors: (mdx.co_authors :> array<BlogFrontmatter.author>),
13+
co_authors: (mdx.co_authors->Nullable.getOr([]) :> array<BlogFrontmatter.author>),
1514
date: mdx.date,
1615
previewImg: mdx.previewImg,
1716
articleImg: mdx.articleImg,
1817
title: mdx.title,
1918
badge: mdx.badge->Nullable.map(badge => BlogFrontmatter.decodeBadge(badge)),
2019
description: mdx.description,
21-
slug: `/blog${archived ? "/archived" : ""}/${mdx.slug}`,
2220
},
2321
}
2422
}
25-
26-
let posts = () => MdxRoute.allMdx->MdxRoute.filterMdxPages("blog")->Array.map(transform)

0 commit comments

Comments
 (0)