forked from dcos/dcos-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
184 lines (167 loc) · 4.65 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
'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')
// --- general build settings --- //
const docsVersions = ['1.7'];
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(markdown({
smartypants: true,
gfm: true,
tables: true
}))
.use(nav)
.use(layouts({
pattern: '**/*.html',
engine: 'jade',
directory: path.join('..', 'layouts'),
default: 'docs.jade'
}))
.use(jade({
pretty: true
}))
.clean(false)
.use(each(updatePaths))
.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(markdown({
smartypants: true,
gfm: true,
tables: true
}))
.use(jade({
pretty: true
}))
.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((() => {
if(!process.env.CI) {
return browserSync({
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()