Skip to content

Commit f6678af

Browse files
authored
Sort blog posts more cleanly (#358)
Following #355, we can now sort blog posts just using their path, since the date string comes before and in the semantically correct comparison order by nature.
1 parent 6dbbb70 commit f6678af

File tree

4 files changed

+58
-97
lines changed

4 files changed

+58
-97
lines changed

src/Blog.mjs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as Curry from "rescript/lib/es6/curry.js";
99
import * as React from "react";
1010
import * as Button from "./components/Button.mjs";
1111
import * as Footer from "./components/Footer.mjs";
12+
import * as $$String from "rescript/lib/es6/string.js";
1213
import * as BlogApi from "./common/BlogApi.mjs";
1314
import * as DateStr from "./common/DateStr.mjs";
1415
import * as Markdown from "./components/Markdown.mjs";
@@ -169,19 +170,7 @@ function Blog$FeatureCard(Props) {
169170
})));
170171
}
171172

172-
function orderByDate(posts) {
173-
return posts.slice().sort(function (a, b) {
174-
var aV = DateStr.toDate(a.frontmatter.date).valueOf();
175-
var bV = DateStr.toDate(b.frontmatter.date).valueOf();
176-
if (aV === bV) {
177-
return 0;
178-
} else if (aV > bV) {
179-
return -1;
180-
} else {
181-
return 1;
182-
}
183-
});
184-
}
173+
var Post = {};
185174

186175
var Malformed = {};
187176

@@ -315,7 +304,9 @@ function $$default(props) {
315304
}
316305

317306
function getStaticProps(_ctx) {
318-
var match = Belt_Array.reduce(BlogApi.getAllPosts(undefined), [
307+
var match = Belt_Array.reduce(BlogApi.getAllPosts(undefined).sort(function (a, b) {
308+
return $$String.compare(b.path, a.path);
309+
}), [
319310
[],
320311
[],
321312
[]
@@ -356,8 +347,8 @@ function getStaticProps(_ctx) {
356347
archived
357348
];
358349
}));
359-
var props_posts = orderByDate(match[0]);
360-
var props_archived = orderByDate(match[2]);
350+
var props_posts = match[0];
351+
var props_archived = match[2];
361352
var props_malformed = match[1];
362353
var props = {
363354
posts: props_posts,
@@ -369,8 +360,6 @@ function getStaticProps(_ctx) {
369360
});
370361
}
371362

372-
var Post = {};
373-
374363
export {
375364
Post ,
376365
Malformed ,

src/Blog.res

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -212,21 +212,6 @@ module Post = {
212212
id: string,
213213
frontmatter: BlogFrontmatter.t,
214214
}
215-
216-
let orderByDate = (posts: array<t>): array<t> =>
217-
posts
218-
->Js.Array.copy
219-
->Js.Array2.sortInPlaceWith((a, b) => {
220-
let aV = a.frontmatter.date->DateStr.toDate->Js.Date.valueOf
221-
let bV = b.frontmatter.date->DateStr.toDate->Js.Date.valueOf
222-
if aV === bV {
223-
0
224-
} else if aV > bV {
225-
-1
226-
} else {
227-
1
228-
}
229-
})
230215
}
231216

232217
module Malformed = {
@@ -367,34 +352,36 @@ let default = (props: props): React.element => {
367352
}
368353

369354
let getStaticProps: Next.GetStaticProps.t<props, params> = _ctx => {
370-
let (posts, malformed, archived) = BlogApi.getAllPosts()->Belt.Array.reduce(([], [], []), (
371-
acc,
372-
postData,
373-
) => {
374-
let (posts, malformed, archived) = acc
375-
let id = BlogApi.blogPathToSlug(postData.path)
376-
377-
let decoded = BlogFrontmatter.decode(postData.frontmatter)
378-
379-
switch decoded {
380-
| Error(message) =>
381-
let m = {Malformed.id: id, message: message}
382-
let malformed = Belt.Array.concat(malformed, [m])
383-
(posts, malformed, archived)
384-
| Ok(frontmatter) =>
385-
if postData.archived {
386-
Js.Array2.push(archived, {Post.id: id, frontmatter: frontmatter})->ignore
387-
} else {
388-
Js.Array2.push(posts, {Post.id: id, frontmatter: frontmatter})->ignore
355+
let (posts, malformed, archived) =
356+
BlogApi.getAllPosts()
357+
->Js.Array2.sortInPlaceWith((a, b) => {
358+
String.compare(b.path, a.path)
359+
})
360+
->Belt.Array.reduce(([], [], []), (acc, postData) => {
361+
let (posts, malformed, archived) = acc
362+
let id = BlogApi.blogPathToSlug(postData.path)
363+
364+
let decoded = BlogFrontmatter.decode(postData.frontmatter)
365+
366+
switch decoded {
367+
| Error(message) =>
368+
let m = {Malformed.id: id, message: message}
369+
let malformed = Belt.Array.concat(malformed, [m])
370+
(posts, malformed, archived)
371+
| Ok(frontmatter) =>
372+
if postData.archived {
373+
Js.Array2.push(archived, {Post.id: id, frontmatter: frontmatter})->ignore
374+
} else {
375+
Js.Array2.push(posts, {Post.id: id, frontmatter: frontmatter})->ignore
376+
}
377+
(posts, malformed, archived)
389378
}
390-
(posts, malformed, archived)
391-
}
392-
})
379+
})
393380

394381
let props = {
395-
posts: Post.orderByDate(posts),
382+
posts: posts,
396383
malformed: malformed,
397-
archived: Post.orderByDate(archived),
384+
archived: archived,
398385
}
399386

400387
Js.Promise.resolve({"props": props})

src/common/BlogApi.mjs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import * as Fs from "fs";
44
import * as Path from "path";
5+
import * as $$String from "rescript/lib/es6/string.js";
56
import * as DateStr from "./DateStr.mjs";
67
import * as Process from "process";
78
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
@@ -51,34 +52,26 @@ function dateToUTCString(date) {
5152
function getLatest(maxOpt, baseUrlOpt, param) {
5253
var max = maxOpt !== undefined ? maxOpt : 10;
5354
var baseUrl = baseUrlOpt !== undefined ? baseUrlOpt : "https://rescript-lang.org";
54-
return Belt_Array.reduce(getAllPosts(undefined), [], (function (acc, next) {
55-
var fm = BlogFrontmatter.decode(next.frontmatter);
56-
if (fm.TAG !== /* Ok */0) {
57-
return acc;
58-
}
59-
var fm$1 = fm._0;
60-
var description = Belt_Option.getWithDefault(Caml_option.null_to_opt(fm$1.description), "");
61-
var item_title = fm$1.title;
62-
var item_href = baseUrl + ("/blog/" + blogPathToSlug(next.path));
63-
var item_pubDate = DateStr.toDate(fm$1.date);
64-
var item = {
65-
title: item_title,
66-
href: item_href,
67-
description: description,
68-
pubDate: item_pubDate
69-
};
70-
return Belt_Array.concat(acc, [item]);
71-
})).sort(function (item1, item2) {
72-
var v1 = item1.pubDate.valueOf();
73-
var v2 = item2.pubDate.valueOf();
74-
if (v1 === v2) {
75-
return 0;
76-
} else if (v1 > v2) {
77-
return -1;
78-
} else {
79-
return 1;
80-
}
81-
}).slice(0, max);
55+
return Belt_Array.reduce(getAllPosts(undefined).sort(function (a, b) {
56+
return $$String.compare(Path.basename(b.path), Path.basename(a.path));
57+
}), [], (function (acc, next) {
58+
var fm = BlogFrontmatter.decode(next.frontmatter);
59+
if (fm.TAG !== /* Ok */0) {
60+
return acc;
61+
}
62+
var fm$1 = fm._0;
63+
var description = Belt_Option.getWithDefault(Caml_option.null_to_opt(fm$1.description), "");
64+
var item_title = fm$1.title;
65+
var item_href = baseUrl + "/blog/" + blogPathToSlug(next.path);
66+
var item_pubDate = DateStr.toDate(fm$1.date);
67+
var item = {
68+
title: item_title,
69+
href: item_href,
70+
description: description,
71+
pubDate: item_pubDate
72+
};
73+
return Belt_Array.concat(acc, [item]);
74+
})).slice(0, max);
8275
}
8376

8477
function toXmlString(siteTitleOpt, siteDescriptionOpt, items) {

src/common/BlogApi.res

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,16 @@ module RssFeed = {
110110
let getLatest = (~max=10, ~baseUrl="https://rescript-lang.org", ()): array<item> => {
111111
let items =
112112
getAllPosts()
113+
->Js.Array2.sortInPlaceWith((a, b) => {
114+
String.compare(Node.Path.basename(b.path), Node.Path.basename(a.path))
115+
})
113116
->Belt.Array.reduce([], (acc, next) =>
114117
switch BlogFrontmatter.decode(next.frontmatter) {
115118
| Ok(fm) =>
116119
let description = Js.Null.toOption(fm.description)->Belt.Option.getWithDefault("")
117120
let item = {
118121
title: fm.title,
119-
href: baseUrl ++ ("/blog/" ++ blogPathToSlug(next.path)),
122+
href: baseUrl ++ "/blog/" ++ blogPathToSlug(next.path),
120123
description: description,
121124
pubDate: DateStr.toDate(fm.date),
122125
}
@@ -125,17 +128,6 @@ module RssFeed = {
125128
| Error(_) => acc
126129
}
127130
)
128-
->Js.Array2.sortInPlaceWith((item1, item2) => {
129-
let v1 = item1.pubDate->Js.Date.valueOf
130-
let v2 = item2.pubDate->Js.Date.valueOf
131-
if v1 === v2 {
132-
0
133-
} else if v1 > v2 {
134-
-1
135-
} else {
136-
1
137-
}
138-
})
139131
->Js.Array2.slice(~start=0, ~end_=max)
140132
items
141133
}

0 commit comments

Comments
 (0)