forked from craigbuckler/metalsmith-demo
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbuild.js
136 lines (122 loc) · 3.95 KB
/
build.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
#!/usr/bin/env node
/*
Metalsmith build file
Build site with `node ./build.js` or `npm start`
Build production site with `npm run production`
*/
'use strict';
var
// defaults
consoleLog = false, // set true for metalsmith file and meta content logging
devBuild = ((process.env.NODE_ENV || '').trim().toLowerCase() !== 'production'),
pkg = require('./package.json'),
// main directories
dir = {
base: __dirname + '/',
lib: __dirname + '/lib/',
source: './src/',
dest: './build/'
},
// modules
metalsmith = require('metalsmith'),
markdown = require('metalsmith-markdown'),
publish = require('metalsmith-publish'),
wordcount = require("metalsmith-word-count"),
collections = require('metalsmith-collections'),
permalinks = require('metalsmith-permalinks'),
inplace = require('metalsmith-in-place'),
layouts = require('metalsmith-layouts'),
sitemap = require('metalsmith-mapsite'),
rssfeed = require('metalsmith-feed'),
assets = require('metalsmith-assets'),
htmlmin = devBuild ? null : require('metalsmith-html-minifier'),
browsersync = devBuild ? require('metalsmith-browser-sync') : null,
// custom plugins
setdate = require(dir.lib + 'metalsmith-setdate'),
moremeta = require(dir.lib + 'metalsmith-moremeta'),
debug = consoleLog ? require(dir.lib + 'metalsmith-debug') : null,
siteMeta = {
devBuild: devBuild,
version: pkg.version,
name: 'Static site',
desc: 'A demonstration static site built using Metalsmith',
author: 'Craig Buckler',
contact: 'https://twitter.com/craigbuckler',
domain: devBuild ? 'http://127.0.0.1' : 'https://rawgit.com', // set domain
rootpath: devBuild ? null : '/craigbuckler/metalsmith-demo/master/build/' // set absolute path (null for relative)
},
templateConfig = {
engine: 'handlebars',
directory: dir.source + 'template/',
partials: dir.source + 'partials/',
default: 'page.html'
};
console.log((devBuild ? 'Development' : 'Production'), 'build, version', pkg.version);
var ms = metalsmith(dir.base)
.clean(!devBuild) // clean folder before a production build
.source(dir.source + 'html/') // source folder (src/html/)
.destination(dir.dest) // build folder (build/)
.metadata(siteMeta) // add meta data to every page
.use(publish()) // draft, private, future-dated
.use(setdate()) // set date on every page if not set in front-matter
.use(collections({ // determine page collection/taxonomy
page: {
pattern: '**/index.*',
sortBy: 'priority',
reverse: true,
refer: false
},
start: {
pattern: 'start/**/*',
sortBy: 'priority',
reverse: true,
refer: true,
metadata: {
layout: 'article.html'
}
},
article: {
pattern: 'article/**/*',
sortBy: 'date',
reverse: true,
refer: true,
limit: 50,
metadata: {
layout: 'article.html'
}
}
}))
.use(markdown()) // convert markdown
.use(permalinks({ // generate permalinks
pattern: ':mainCollection/:title'
}))
.use(wordcount({
raw: true
})) // word count
.use(moremeta()) // determine root paths and navigation
.use(inplace(templateConfig)) // in-page templating
.use(layouts(templateConfig)); // layout templating
if (htmlmin) ms.use(htmlmin()); // minify production HTML
if (debug) ms.use(debug()); // output page debugging information
if (browsersync) ms.use(browsersync({ // start test server
server: dir.dest,
files: [dir.source + '**/*']
}));
ms
.use(sitemap({ // generate sitemap.xml
hostname: siteMeta.domain + (siteMeta.rootpath || ''),
omitIndex: true
}))
.use(rssfeed({ // generate RSS feed for articles
collection: 'article',
site_url: siteMeta.domain + (siteMeta.rootpath || ''),
title: siteMeta.name,
description: siteMeta.desc
}))
.use(assets({ // copy assets: CSS, images etc.
source: dir.source + 'assets/',
destination: './'
}))
.build(function(err) { // build
if (err) throw err;
});