This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathpages.js
284 lines (259 loc) · 10.2 KB
/
pages.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
const gulp = require('gulp');
const config = require('../config.js');
const path = require('path');
const fs = require('fs');
const gutil = require('gulp-util');
const multiDest = require('gulp-multi-dest');
const through = require('through2');
const YAML = require('yamljs');
const ReactDOMServer = require('react-dom/server');
import Loading from '../src/app/components/Loading';
import React from 'react';
import ReactMarkdown from 'react-markdown';
const THUMBNAIL_FACEBOOK = config.PRODUCTION_URL + '/static/images/thumbnail-facebook.png';
const THUMBNAIL_TWITTER = config.PRODUCTION_URL + '/static/images/thumbnail-twitter.png';
const META_TITLE = 'Firefox Test Pilot';
const META_DESCRIPTION = 'Test new Features. Give us feedback. Help build Firefox.';
gulp.task('pages-misc', () => {
// We just need a dummy file to get a stream going; we're going to ignore
// the contents in buildLandingPage
return gulp.src(config.SRC_PATH + 'pages/*.js')
.pipe(buildLandingPage())
.pipe(gulp.dest(config.DEST_PATH));
});
gulp.task('pages-experiments', () => {
return gulp.src(config.CONTENT_SRC_PATH + 'experiments/*.yaml')
.pipe(buildExperimentPage())
.pipe(gulp.dest(config.DEST_PATH + 'experiments'));
});
gulp.task('pages-compiled', () => {
return gulp.src(config.SRC_PATH + 'pages/**/*.md')
.pipe(convertToCompiledPage())
.pipe(gulp.dest(config.DEST_PATH));
});
gulp.task('pages-contributing', () => {
gulp.src('./contribute.json')
.pipe(gulp.dest(config.DEST_PATH));
});
gulp.task('pages-build', [
'pages-misc',
'pages-experiments',
'pages-contributing',
'pages-compiled'
]);
gulp.task('pages-watch', () => {
gulp.watch(config.SRC_PATH + 'app.js', ['pages-build']);
gulp.watch(config.CONTENT_SRC_PATH + '/**/*.yaml', ['pages-build']);
gulp.watch(config.SRC_PATH + 'pages/**/*.md', ['pages-compiled']);
});
function buildLandingPage() {
return through.obj(function landingPage(file, enc, cb) {
const fileName = path.basename(file.history[0]);
const noExtension = fileName.slice(0, fileName.indexOf('.'));
const pageModule = path.join('..', 'src', 'pages', fileName);
const outputPath = noExtension === 'home' ? 'index.html' : noExtension + '/index.html'
const importedCreate = require(pageModule).default;
const importedComponent = importedCreate();
const pageContent = generateStaticPage(
true,
noExtension, '',
importedComponent,
{
meta_title: META_TITLE,
meta_description: META_DESCRIPTION,
canonical_path: '',
image_facebook: THUMBNAIL_FACEBOOK,
image_twitter: THUMBNAIL_TWITTER,
enable_pontoon: config.ENABLE_PONTOON,
available_locales: config.AVAILABLE_LOCALES
}
);
this.push(new gutil.File({
path: outputPath,
contents: new Buffer(pageContent)
}));
cb();
});
}
function buildExperimentPage() {
return through.obj(function experimentPage(file, enc, cb) {
const yamlData = file.contents.toString();
const experiment = YAML.parse(yamlData);
const requiredCreate = require('../src/pages/experiment.js').default;
const requiredComponent = requiredCreate(experiment.slug);
const pageContent = generateStaticPage(
true,
'experiment', experiment.slug,
requiredComponent,
{
meta_title: META_TITLE + ' - ' + experiment.title,
meta_description: experiment.description,
canonical_path: 'experiments/' + experiment.slug + '/',
image_facebook: config.PRODUCTION_URL + experiment.image_facebook ||
THUMBNAIL_FACEBOOK,
image_twitter: config.PRODUCTION_URL + experiment.image_twitter ||
THUMBNAIL_TWITTER,
enable_pontoon: config.ENABLE_PONTOON,
available_locales: config.AVAILABLE_LOCALES
}
);
this.push(new gutil.File({
path: experiment.slug + '/index.html',
contents: new Buffer(pageContent)
}));
cb();
});
}
function availableLanguages(path) {
return fs.readdirSync(path)
.map(function (f) {
return f.split('.')[0]
})
.join(',');
}
function convertToCompiledPage() {
return through.obj(function compiledConvert(file, encoding, callback) {
const p = path.parse(file.path);
const locale = p.name;
const page = path.basename(p.dir);
const noExtension = page.slice(0, page.indexOf('.'));
const pageContent = generateStaticPageFromMarkdown(
noExtension, '',
file.contents.toString(),
{
available_locales: availableLanguages(p.dir),
canonical_path: page,
meta_title: META_TITLE,
meta_description: META_DESCRIPTION,
image_facebook: THUMBNAIL_FACEBOOK,
image_twitter: THUMBNAIL_TWITTER
}
);
const contents = new Buffer(pageContent);
if (locale === 'en-US') {
this.push(new gutil.File({
path: path.join(page, 'index.html'),
contents
}));
}
this.push(new gutil.File({
path: path.join(page, locale, 'index.html'),
contents
}));
callback();
});
}
function makeStaticString(
prepareForClient, pageName, pageParam, headComponent,
staticBodyComponent, dynamicBodyComponent
) {
const head = ReactDOMServer.renderToStaticMarkup(headComponent);
const staticRoot = ReactDOMServer.renderToStaticMarkup(staticBodyComponent);
let dynamicRoot;
let bootstrapScript;
let dynamicPageClass = '';
if (prepareForClient) {
dynamicRoot = ReactDOMServer.renderToString(dynamicBodyComponent);
bootstrapScript = '<script src="/static/app/app.js"></script>';
dynamicPageClass = ' class="dynamic-page"';
} else if (dynamicBodyComponent) {
dynamicRoot = ReactDOMServer.renderToStaticMarkup(dynamicBodyComponent);
bootstrapScript = '';
}
return `<!DOCTYPE html>
<html>
${head}
<body class="blue" data-page-name="${pageName}" data-page-param="${pageParam}">
<div class="stars"></div>
${staticRoot}
<div id="page-container"${dynamicPageClass}>${dynamicRoot}</div>
${bootstrapScript}
</body>
</html>`;
}
function generateStaticPage(prepareForClient, pageName, pageParam, component, {
available_locales, canonical_path, meta_title, meta_description,
image_facebook, image_twitter, enable_pontoon
}) {
const headComponent = <head>
<meta charSet="utf-8" />
<link rel="shortcut icon" href="/static/images/favicon.ico" />
<link rel="stylesheet" href="https://code.cdn.mozilla.net/fonts/fira.css" />
<link rel="stylesheet" href="/static/styles/experiments.css" />
<link rel="stylesheet" href="/static/styles/main.css" />
<meta name="defaultLanguage" content="en-US" />
<meta name="availableLanguages" content={ available_locales } />
<meta name="viewport" content="width=device-width" />
<link rel="alternate" type="application/atom+xml" href="/feed.atom" title="Atom Feed"/>
<link rel="alternate" type="application/rss+xml" href="/feed.rss" title="RSS Feed"/>
<link rel="alternate" type="application/json" href="/feed.json" title="JSON Feed"/>
<link rel="canonical" href={ `https://testpilot.firefox.com/${canonical_path}` } />
<title>{ meta_title }</title>
<meta property="og:title" content={ meta_title } />
<meta name="twitter:title" content={ meta_title } />
<meta name="description" content={ meta_description } />
<meta property="og:description" content={ meta_description } />
<meta name="twitter:description" content={ meta_description } />
<meta name="twitter:card" content="summary" />
<meta property="og:image" content={ image_facebook } />
<meta name="twitter:image" content={ image_twitter } />
<meta property="og:url" content="https://testpilot.firefox.com" />
</head>;
const bodyComponent = <div id="static-root">
<noscript>
<div className="full-page-wrapper centered">
<div className="layout-wrapper layout-wrapper--column-center">
<div id="four-oh-four" className="modal delayed-fade-in">
<h1 className="title">Uh oh...</h1>
<div className="modal-content">
<p>Test Pilot requires JavaScript.<br />Sorry about that.</p>
</div>
<div className="modal-actions">
<a className="button default large" href="https://github.com/mozilla/testpilot/blob/master/docs/faq.md">Find out why</a>
</div>
</div>
<div className="copter">
<div className="copter__inner"></div>
</div>
</div>
</div>
</noscript>
{ prepareForClient ? <Loading /> : null }
{ prepareForClient ? <script src="/static/app/vendor.js"></script> : null }
</div>;
return makeStaticString(prepareForClient, pageName, pageParam, headComponent, bodyComponent, component);
}
function generateStaticPageFromMarkdown(pageName, pageParam, markdown, params) {
const body = <div className="full-page-wrapper">
<header id="main-header" className="layout-wrapper layout-wrapper--row-between-top">
<h1>
<a href="/" className="wordmark">Firefox Test Pilot</a>
</h1>
</header>
<div className="layout-wrapper static-page-content">
<ReactMarkdown source={ markdown } />
</div>
<footer id="main-footer">
<div id="footer-links" className="layout-wrapper layout-wrapper--row-bottom-breaking">
<div className="legal-links">
<a href="https://www.mozilla.org" className="mozilla-logo"></a>
<a href="https://www.mozilla.org/about/legal/" className="boilerplate">Legal</a>
<a href="/about" className="boilerplate">About Test Pilot</a>
<a href="/privacy" className="boilerplate">Privacy</a>
<a href="/terms" className="boilerplate">Terms</a>
<a href="https://www.mozilla.org/privacy/websites/#cookies" className="boilerplate">Cookies</a>
</div>
<div className="social-links">
<a href="https://github.com/mozilla/testpilot" target="_blank" title="GitHub" className="link-icon github"></a>
<a href="https://twitter.com/FxTestPilot" target="_blank" title="Twitter" className="link-icon twitter"></a>
</div>
</div>
</footer>
<script src="/static/app/vendor.js"></script>
<script src="/static/scripts/locale.js"></script>
<script src="/static/scripts/legal.js"></script>
</div>;
return generateStaticPage(
false, pageName, pageParam, body, params
);
}