Skip to content

Commit 6dbbb70

Browse files
authored
Remove BlogData completely (#357)
Directly read the directories from FS
1 parent afdde56 commit 6dbbb70

File tree

8 files changed

+69
-200
lines changed

8 files changed

+69
-200
lines changed

pages/blogpost-guide.mdx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,7 @@ For example, let's create a new file:
2525
touch _blogposts/2020-01-30-my-new-blog-post.mdx
2626
```
2727

28-
### Map the file to a URL
29-
30-
Open the `src/BlogData.res` file and add the path to `data`:
31-
32-
```res
33-
let data = [
34-
// ...
35-
"2020-01-30-my-new-blog-post",
36-
// ...
37-
]
38-
```
39-
40-
Save your changes within and refresh your browser within [/blog](/blog). You should now see a warning for some malformed frontmatter data. Let's fix this!
28+
Refresh your browser within [/blog](/blog). You should now see a warning for some malformed frontmatter data. Let's fix this!
4129

4230
## Add Frontmatter Data
4331

scripts/test-hrefs.mjs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@ import path from "path";
1414
import fs from "fs";
1515
import urlModule from "url";
1616
import { URL } from 'url';
17-
import {data as blogIndex} from '../src/BlogData.mjs'
18-
import {blogPathToSlug} from '../src/common/BlogApi.mjs'
17+
import {getAllPosts, blogPathToSlug} from '../src/common/BlogApi.mjs'
1918

2019
const __dirname = new URL('.', import.meta.url).pathname;
2120

2221
const mapBlogFilePath = path => {
23-
const match = path.match(/\.\/_blogposts\/(.*)\.mdx/);
22+
const match = path.match(/\.\/_blogposts\/(.*\.mdx)/);
2423

2524
if (match) {
2625
let relPath = match[1];
27-
let path = blogIndex.find((path) => path === relPath);
28-
if (path != null) {
29-
return `./pages/blog/${blogPathToSlug(path)}`;
26+
let data = getAllPosts().find(({path}) => path === relPath);
27+
if (data != null) {
28+
return `./pages/blog/${blogPathToSlug(data.path)}`;
3029
}
3130
return path;
3231
}

src/BlogArticle.mjs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import * as Util from "./common/Util.mjs";
99
import * as React from "react";
1010
import * as BlogApi from "./common/BlogApi.mjs";
1111
import * as DateStr from "./common/DateStr.mjs";
12-
import * as BlogData from "./BlogData.mjs";
1312
import * as Markdown from "./components/Markdown.mjs";
1413
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
1514
import * as MainLayout from "./layouts/MainLayout.mjs";
@@ -111,7 +110,7 @@ function BlogArticle$BlogHeader(Props) {
111110

112111
function $$default(props) {
113112
var path = props.path;
114-
var module_ = require("../_blogposts/" + (path + ".mdx"));
113+
var module_ = require("../_blogposts/" + path);
115114
var archived = path.startsWith("archive/");
116115
var component = module_.default;
117116
var fm = BlogFrontmatter.decode(frontmatter(component));
@@ -194,10 +193,10 @@ function $$default(props) {
194193

195194
function getStaticProps(ctx) {
196195
var params = ctx.params;
197-
var slug = BlogData.data.find(function (path2) {
198-
return BlogApi.blogPathToSlug(path2) === params.slug;
196+
var match = BlogApi.getAllPosts(undefined).find(function (param) {
197+
return BlogApi.blogPathToSlug(param.path) === params.slug;
199198
});
200-
var path = slug !== undefined ? slug : params.slug;
199+
var path = match !== undefined ? match.path : params.slug;
201200
var props = {
202201
path: path
203202
};

src/BlogArticle.res

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ module BlogHeader = {
126126
let default = (props: props) => {
127127
let {path} = props
128128

129-
let module_ = BlogComponent.require("../_blogposts/" ++ (path ++ ".mdx"))
129+
let module_ = BlogComponent.require("../_blogposts/" ++ path)
130130

131131
let archived = Js.String2.startsWith(path, "archive/")
132132

@@ -225,11 +225,11 @@ let getStaticProps: Next.GetStaticProps.t<props, Params.t> = ctx => {
225225
open Next.GetStaticProps
226226
let {params} = ctx
227227

228-
let path = switch BlogData.data->Js.Array2.find(path2 =>
229-
BlogApi.blogPathToSlug(path2) == params.slug
228+
let path = switch BlogApi.getAllPosts()->Js.Array2.find(({path}) =>
229+
BlogApi.blogPathToSlug(path) == params.slug
230230
) {
231231
| None => params.slug
232-
| Some(slug) => slug
232+
| Some({path}) => path
233233
}
234234

235235
let props = {path: path}

src/BlogData.mjs

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/BlogData.res

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/common/BlogApi.mjs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,43 @@ import * as Fs from "fs";
44
import * as Path from "path";
55
import * as DateStr from "./DateStr.mjs";
66
import * as Process from "process";
7-
import * as BlogData from "../BlogData.mjs";
87
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
98
import * as Belt_Option from "rescript/lib/es6/belt_Option.js";
109
import * as Caml_option from "rescript/lib/es6/caml_option.js";
1110
import GrayMatter from "gray-matter";
1211
import * as BlogFrontmatter from "./BlogFrontmatter.mjs";
1312

1413
function blogPathToSlug(path) {
15-
return path.replace(/(archive\/)?\d\d\d\d-\d\d-\d\d-/, "");
14+
return path.replace(/^(archive\/)?\d\d\d\d-\d\d-\d\d-(.+)\.mdx$/, "$2");
1615
}
1716

1817
function getAllPosts(param) {
19-
var postsDirectory = Path.join(Process.cwd(), "./_blogposts");
20-
return Belt_Array.keepMap(BlogData.data, (function (path) {
21-
var fullPath = Path.join(postsDirectory, path + ".mdx");
22-
if (!Fs.existsSync(fullPath)) {
23-
return ;
24-
}
25-
var fileContents = Fs.readFileSync(fullPath, "utf8");
26-
var match = GrayMatter(fileContents);
27-
var archived = fullPath.includes("/archive/");
28-
return {
29-
content: match.content,
30-
path: path,
31-
archived: archived,
32-
frontmatter: match.data
33-
};
34-
}));
18+
var postsDirectory = Path.join(Process.cwd(), "_blogposts");
19+
var archivedPostsDirectory = Path.join(postsDirectory, "archive");
20+
var mdxFiles = function (dir) {
21+
return Fs.readdirSync(dir).filter(function (path) {
22+
return Path.extname(path) === ".mdx";
23+
});
24+
};
25+
var nonArchivedPosts = mdxFiles(postsDirectory).map(function (path) {
26+
var match = GrayMatter(Fs.readFileSync(Path.join(postsDirectory, path), "utf8"));
27+
return {
28+
content: match.content,
29+
path: path,
30+
archived: false,
31+
frontmatter: match.data
32+
};
33+
});
34+
var archivedPosts = mdxFiles(archivedPostsDirectory).map(function (path) {
35+
var match = GrayMatter(Fs.readFileSync(Path.join(archivedPostsDirectory, path), "utf8"));
36+
return {
37+
content: match.content,
38+
path: Path.join("archive", path),
39+
archived: true,
40+
frontmatter: match.data
41+
};
42+
});
43+
return nonArchivedPosts.concat(archivedPosts);
3544
}
3645

3746
function dateToUTCString(date) {

src/common/BlogApi.res

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,42 @@ type postData = {
4040
}
4141

4242
let blogPathToSlug = path => {
43-
path->Js.String2.replaceByRe(%re(`/(archive\/)?\d\d\d\d-\d\d-\d\d-/`), "")
43+
path->Js.String2.replaceByRe(%re(`/^(archive\/)?\d\d\d\d-\d\d-\d\d-(.+)\.mdx$/`), "$2")
4444
}
4545

46-
let getAllPosts = () => {
47-
let postsDirectory = Node.Path.join2(Node.Process.cwd(), "./_blogposts")
46+
@module("path") external extname: string => string = "extname"
4847

49-
BlogData.data->Belt.Array.keepMap((path) => {
50-
let fullPath = Node.Path.join2(postsDirectory, path ++ ".mdx")
48+
let getAllPosts = () => {
49+
let postsDirectory = Node.Path.join2(Node.Process.cwd(), "_blogposts")
50+
let archivedPostsDirectory = Node.Path.join2(postsDirectory, "archive")
5151

52-
if Node.Fs.existsSync(fullPath) {
53-
let fileContents = Node.Fs.readFileSync(fullPath, #utf8)
54-
let {GrayMatter.data: data, content} = GrayMatter.matter(fileContents)
52+
let mdxFiles = dir => {
53+
Node.Fs.readdirSync(dir)->Js.Array2.filter(path => extname(path) === ".mdx")
54+
}
5555

56-
// We currently derive the archived state from the directory hierarchy
57-
// TODO: We need to handle archived files differently later on
58-
let archived = Js.String2.includes(fullPath, "/archive/")
56+
let nonArchivedPosts = mdxFiles(postsDirectory)->Js.Array2.map(path => {
57+
let {GrayMatter.data: data, content} =
58+
Node.Path.join2(postsDirectory, path)->Node.Fs.readFileSync(#utf8)->GrayMatter.matter
59+
{
60+
path: path,
61+
content: content,
62+
frontmatter: data,
63+
archived: false,
64+
}
65+
})
5966

60-
Some({
61-
path: path,
62-
content: content,
63-
frontmatter: data,
64-
archived: archived,
65-
})
66-
} else {
67-
None
67+
let archivedPosts = mdxFiles(archivedPostsDirectory)->Js.Array2.map(path => {
68+
let {GrayMatter.data: data, content} =
69+
Node.Path.join2(archivedPostsDirectory, path)->Node.Fs.readFileSync(#utf8)->GrayMatter.matter
70+
{
71+
path: Node.Path.join2("archive", path),
72+
content: content,
73+
frontmatter: data,
74+
archived: true,
6875
}
6976
})
77+
78+
Js.Array2.concat(nonArchivedPosts, archivedPosts)
7079
}
7180

7281
module RssFeed = {

0 commit comments

Comments
 (0)