This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
forked from d2iq-archive/dcos-docs-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
358 lines (310 loc) · 9.67 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Packages
const Metalsmith = require('metalsmith');
const markdown = require('metalsmith-markdownit');
const layouts = require('metalsmith-layouts');
const permalinks = require('metalsmith-permalinks');
const assets = require('metalsmith-assets');
const dataLoader = require('metalsmith-data-loader');
const watch = require('metalsmith-watch');
const branch = require('metalsmith-branch')
const serve = require('metalsmith-serve');
const redirect = require('metalsmith-redirect');
const webpack = require('metalsmith-webpack2');
const anchor = require('markdown-it-anchor');
const attrs = require('markdown-it-attrs');
const timer = require('metalsmith-timer');
const ignore = require('metalsmith-ignore');
// Local Plugins
const reduce = require('./plugins/metalsmith-revision').reduce;
const restore = require('./plugins/metalsmith-revision').restore;
const hierarchy = require('./plugins/metalsmith-hierarchy');
const hierarchyRss = require('./plugins/metalsmith-hierarchy-rss');
const headings = require('./plugins/metalsmith-headings');
const algolia = require('./plugins/metalsmith-algolia');
const inPlace = require('./plugins/metalsmith-in-place-dcos');
const includeContent = require('./plugins/metalsmith-include-content-dcos');
const shortcodes = require('./plugins/metalsmith-shortcodes');
const wkhtmltopdfLinkResolver = require('./plugins/metalsmith-wkhtmltopdf-link-resolver');
// Configs
const shortcodesConfig = require('./shortcodes');
function splitCommasOrEmptyArray(val) {
return (val && val.length > 0) ? val.split(',') : [];
}
// Environment Variables
const GIT_BRANCH = process.env.GIT_BRANCH;
const ALGOLIA_UPDATE = process.env.ALGOLIA_UPDATE;
const ALGOLIA_PROJECT_ID = process.env.ALGOLIA_PROJECT_ID;
const ALGOLIA_PUBLIC_KEY = process.env.ALGOLIA_PUBLIC_KEY;
const ALGOLIA_PRIVATE_KEY = process.env.ALGOLIA_PRIVATE_KEY;
const ALGOLIA_INDEX = process.env.ALGOLIA_INDEX;
const ALGOLIA_CLEAR_INDEX = process.env.ALGOLIA_CLEAR_INDEX;
const RENDER_PATH_PATTERN = process.env.RENDER_PATH_PATTERN;
const ALGOLIA_SKIP_SECTIONS = splitCommasOrEmptyArray(process.env.ALGOLIA_SKIP_SECTIONS);
const METALSMITH_SKIP_SECTIONS = splitCommasOrEmptyArray(process.env.METALSMITH_SKIP_SECTIONS);
//
// Errors
//
if (!GIT_BRANCH && process.env.NODE_ENV !== 'development') {
throw new Error('Env var GIT_BRANCH has not been set.');
}
if (ALGOLIA_UPDATE === 'true') {
if (process.env.NODE_ENV === 'pdf') {
throw new Error('Algolia env vars set while build env is pdf');
}
if (!ALGOLIA_PROJECT_ID) {
throw new Error('Env var ALGOLIA_PROJECT_ID has not been set.');
}
if (!ALGOLIA_PUBLIC_KEY) {
throw new Error('Env var ALGOLIA_PUBLIC_KEY has not been set.');
}
if (!ALGOLIA_PRIVATE_KEY) {
throw new Error('Env var ALGOLIA_PRIVATE_KEY has not been set.');
}
if (!ALGOLIA_INDEX) {
throw new Error('Env var ALGOLIA_INDEX has not been set.');
}
}
//
// Metalsmith
//
const MS = Metalsmith(__dirname);
const currentYear = (new Date()).getFullYear();
// Metadata
// These are available in the layouts as js variables
MS.metadata({
url: 'https://docs.mesosphere.com',
siteTitle: 'Mesosphere DC/OS Documentation',
siteDescription: 'Welcome to the DC/OS documentation. The DC/OS documentation ' +
'can help you set up, learn about the system, and get your applications and' +
' workloads running on DC/OS.',
copyright: `© ${currentYear} Mesosphere, Inc. All rights reserved.`,
env: process.env.NODE_ENV,
gitBranch: GIT_BRANCH,
dcosDocsLatest: '1.11',
});
// Source
// Where metalsmith looks for all files
MS.source('./pages');
// Destination
// Where metalsmith will put the output code
MS.destination('./build');
// Don't Clean
// Cleaning removes the destination directory before writing to it
// I imagine cleaning makes watching take a long time, but untested for now
MS.clean(false);
//
// Content Branch Pipeline
//
const CB = branch();
// Start timer
CB.use(timer('CB: Init'));
CB.use(ignore(METALSMITH_SKIP_SECTIONS));
CB.use(timer('CB: Ignore'));
// Load model data from external .json/.yaml files
// For example (in your Front Matter):
// model: path/to/my.yml (access content in my.yml as model.foo.bar)
// Can also specify multiple named models:
// model:
// data1: path/to/my.json (access content in my.json as model.data1.foo.bar)
// data2: path/to/my.yml (access content in my.yml as model.data2.foo.bar)
CB.use(dataLoader({
dataProperty: 'model',
// Only enable in service pages for now.
match: 'services/**/*.md',
}));
CB.use(timer('CB: Dataloader'));
// Load raw content via '#include' directives before rendering any mustache or markdown.
// For example (in your content):
// #include path/to/file.tmpl
CB.use(includeContent({
// Style as a C-like include statement. Must be on its own line.
pattern: '^#include ([^ \n]+)$',
// Only enable in service pages for now.
match: 'services/**/*.md*',
}));
CB.use(timer('CB: IncludeContent'));
// Process any mustache templating in files.
// For example (in your Front Matter):
// render: mustache
CB.use(inPlace({
renderProperty: 'render',
// Only enable in service pages for now.
match: 'services/**/*.md',
}));
CB.use(timer('CB: Mustache'));
// Folder Hierarchy
CB.use(hierarchy({
files: ['.md'],
excerpt: true,
}));
CB.use(timer('CB: Hierarchy'));
// RSS Feed
CB.use(hierarchyRss({
itemOptionsMap: {
title: 'title',
description: 'excerpt',
},
}));
CB.use(timer('CB: Hierarchy RSS'));
// Filter unmodified files
if (process.env.NODE_ENV === 'development') {
CB.use(reduce());
CB.use(timer('CB: Reduce'));
}
//
// Slow Plugins
//
// Shortcodes
CB.use(shortcodes({
files: ['.md'],
shortcodes: shortcodesConfig,
}));
CB.use(timer('CB: Shortcodes'));
// Markdown
CB.use(markdown(
{
smartList: false,
typographer: true,
html: true,
})
.use(anchor, {
permalink: true,
renderPermalink: (slug, opts, state, idx) => {
const linkTokens = [
Object.assign(new state.Token('link_open', 'a', 1), {
attrs: [
['class', opts.permalinkClass],
['href', opts.permalinkHref(slug, state)],
['aria-hidden', 'true'],
],
}),
Object.assign(new state.Token('html_block', '', 0), { content: opts.permalinkSymbol }),
new state.Token('link_close', 'a', -1),
];
state.tokens[idx + 1].children.unshift(...linkTokens);
},
permalinkClass: 'content__anchor',
permalinkSymbol: '<i data-feather="bookmark"></i>',
permalinkBefore: true,
})
.use(attrs),
);
CB.use(timer('CB: Markdown'));
// Headings
CB.use(headings());
CB.use(timer('CB: Headings'));
CB.use(redirect({
'/support': 'https://support.mesosphere.com',
}));
CB.use(timer('CB: Redirects'));
// Permalinks
CB.use(permalinks());
CB.use(timer('CB: Permalinks'));
// Layouts
if (!RENDER_PATH_PATTERN) {
// Default: Render all pages.
CB.use(layouts({
engine: 'pug',
cache: true,
}));
} else {
// Dev optimization: Only render within a specific path (much faster turnaround)
// For example, 'services/beta-cassandra/latest/**'
CB.use(layouts({
engine: 'pug',
pattern: RENDER_PATH_PATTERN,
cache: true,
}));
}
CB.use(timer('CB: Layouts'));
//
// Slow Plugins End
//
// Restore unmodified files
if (process.env.NODE_ENV === 'development') {
CB.use(restore());
CB.use(timer('CB: Reduce'));
}
// The expected pattern format doesn't work with regex
let pathPatternRegex;
if (RENDER_PATH_PATTERN) {
pathPatternRegex = RENDER_PATH_PATTERN.split('/').slice(0, -1).join("\/");
}
// Search Indexing
if (ALGOLIA_UPDATE === 'true') {
CB.use(algolia({
projectId: ALGOLIA_PROJECT_ID,
privateKey: ALGOLIA_PRIVATE_KEY,
index: ALGOLIA_INDEX,
clearIndex: (ALGOLIA_CLEAR_INDEX !== undefined) ? (ALGOLIA_CLEAR_INDEX === 'true') : true,
skipSections: ALGOLIA_SKIP_SECTIONS,
renderPathPattern: pathPatternRegex,
}));
CB.use(timer('CB: Algolia'));
}
// Enable watching
// The keys represent the files to watch, the values are the files that will
// be updated. ONLY the files that are being updated will be accessible to
// during the rebuild. We must include everything at this point or the
// templates will not be accessible. Need changes to fix this.
// Can only watch with a RENDER_PATH_PATTERN because there are too many
// files without it.
if (process.env.NODE_ENV === 'development' && RENDER_PATH_PATTERN) {
CB.use(watch({
paths: {
[`pages/${RENDER_PATH_PATTERN}/*`]: '**/*.{md,tmpl}',
'layouts/**/*': '**/*.pug',
},
}));
CB.use(timer('CB: Watch'));
}
// WkhtmltopdfLinkResolver
if (process.env.NODE_ENV === 'pdf') {
CB.use(wkhtmltopdfLinkResolver({
prefix: '/tmp/pdf/build',
}));
CB.use(timer('CB: WkhtmltopdfLinkResolver'));
}
// Serve
if (process.env.NODE_ENV === 'development') {
CB.use(serve({
port: 3000,
}));
CB.use(timer('CB: Webserver'));
}
//
// Assets Branch
//
const AB = branch();
// Start timer
AB.use(timer('AB: Init'));
// Watch
// Can only watch with a RENDER_PATH_PATTERN because there are too many
// files without it.
if (process.env.NODE_ENV === 'development' && RENDER_PATH_PATTERN) {
AB.use(watch({
paths: {
'js/**/*': '**/*.js',
'scss/**/*': '**/*.scss',
},
}));
AB.use(timer('AB: Watch'));
}
// Assets
AB.use(assets({
source: 'assets',
destination: 'assets',
}));
AB.use(timer('AB: Assets'));
// Webpack
AB.use(webpack('./webpack.config.js'));
AB.use(timer('AB: Webpack'));
//
// Metalsmith
//
MS.use(CB);
MS.use(AB);
// Build
MS.build((err, files) => {
if (err) throw err;
});