forked from center-key/node-slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
195 lines (190 loc) Β· 7.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// node-slate
// Imports
const cleanCss = require('gulp-clean-css');
const concat = require('gulp-concat');
const del = require('del');
const ejs = require('gulp-ejs');
const fs = require('fs');
const gls = require('gulp-live-server');
const gulp = require('gulp');
const gulpIf = require('gulp-if');
const log = require('fancy-log');
const highlight = require('highlight.js');
const htmlHint = require('gulp-htmlhint');
const htmlValidator = require('gulp-w3c-html-validator');
const jsHint = require('gulp-jshint');
const marked = require('marked');
const mergeStream = require('merge-stream');
const open = require('gulp-open');
const path = require('path');
const prettify = require('gulp-prettify');
const rename = require('gulp-rename');
const sass = require('gulp-sass');
const uglify = require('gulp-uglify');
const yaml = require('js-yaml');
// Setup
const pkg = require('./package.json');
const port = 4567;
const htmlHintConfig = { 'attr-value-double-quotes': false };
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/javascripts/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/javascripts/app/_lang.js',
'source/javascripts/app/_toc.js',
],
search: [
'source/javascripts/lib/_lunr.js', //see: https://github.com/center-key/node-slate/issues/9
// 'node_modules/lunr/lunr.js',
'source/javascripts/lib/_jquery.highlight.js',
// 'node_modules/jquery-highlight/jquery.highlight.js',
'source/javascripts/app/_search.js',
]
};
// Helper functions
const renderer = new marked.Renderer();
renderer.code = (code, language) => {
const highlighted = language ? highlight.highlight(language, code).value :
highlight.highlightAuto(code).value;
return `<pre class="highlight ${language}"><code>${highlighted}</code></pre>`;
};
const readIndexYml = () => yaml.safeLoad(fs.readFileSync('source/index.yml', 'utf8'));
const getPageData = () => {
const config = readIndexYml();
const includes = config.includes
.map(include => `source/includes/${include}.md`)
.map(include => fs.readFileSync(include, 'utf8'))
.map(include => marked(include, { renderer: renderer }));
const getPageData = {
current_page: { data: config },
page_classes: '',
includes: includes,
image_tag: (filename) => {
const code = filename.split('.')[0];
return `<img alt=${code} class=image-${code} src=images/${filename}>`;
},
javascript_include_tag: (name) =>
`<script src=javascripts/${name}.js type=text/javascript></script>\n`,
stylesheet_link_tag: (name, media) =>
`<link href=stylesheets/${name}.css rel=stylesheet media=${media}>`,
langs: (config.language_tabs || []).map(
lang => typeof lang === 'string' ? lang : lang.keys.first)
};
return getPageData;
};
// Tasks
const task = {
clean: () => {
console.log(pkg.name, 'v' + pkg.version);
return del(['build/*', '**/.DS_Store']);
},
runStaticAnalysis: () => {
const ignoreDuplicateIds = (type, message) => !/^Duplicate ID/.test(message);
return mergeStream(
gulp.src('build/index.html')
.pipe(htmlHint(htmlHintConfig))
.pipe(htmlHint.reporter())
.pipe(htmlValidator({ verifyMessage: ignoreDuplicateIds }))
.pipe(htmlValidator.reporter()),
gulp.src(jsFiles.scripts)
.pipe(jsHint(jsHintConfig))
.pipe(jsHint.reporter())
);
},
buildFonts: () => {
return gulp.src('source/fonts/**/*')
.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/javascripts'));
},
buildCss: () => {
return gulp.src('source/stylesheets/*.css.scss')
.pipe(sass().on('error', sass.logError))
.pipe(rename({ extname: '' }))
.pipe(gulpIf(settings.compress, cleanCss()))
.pipe(gulp.dest('build/stylesheets'));
},
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/stylesheets'));
},
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/javascripts/**/*', gulp.parallel('build-js'));
gulp.watch('source/stylesheets/**/*', gulp.parallel('build-css'));
gulp.watch('source/index.yml', gulp.parallel('build-highlightjs', 'build-js', 'build-html'));
const server = gls.static('build', port);
server.start();
const notifyServer = (file) => server.notify.apply(server, [file]);
gulp.watch('build/**/*', notifyServer);
gulp.src(__filename).pipe(open({ uri: 'http://localhost:' + port }));
console.log('Slate markdown source:');
console.log(path.resolve('source'));
},
publishToDocs: () => {
// fs.mkdirSync('docs');
fs.writeFileSync('docs/CNAME', 'node-slate.js.org\n');
return gulp.src('build/**/*')
.pipe(gulp.dest('docs'));
}
};
// Gulp
gulp.task('clean', task.clean);
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);