-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
275 lines (252 loc) · 7.55 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
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
"use strict";
const sass = require("gulp-sass")(require("sass"));
const gulp = require("gulp");
const sourcemaps = require("gulp-sourcemaps");
const fileinclude = require("gulp-file-include");
const autoprefixer = require("gulp-autoprefixer");
const bs = require("browser-sync").create();
const rimraf = require("rimraf");
const plumber = require('gulp-plumber');
const fs = require('fs');
const tap = require('gulp-tap');
const through = require('through2');
const frontMatter = require('gulp-front-matter');
async function getMarkdown() {
const markdown = await import('gulp-markdown');
return markdown.default;
}
const path = {
src: {
html: "source/*.html",
blog: "source/blog/*.md",
files: "source/files/*.pdf",
others: "source/*.+(php|ico|png)",
htminc: "source/partials/**/*.htm",
incdir: "source/partials/",
plugins: "source/plugins/**/*.*",
js: "source/js/*.js",
scss: "source/scss/**/*.scss",
images: "source/images/**/*.+(png|jpg|gif|svg)",
},
build: {
dirBuild: "theme/",
dirDev: "theme/",
},
};
let blogItemsIncludes = '';
// Blog
gulp.task("blog:build", async function () {
const markdown = await getMarkdown();
return gulp.src(path.src.blog)
.pipe(plumber())
.pipe(frontMatter())
.pipe(markdown())
.pipe(through.obj(function (file, enc, cb) {
this.push(file);
cb();
}, function (cb) {
if (this._transformState.writechunk) {
console.log('Processing Markdown file:', this._transformState.writechunk.relative);
}
cb();
}))
.pipe(tap(function (file) {
// Read the template
const template = fs.readFileSync('source/blog/blog_template.html', 'utf8');
const frontMatterData = file.frontMatter;
const title = frontMatterData.title || 'Default Title';
const pageName = frontMatterData['page-name'] || 'Default Page Name';
const bgImageUrl = frontMatterData['background-image-url'] || 'default-image.jpg';
const author = frontMatterData['author'] || 'AlgoÉTS';
const authorTitle = frontMatterData['author-title'] || 'Club';
const authorRole = frontMatterData['author-role'] || '.';
const authorImg = frontMatterData['author-img'] || 'default-image.jpg';
const facebookLink = frontMatterData['facebook-link'] || 'https://www.facebook.com/';
const twitterLink = frontMatterData['twitter-link'] || 'https://twitter.com/';
const linkedinLink = frontMatterData['linkedin-link'] || 'https://www.linkedin.com/';
const date = frontMatterData['date'];
const tagsHtml = frontMatterData['tags'].map(tag =>
`<li class="list-inline-item"><a href="#" rel="tag">${tag}</a></li>`
).join('\n');
const link = file.basename;
blogItemsIncludes += `
@@include('blocks/blog/blog-item.htm', {"image_src": "${bgImageUrl}", "date": "${date}", "title": "${title}", "summary": "${pageName}", "link": "${link}"})
`;
// Replace placeholders in the template
const htmlContent = template
.replace(/{{ title }}/g, title)
.replace(/{{ page-name }}/g, pageName)
.replace(/{{ background-image-url }}/g, bgImageUrl)
.replace(/{{ author }}/g, author)
.replace(/{{ author-title }}/g, authorTitle)
.replace(/{{ author-role }}/g, authorRole)
.replace(/{{ author-img }}/g, authorImg)
.replace(/{{ facebook-link }}/g, facebookLink)
.replace(/{{ twitter-link }}/g, twitterLink)
.replace(/{{ linkedin-link }}/g, linkedinLink)
.replace(/{{ tags }}/g, `<ul class="list-inline">${tagsHtml}</ul>`)
.replace(/{{ content }}/g, file.contents.toString());
// Update the file content
file.contents = Buffer.from(htmlContent);
}))
.pipe(fileinclude({
basepath: path.src.incdir,
}))
.pipe(gulp.dest(path.build.dirDev))
.pipe(bs.reload({
stream: true,
}));
});
// HTML
// HTML
gulp.task("html:build", function () {
return gulp
.src(path.src.html)
.pipe(plumber({
errorHandler: function (err) {
console.error('Error in plugin "' + err.plugin + '": ' + err.message);
this.emit('end');
}
}))
.pipe(through.obj(function (file, enc, cb) {
// Check if the file is blog-grid.html
if (file.basename === 'blog-grid.html') {
// Replace {{ blogList }} with blogItemsIncludes
let fileContent = file.contents.toString();
fileContent = fileContent.replace('{{ blogList }}', blogItemsIncludes);
file.contents = Buffer.from(fileContent);
}
this.push(file);
cb();
}, function (cb) {
if (this._transformState.writechunk) {
console.log('Processing file:', this._transformState.writechunk.relative);
}
cb();
}))
.pipe(fileinclude({
basepath: path.src.incdir,
}))
.pipe(gulp.dest(path.build.dirDev))
.pipe(bs.reload({
stream: true,
}));
});
gulp.task("blog-and-html:build", gulp.series("blog:build", "html:build"));
// SCSS
gulp.task("scss:build", function () {
return gulp
.src(path.src.scss)
.pipe(sourcemaps.init())
.pipe(
sass({
outputStyle: "expanded",
}).on("error", sass.logError)
)
.pipe(autoprefixer())
.pipe(sourcemaps.write("/"))
.pipe(gulp.dest(path.build.dirDev + "css/"))
.pipe(
bs.reload({
stream: true,
})
);
});
// Javascript
gulp.task("js:build", function () {
return gulp
.src(path.src.js)
.pipe(gulp.dest(path.build.dirDev + "js/"))
.pipe(
bs.reload({
stream: true,
})
);
});
// Images
gulp.task("images:build", function () {
return gulp
.src(path.src.images)
.pipe(gulp.dest(path.build.dirDev + "images/"))
.pipe(
bs.reload({
stream: true,
})
);
});
// Plugins
gulp.task("plugins:build", function () {
return gulp
.src(path.src.plugins)
.pipe(gulp.dest(path.build.dirDev + "plugins/"))
.pipe(
bs.reload({
stream: true,
})
);
});
// Other files like favicon, php, sourcele-icon on root directory
gulp.task("others:build", function () {
return gulp.src(path.src.others).pipe(gulp.dest(path.build.dirDev));
});
// PDF Files
gulp.task("files:build", function () {
return gulp
.src(path.src.files)
.pipe(gulp.dest(path.build.dirDev + "files/"))
.pipe(
bs.reload({
stream: true,
})
);
});
// Clean Build Folder
gulp.task("clean", function (cb) {
rimraf("./theme", cb);
});
// Watch Task
gulp.task("watch:build", function () {
gulp.watch(path.src.html, gulp.series("html:build"));
gulp.watch(path.src.blog, gulp.series("blog:build"));
gulp.watch(path.src.blog, gulp.series("blog-and-html:build"));
gulp.watch(path.src.htminc, gulp.series("html:build"));
gulp.watch(path.src.scss, gulp.series("scss:build"));
gulp.watch(path.src.js, gulp.series("js:build"));
gulp.watch(path.src.images, gulp.series("images:build"));
gulp.watch(path.src.plugins, gulp.series("plugins:build"));
gulp.watch(path.src.files, gulp.series("files:build"));
});
// Dev Task
gulp.task(
"default",
gulp.series(
"clean",
"blog-and-html:build",
"js:build",
"scss:build",
"images:build",
"plugins:build",
"others:build",
"files:build",
gulp.parallel("watch:build", function () {
bs.init({
server: {
baseDir: path.build.dirDev,
},
});
})
)
);
// Build Task
gulp.task(
"build",
gulp.series(
"blog-and-html:build",
"js:build",
"scss:build",
"images:build",
"plugins:build",
"others:build",
"files:build"
)
);