-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic.config.js
85 lines (78 loc) · 2.01 KB
/
static.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import path from "path";
import { readFileSync } from "fs";
import glob from "glob";
import cloudinary from "cloudinary-core";
const URL =
process.env.CONTEXT === "production"
? process.env.URL
: process.env.DEPLOY_URL;
const cl = new cloudinary.Cloudinary({
cloud_name: "benoitzohar",
secure: true
});
function replaceImages(content) {
return content.replace(/(?:!\[(.*?)\]\((.*?)\))/gim, function replacer(
_,
title,
filename
) {
// this will fallback gracefully if the filename is actually an image URL
const url = cl.url(filename, {
fetchFormat: "auto",
width: "IMG_WIDTH",
crop: "pad"
});
return `![${title}](${url})`;
});
}
export default {
siteRoot: URL,
getRoutes: async () => {
const files = glob.sync("./posts/*.md");
const posts = files.reverse().map(filepath => {
const filename = path.basename(filepath);
const date = new Date(filename.substr(0, 10));
const slug = filename.substr(11).replace(".md", "");
const content = replaceImages(String(readFileSync(filepath)) || "");
const rows = content.split("\n");
const title = rows.find(row => row.startsWith("# ")).replace("# ", "");
const description = rows
.find(row => row.startsWith("> "))
.replace("> ", "");
const url = `${URL}/blog/post/${slug}`;
return {
slug,
date,
title,
description,
content,
url
};
});
return [
{
path: "/blog",
getData: () => ({
posts
}),
children: posts.map(post => ({
path: `/post/${post.slug}`,
template: "src/containers/Post",
getData: () => ({
post
})
}))
}
];
},
plugins: [
[
require.resolve("react-static-plugin-source-filesystem"),
{
location: path.resolve("./src/pages")
}
],
require.resolve("react-static-plugin-reach-router"),
require.resolve("react-static-plugin-sitemap")
]
};