-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetalsmith.js
134 lines (115 loc) · 3.04 KB
/
metalsmith.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* eslint-disable import/no-extraneous-dependencies */
const Metalsmith = require("metalsmith");
const markdown = require("@metalsmith/markdown");
const layouts = require("@metalsmith/layouts");
const collections = require("@metalsmith/collections");
const drafts = require("@metalsmith/drafts");
const permalinks = require("@metalsmith/permalinks");
const when = require("metalsmith-if");
const htmlMinifier = require("metalsmith-html-minifier");
const assets = require("metalsmith-static-files");
const metadata = require("@metalsmith/metadata");
const prism = require("metalsmith-prism");
const rss = require("@metalsmith/rss/lib");
const marked = require("marked");
const { dependencies } = require("./package.json");
const isProduction = process.env.NODE_ENV === "production";
// functions to extend Nunjucks environment
const spaceToDash = (string) => string.replace(/\s+/g, "-");
const condenseTitle = (string) => string.toLowerCase().replace(/\s+/g, "");
const UTCdate = (date) => date.toUTCString("M d, yyyy");
const blogDate = (string) =>
new Date(string).toLocaleString("en-US", { year: "numeric", month: "long", day: "numeric" });
const trimSlashes = (string) => string.replace(/(^\/)|(\/$)/g, "");
const md = (mdString) => {
try {
return marked.parse(mdString);
} catch (e) {
return mdString;
}
};
// Define engine options for the inplace and layouts plugins
const templateConfig = {
directory: "layouts",
engineOptions: {
path: ["layouts"],
filters: {
spaceToDash,
condenseTitle,
UTCdate,
blogDate,
trimSlashes,
md,
},
},
};
function msBuild() {
Metalsmith(__dirname)
.source("./src/content")
.destination("./build")
.clean(true)
.metadata({
msVersion: dependencies.metalsmith,
nodeVersion: process.version,
})
.use(when(isProduction, drafts()))
.use(
metadata({
site: "src/content/data/site.json",
nav: "src/content/data/navigation.json",
})
)
.use(
collections({
posts: {
pattern: "posts/*.md",
sortBy: "date",
reverse: true,
limit: 10,
},
})
)
.use(markdown())
.use((files, _, done) => {
for (const file of Object.values(files)) {
if (file.collection && file.collection.length) {
file.url = file.path.replace(/\.md$/, "/");
file.title = file.blogTitle;
}
}
done();
})
.use(
rss({
feedOptions: {
title: "urbanists.video blogposts",
site_url: "https://blog.urbanists.video",
},
})
)
.use(permalinks())
.use(layouts(templateConfig))
.use(
prism({
lineNumbers: true,
decode: true,
})
)
.use(
assets({
source: "src/assets/",
destination: "assets/",
})
)
.use(when(isProduction, htmlMinifier()))
.build((err) => {
if (err) {
throw err;
}
});
}
if (require.main === module) {
msBuild();
} else {
module.exports = msBuild;
}