-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
182 lines (167 loc) · 4.52 KB
/
index.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
var Metalsmith = require('metalsmith');
var markdown = require('metalsmith-markdown');
var layouts = require('metalsmith-layouts');
var sass = require('metalsmith-dart-sass');
var metadata = require('metalsmith-metadata');
var collections = require('metalsmith-collections');
var inplace = require('metalsmith-in-place');
var ignore = require('metalsmith-ignore');
var metadataIF = require('metalsmith-metadata-in-filename');
var filemetadata = require('metalsmith-filemetadata');
var components = require('metalsmith-components');
var jsonfeed = require('metalsmith-json-feed');
var feed = require('metalsmith-feed');
var bibtex = require('metalsmith-bibtex');
var mcopy = require('metalsmith-copy');
var marked = require('marked');
var nunjucks = require('nunjucks');
var moment = require('moment');
var serveMode = process.argv.indexOf('--serve') != -1;
var myFilters = {
'markdown': function (str) {
return marked(str.toString(), { smartypants: true });
},
'date': function (d, f) {
return moment(d).format(f);
},
'limit': function (array, limit) {
return array.slice(0, limit);
},
'fmtAuthor': function (author) {
return author.split(' and ').join(', ');
},
};
var site = Metalsmith(__dirname)
.source('./src')
.destination('./build')
.metadata({
serve: serveMode,
site: {
title: 'Cornell Capra',
url: 'https://capra.cs.cornell.edu/',
},
})
.use(ignore(['**/.DS_Store']))
.use(sass())
.use(metadataIF())
.use((files, metalsmith, done) => {
// Workaround:
// https://github.com/segmentio/metalsmith-collections/pull/48#issuecomment-246612758
metalsmith._metadata.collections = null;
metalsmith._metadata.pages = null;
done();
})
.use(collections({
pages: {
pattern: '{*,*/index}.{md,html}',
sortBy: 'order',
},
news: {
pattern: 'news/*-*-*-*.{md,html}',
sortBy: 'date',
reverse: true,
metadata: {
layout: 'news.html.njk',
},
},
}))
// News section.
.use(filemetadata([
{
pattern: 'news/*',
metadata: { layout: "news.html.njk" },
}
]))
.use((files, metalsmith, done) => {
for (let item of metalsmith._metadata.news) {
// News items just get the date as their title.
if (!item.kind || item.kind === 'news') {
item.title = moment(item.date).format('MMM DD, YYYY');
}
// Super hacky: save the original Markdown source for use after the
// "main" `metalsmith-markdown` phase.
item.mdsource = Buffer.from(item.contents);
// Blog post items get a body containing the title and author.
if (item.kind === 'post') {
let body = `<p>${item.author} published: <a href="${item.link}">` +
`${item.title}</a></p>`;
item.contents = Buffer.from(body, 'utf8');
}
}
done();
})
// People.
.use(metadata({
people: 'data/people.yaml',
}))
// Publications.
.use(mcopy({
pattern: '*.bib',
directory: 'pubs',
}))
.use(bibtex({
collections: { pubs: 'pubs.bib' },
default: 'pubs',
style: 'default',
keystyle: 'numbered',
sortBy: 'year',
reverseOrder: true,
}))
// Format in-place Nunjucks markup and Markdown.
.use(inplace({
"engineOptions": { "filters": myFilters },
}))
.use(markdown({
smartypants: true,
}))
// Add links to each item in `path` and `link`.
.use((files, metalsmith, done) => {
for (let filepath in files) {
if (files.hasOwnProperty(filepath)) {
let obj = files[filepath];
if (filepath.endsWith("/index.html")) {
filepath = filepath.slice(0, 0 - "index.html".length);
}
obj.path = filepath;
if (!obj.link) {
obj.link = '/' + filepath;
}
}
}
done();
})
// News feeds.
.use(jsonfeed({
collection: 'news',
}))
.use(feed({
collection: 'news',
}))
.use(components({
"componentsDirectory": "node_modules",
"components": {
"octicons": {
"build/svg/{mark-github,file-pdf,rss}.svg": "img/octicons/",
},
},
}))
.use(layouts({
"engineOptions": { "filters": myFilters },
}));
if (serveMode) {
var serve = require('metalsmith-serve');
var watch = require('metalsmith-watch');
site = site
.use(serve())
.use(watch({
paths: {
"${source}/**/*": true,
"layouts/**/*": "**/*.md",
"${source}/**/*.yaml": "**/*",
},
livereload: true
}));
}
site.build(function(err) {
if (err) throw err;
});