forked from cucapra/capra-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
173 lines (160 loc) · 4.38 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
var Metalsmith = require('metalsmith');
var markdown = require('metalsmith-markdown');
var layouts = require('metalsmith-layouts');
var sass = require('metalsmith-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 marked = require('marked');
var nunjucks = require('nunjucks');
var moment = require('moment');
var serveMode = process.argv.indexOf('--serve') != -1;
// Nunjucks options.
nunjucks.configure().addFilter('markdown', function (str) {
return marked(str, { smartypants: true });
}).addFilter('date', function (d, f) {
return moment(d).format(f);
}).addFilter('limit', function (array, limit) {
return array.slice(0, limit);
});
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(markdown({
smartypants: true,
}))
.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',
},
research: {
pattern: 'research/*/index.{md,html}',
metadata: {
name: 'Research',
},
},
news: {
pattern: 'news/*-*-*-*.{md,html}',
sortBy: 'date',
reverse: true,
metadata: {
layout: 'news.html',
},
},
}))
// News section.
.use(filemetadata([
{
pattern: 'news/*',
metadata: { layout: "news.html" },
}
]))
.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');
}
// 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 = new Buffer(body, 'utf8');
}
}
done();
})
// Research project listings.
.use(metadata({
research_ext: 'data/research.yaml',
}))
.use((files, metalsmith, done) => {
// Merge internal and external research.
metalsmith._metadata.projects =
metalsmith._metadata.research.concat(
metalsmith._metadata.research_ext);
// Sort by a numeric field.
metalsmith._metadata.projects.sort((a, b) => a.order - b.order);
done();
})
// 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',
}))
// People.
.use(metadata({
people: 'data/people.yaml',
}))
.use(inplace({
engine: "nunjucks",
pattern: "**/*.{html,md}"
}))
.use(components({
"componentsDirectory": "node_modules",
"components": {
"octicons": {
"build/svg/{mark-github,file-pdf,rss}.svg": "img/octicons/",
},
},
}))
.use(layouts('nunjucks'));
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;
});