-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmetalsmith.js
127 lines (116 loc) · 3.49 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
'use strict';
var defaultsDeep = require('lodash.defaultsdeep');
var Metalsmith = require('metalsmith');
var markdown = require('metalsmith-markdown');
var layouts = require('metalsmith-layouts');
var permalinks = require('metalsmith-permalinks');
var collections = require('metalsmith-collections');
var debug = require('metalsmith-debug');
var dirHierarchy = require('metalsmith-directory-hierarchy');
var kebabCase = require('lodash.kebabcase');
module.exports = function (opts) {
// Reload config.
var config = require('./_config');
if (opts.env === 'staging') {
config = defaultsDeep({}, require('./_config-staging'), config);
} else if (opts.env === 'development') {
config = defaultsDeep({}, require('./_config-dev'), config);
}
return Metalsmith(__dirname)
.metadata({ site: config })
.source('./app')
.ignore(['assets', 'layouts', 'includes'])
.destination('./build')
.clean(true)
.use(originalFileURI('originalURI'))
.use(collections({
posts: {
pattern: 'posts/**/*.md',
sortBy: 'originalURI'
}
}))
.use(addPostSectionInfo())
.use(dirHierarchy({
name: 'hierarchy',
test: /posts\/(.)+/,
sortBy: 'title'
}))
.use(markdown())
.use(permalinks({
// original options would act as the keys of a `default` linkset.
pattern: ':title',
// each linkset defines a match, and any other desired option.
linksets: [{
match: { collection: 'posts' },
pattern: ':sectionSlug/:title'
}]
}))
.use(permalinkOverride())
.use(layouts({
engine: 'ejs',
directory: 'app/layouts'
}))
.use(debug());
};
/**
* Override the permalink if a permalink property is set in the frontmatter.
*/
function permalinkOverride () {
return function (files, metalsmith, done) {
setImmediate(done);
Object.keys(files).forEach(function (file) {
var data = files[file];
var permalink = data.permalink;
if (permalink) {
// remove leading slash, and trailing / + index.html if exists.
permalink = permalink.replace(/^\//, '').replace(/\/(index\.html)?$/, '');
// Store in path.
data.path = permalink;
// Complete file path. If at this point the permalink is empty means
// that we want the index file.
permalink = (permalink === '' ? '' : `${permalink}/`) + 'index.html';
delete files[file];
files[permalink] = data;
}
});
};
}
/**
* Add the section the post belongs to it.
* Adds:
* section: {
* key,
* label
* }
*/
function addPostSectionInfo (files, metalsmith, done) {
return function (files, metalsmith, done) {
setImmediate(done);
var sectionsMeta = metalsmith.metadata().site.sectionsMeta;
var sections = Object.keys(sectionsMeta);
// Add the section the post belongs to it.
Object.keys(files).forEach(function (file) {
sections.forEach(function (sec) {
if (file.match('^posts/' + sec + '/')) {
files[file].section = {
key: sec,
label: sectionsMeta[sec]
};
// Store section slug as highlevel prop to be used by the permalink.
files[file].sectionSlug = kebabCase(sectionsMeta[sec]);
}
});
});
};
}
/**
* Store the original file uri.
*/
function originalFileURI (uriKey) {
return function (files, metalsmith, done) {
setImmediate(done);
Object.keys(files).forEach(function (file) {
files[file][uriKey] = file;
});
};
}