forked from dcos/dcos-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
278 lines (257 loc) · 7.11 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
'use strict';
const Metalsmith = require('metalsmith')
const jade = require('metalsmith-jade')
const sass = require('metalsmith-sass')
const browserSync = require('metalsmith-browser-sync')
const markdown = require('metalsmith-markdown')
const layouts = require('metalsmith-layouts')
const permalinks = require('metalsmith-permalinks')
const babel = require('metalsmith-babel')
const bourbon = require('node-bourbon')
const path = require('path')
const postcss = require('metalsmith-postcss')
const autoprefixer = require('autoprefixer')
const copy = require('metalsmith-copy')
const each = require('metalsmith-each')
const navigation = require('metalsmith-navigation')
const modRewrite = require('connect-modrewrite')
const uglify = require('metalsmith-uglify')
const define = require('metalsmith-define')
const collections = require('metalsmith-collections')
const logger = require('metalsmith-logger')
const writemetadata = require('metalsmith-writemetadata')
const moment = require('moment')
const tags = require('metalsmith-tags')
const mlunr = require('metalsmith-lunr')
const CONFIG = require('./env.json')[process.env.NODE_ENV] || require('./env.json')['production']
// --- general build settings --- //
const docsVersions = ['1.7', '1.8', '1.9'];
const cssTimestamp = new Date().getTime()
const updatePaths = function(file, filename) {
if (path.basename(filename) === "index.html" ) { return filename; }
if (path.extname(filename) === '.html' &&
path.extname(filename) !== '') {
return filename.split(".html")[0] + "/index.html";
}
return filename;
};
// --------- figuring out the navigation ---------- //
const navConfig = {
header: {
includeDirs: true,
pathProperty: 'nav_path',
childrenProperty: 'nav_children',
sortBy: function(file, node) {
if (file !== undefined) {
if (file.menu_order !== undefined) {
return file.menu_order
}
} else if (node.type === 'dir') {
// for directories find the index.html and grab its menu_order
let indexFile = node.children.find((c) => c.name == 'index.html')
if (indexFile !== undefined) {
if (indexFile.file.menu_order !== undefined) {
return indexFile.file.menu_order
}
}
}
return 999
},
sortByNameFirst: true
}
}
let createDocsJSON = function(obj) {
var newObj = {
name: obj.type,
path: obj.path
};
if(obj.file) {
newObj.file = {
post_title: obj.file.nav_title || obj.file.post_title,
search_blurb: obj.file.search_blurb
}
}
newObj.children = obj.children.map(createDocsJSON);
return newObj;
}
const navSettings = {
navListProperty: 'navs',
permalinks: false,
formatJSONfn: createDocsJSON
}
let nav = navigation(navConfig, navSettings);
// --------- Compiling the Markdown files to HTML --------//
let createDocs = function(version) {
Metalsmith(path.join(__dirname, 'dcos-docs'))
.source(version)
.use(addTimestampToMarkdownFiles)
.use(markdown({
smartypants: true,
gfm: true,
tables: true
}))
.use(nav)
.use(layouts({
pattern: '**/*.html',
engine: 'jade',
directory: path.join('..', 'layouts'),
default: 'docs.jade'
}))
.clean(false)
.use(each(updatePaths))
.use(jade({
locals: { cssTimestamp },
pretty: true
}))
.destination(path.join('..', 'build', 'docs', version))
.build((err) => {
if (err) throw err
})
}
let allDocs = function() {
for (let version of docsVersions) {
createDocs(version)
}
}
Metalsmith(__dirname)
.use(addTimestampToMarkdownFiles)
.use(markdown({
smartypants: true,
gfm: true,
tables: true
}))
.use(jade({
locals: { cssTimestamp },
pretty: true
}))
.use(permalinks({
pattern: ':title',
date: 'YYYY',
linksets: [{
match: { collection: 'posts' },
pattern: 'blog/:date/:title'
}]
}))
.use(collections({
posts: {
pattern: '*.md',
sortBy: 'date',
reverse: true
}
}))
.use(addPropertiesToCollectionItems('posts', post => {
return Object.assign(post, {
formattedDate: moment(post.date).format('MMMM DD')
})
}))
.use(writemetadata({
collections: {
posts: {
output: {
path: 'blog/posts.json',
asObject: true
},
ignorekeys: ['contents', 'next', 'previous', 'stats', 'mode', 'lunr']
}
}
}))
.use(tags({
handle: 'category',
path:'blog/category/:tag.html',
layout:'../layouts/blog-category.jade',
sortBy: 'date',
reverse: true
}))
.use(mlunr({
indexPath: 'blog/search-index.json',
fields: {
contents: 2,
title: 10,
category: 5
}
}))
.use(define({
moment,
rootUrl: CONFIG.root_url
}))
.use(sass({
outputStyle: 'expanded',
includePaths: [
'/node_modules/',
path.join(__dirname, 'node_modules/support-for/sass')
].concat(bourbon.includePaths)
}))
.use(postcss([
autoprefixer({ browsers: ['last 4 versions'] })
]))
.use(babel({
presets: ['es2015'],
only: './src/scripts/**'
}))
.use(copy({
pattern: 'assets/*',
directory: 'assets'
}))
.use(each(updatePaths))
.clean(false)
.use(uglify({
filter: 'scripts/**/*.js',
concat: 'scripts/main.min.js',
// sourceMap: !process.env.CI,
// preserveComments: !process.env.CI,
// removeOriginal: process.env.CI,
// compress: process.env.CI
}))
.use(layouts({
pattern: '**/*.html',
engine: 'jade',
directory: path.join('layouts')
}))
.use((() => {
if(!process.env.CI) {
return browserSync({
open: false,
server: {
baseDir: './build',
middleware: [
modRewrite([
"^/docs/latest/(.*) /docs/" + docsVersions.slice(-1).pop() + "/$1"
]),
function(req, res, next) {
var file = `./build${req.originalUrl}.html`;
require('fs').exists(file, function(exists) {
if (exists) req.url += '.html';
next();
});
}
]
},
files: ['./src/**/*', './dcos-docs/**/*', './layouts/**/*', './mixins/**/*', './includes/**/*']
}, null, allDocs)
}
})())
.build((err) => {
if (err) throw err
})
allDocs()
// Utility functions
function addPropertiesToCollectionItems (collectionName, callback) {
return function (files, metalsmith, done) {
let metadata = metalsmith.metadata()
let collection = metadata[collectionName] || []
metadata[collectionName] = collection.map(callback)
return done()
}
}
function addTimestampToMarkdownFiles (files, metalsmith, callback) {
Object.keys(files).forEach(key => {
if (key.split('.').pop() !== 'md') return
Object.assign(files[key], { cssTimestamp })
})
callback()
}
function dasherize (string) {
return string.replace(/[A-Z]/g, function (char, index) {
return (index !== 0 ? '-' : '') + char.toLowerCase()
})
}