forked from center-key/node-slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
180 lines (175 loc) · 6.19 KB
/
gulpfile.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
// node-slate
// Imports
import browserSync from 'browser-sync';
import cleanCss from 'gulp-clean-css';
import concat from 'gulp-concat';
import ejs from 'gulp-ejs';
import gulp from 'gulp';
import gulpIf from 'gulp-if';
import highlight from 'highlight.js';
import jsHint from 'gulp-jshint';
import log from 'fancy-log';
import mergeStream from 'merge-stream';
import path from 'path';
import prettify from 'gulp-prettify';
import rename from 'gulp-rename';
import sassCompiler from 'sass';
import sassPlugin from 'gulp-sass';
import uglify from 'gulp-uglify';
import yaml from 'js-yaml';
import { marked } from 'marked';
import { readFileSync, writeFileSync } from 'fs';
// Setup
const port = 4567;
const sass = sassPlugin(sassCompiler);
const jsHintConfig = {
jquery: true,
browser: true,
undef: true,
unused: true,
};
const settings = {
compress: true,
};
const jsFiles = {
libs: [
'node_modules/jquery/dist/jquery.js',
'node_modules/jquery-ui-dist/jquery-ui.js', //not ideal: https://stackoverflow.com/q/34219046
'source/js/lib/_jquery.tocify.js',
'node_modules/tocbot/dist/tocbot.js', //see: https://github.com/center-key/node-slate/issues/8
'node_modules/imagesloaded/imagesloaded.pkgd.js',
],
scripts: [
'source/js/app/_lang.js',
'source/js/app/_toc.js',
],
search: [
'node_modules/fuse.js/dist/fuse.js',
'node_modules/jquery-highlight/jquery.highlight.js',
'source/js/app/_search.js',
],
};
// Helper functions
const renderer = new marked.Renderer();
renderer.code = (code, language) => {
const highlighted = language ? highlight.highlight(code, { language: language }).value :
highlight.highlightAuto(code).value;
return `<pre class="highlight ${language}"><code>${highlighted}</code></pre>`;
};
const readIndexYml = () => yaml.load(readFileSync('source/index.yml', 'utf8'));
const getPageData = () => {
const config = readIndexYml();
const includes = config.includes
.map(include => `source/includes/${include}.md`)
.map(include => readFileSync(include, 'utf8'))
.map(include => marked(include, { renderer: renderer }));
const code = (filename) => filename.split('.')[0];
const getPageData = {
current_page: { data: config },
page_classes: '',
includes: includes,
image_tag: (filename) =>
`<img alt=${code(filename)} class=image-${code(filename)} src=images/${filename}>`,
javascript_include_tag: (name) =>
`<script src=js/${name}.js></script>\n`,
stylesheet_link_tag: (name, media) =>
`<link href=css/${name}.css rel=stylesheet media=${media}>`,
langs: (config.language_tabs || []).map(
lang => typeof lang === 'string' ? lang : lang.keys.first),
};
return getPageData;
};
// Tasks
const task = {
runStaticAnalysis: () => {
return gulp.src(jsFiles.scripts)
.pipe(jsHint(jsHintConfig))
.pipe(jsHint.reporter());
},
buildFonts: () => {
return gulp.src('source/fonts/**/*.+(ttf|woff|woff2)')
.pipe(gulp.dest('build/fonts'));
},
buildImages: () => {
return gulp.src('source/images/**/*')
.pipe(gulp.dest('build/images'));
},
buildJs: () => {
const config = readIndexYml();
return gulp.src(jsFiles.libs.concat(config.search ? jsFiles.search : [], jsFiles.scripts))
.pipe(concat('all.js'))
.pipe(gulpIf(settings.compress, uglify()))
.pipe(gulp.dest('build/js'));
},
buildCss: () => {
return gulp.src('source/css/*.css.scss')
.pipe(sass().on('error', sass.logError))
.pipe(rename({ extname: '' }))
.pipe(gulpIf(settings.compress, cleanCss()))
.pipe(gulp.dest('build/css'));
},
addHighlightStyle: () => {
const config = readIndexYml();
const cssPath = 'node_modules/highlight.js/styles/' + config.highlight_theme + '.css';
return gulp.src(cssPath)
.pipe(rename({ prefix: 'highlight-' }))
.pipe(gulpIf(settings.compress, cleanCss()))
.pipe(gulp.dest('build/css'));
},
buildHtml: () => {
const data = getPageData();
return gulp.src('source/*.html')
.pipe(ejs(data).on('error', log.error))
.pipe(gulpIf(settings.compress, prettify({ indent_size: 3 })))
.pipe(gulp.dest('build'));
},
build: () => {
return mergeStream(
task.buildFonts(),
task.buildImages(),
task.buildJs(),
task.buildCss(),
task.addHighlightStyle(),
task.buildHtml(),
);
},
buildUncompressed: () => {
settings.compress = false;
return task.build();
},
runServer: () => {
gulp.watch('source/*.html', gulp.parallel('build-html'));
gulp.watch('source/includes/**/*', gulp.parallel('build-html'));
gulp.watch('source/js/**/*', gulp.parallel('build-js'));
gulp.watch('source/css/**/*', gulp.parallel('build-css'));
gulp.watch('source/index.yml', gulp.parallel('build-highlightjs', 'build-js', 'build-html'));
const server = browserSync.create();
server.init({
open: true,
ui: false,
listen: 'localhost',
port: port,
server: { baseDir: '.' },
startPath: 'build',
});
gulp.watch('build/**/*').on('change', server.reload);
console.log('Slate markdown source:');
console.log(path.resolve('source'));
},
publishToDocs: () => {
// mkdirSync('docs');
writeFileSync('docs/CNAME', 'node-slate.js.org\n');
return gulp.src('build/**/*')
.pipe(gulp.dest('docs'));
},
};
// Gulp
gulp.task('lint', task.runStaticAnalysis);
gulp.task('build-js', task.buildJs);
gulp.task('build-css', task.buildCss);
gulp.task('build-html', task.buildHtml);
gulp.task('build-highlightjs', task.addHighlightStyle);
gulp.task('build-static-site', task.build);
gulp.task('build-uncompressed', task.buildUncompressed);
gulp.task('serve', task.runServer);
gulp.task('publish', task.publishToDocs);