forked from jpazureid/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
131 lines (119 loc) · 3.07 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
const gulp = require("gulp");
const replace = require("gulp-string-replace");
const del = require('del');
const path = require('path');
const blogRoot = "/blog"
const sourceFolders = ["active-directory-federation-service/", "azure-active-directory/", "azure-active-directory-connect/"];
const markdownFiles = sourceFolders.map(dir => path.join(dir, "**/*.md"));
const imageFiles = sourceFolders.map(dir => path.join(dir, "**/*.+(jpg|jpeg|png|gif|svg)"));
imageFiles.push('favicon/*.+(png|ico|svg|webmanifest|xml)')
const sourceFiles = sourceFolders.map(dir => path.join(dir, "**/*"));
const outputPath = "source/_posts/";
const Hexo = require("hexo");
const hexo = new Hexo(process.cwd(), {});
gulp.task("server", function (cb) {
hexo
.init()
.then(function () {
return hexo.call("server", {});
})
.then(function () {
return hexo.exit();
})
.then(function () {
return cb();
})
.catch(function (err) {
console.log(err);
hexo.exit(err);
return cb(err);
});
});
gulp.task("deploy", function (cb) {
hexo
.init()
.then(function () {
return hexo.call("clean", {});
})
.then(function () {
return hexo.call("deploy", {});
})
.then(function () {
return hexo.exit();
})
.then(function () {
return cb();
})
.catch(function (err) {
console.log(err);
hexo.exit(err);
return cb(err);
});
});
gulp.task("generate", function (cb) {
hexo
.init()
.then(function () {
return hexo.call("clean", {});
})
.then(function () {
return hexo.call("generate", {});
})
.then(function () {
return hexo.exit();
})
.then(function () {
return cb();
})
.catch(function (err) {
console.log(err);
hexo.exit(err);
return cb(err);
});
});
gulp.task('cleanOutputPath', function () {
return del([
path.join(outputPath, "/**/*")
]);
});
gulp.task("open", () => {
return gulp.src("./").pipe(exec("open http://localhost:4000"));
});
gulp.task("copyMarkdown", () => {
return (
gulp
.src(markdownFiles)
//fix absolute path image
.pipe(
replace(/!\[.*\]\(.*\/(.*)\)/g, (match, p1, offset, string) => {
return `{% asset_img ${p1} %}`;
})
).pipe(
// delete first h1 header
replace(/^# .*/m, "")
).pipe(
replace(/\]\((.+?).md\)/g, (match, p1, offset, string) => {
const pathes = p1.split('/')
const area = pathes[pathes.length - 2]
const title = pathes[pathes.length - 1].replace(".md", "")
return `](${blogRoot}/${area}/${title}/)`;
})
)
.pipe(gulp.dest(outputPath))
);
});
gulp.task("copyImage", () => {
return gulp.src(imageFiles).pipe(gulp.dest(outputPath));
});
gulp.task(
"default",
gulp.series("cleanOutputPath", gulp.parallel("copyMarkdown", "copyImage"), "server")
);
gulp.task(
"publish",
gulp.series("cleanOutputPath", gulp.parallel("copyMarkdown", "copyImage"), "deploy")
);
gulp.task(
"build",
gulp.series("cleanOutputPath", gulp.parallel("copyMarkdown", "copyImage"), "generate")
);