-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
executable file
·199 lines (171 loc) · 5.63 KB
/
index.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
#!/usr/bin/env node
const {
asset,
dest,
name,
on,
port,
debugPagePath,
helpMessage,
loggerTitle,
addMiddleware,
} = require("berber");
const layout1 = require("layout1");
const rename = require("gulp-rename");
const { readFileSync, existsSync, statSync } = require("fs");
const { join } = require("path");
const minimisted = require("minimisted");
const openurl = require("openurl");
const transform = require("vinyl-transform");
const map = require("map-stream");
const emoji = require("node-emoji");
require("yaml-hook/register");
const emojify = () =>
transform((filename) =>
map((chunk, next) => next(null, emoji.emojify(chunk.toString())))
);
const read = (path) => readFileSync(join(__dirname, path)).toString();
const layoutFilename = join(__dirname, "layout.njk");
const defaultCss = read("assets/default.css");
const defaultAssetsPath = "assets";
const defaultConfig = {
title: "", // The page title of the result html
description: "", // The description og:description meta tag
ogImage: "", // The url for og:image and twitter:image
ogImageWidth: null, // The width of og:image
ogImageHeight: null, // The height of og:image
twitter: null, // twitter display id
favicon: null, // The path to favicon
url: null, // The url of the slides
port: 6275, // The port number of dev server
livereload: true,
livereloadPort: 35729,
dest: "build", // The destination directory of built assets
out: "index.html", // The result html filename of the slides
source: "slides.md", // The source of the slides
css: defaultCss, // The additional css for the slides
cssFiles: [], // The additional css files
script: "", // The additional script for the slides
scriptFiles: [], // The additional script files
remarkConfig: {}, // The config object passed to remark
remarkPath: join(__dirname, "vendor", "remark.js"), // The remark path
scriptFilesAfterCreate: [], // The additional script files loaded after remark.create
assets: [defaultAssetsPath], // The asset paths
"open-browser": false, // open the browser to the page when the server starts
};
name("remarker");
debugPagePath("__remarker__");
loggerTitle("remarker");
helpMessage(read("assets/help-message.txt"));
const onConfig = (config, argv) => {
config = Object.assign({}, defaultConfig, config, argv);
port(config.port);
dest(config.dest);
const cssFiles = !Array.isArray(config.cssFiles)
? Array(config.cssFiles)
: config.cssFiles;
const scriptFiles = !Array.isArray(config.scriptFiles)
? Array(config.scriptFiles)
: config.scriptFiles;
const scriptFilesAfterCreate = !Array.isArray(config.scriptFilesAfterCreate)
? Array(config.scriptFilesAfterCreate)
: config.scriptFilesAfterCreate;
const slidePipeline = asset(config.source)
.assetOptions({ allowEmpty: true })
.pipe(emojify())
.pipe(rename(config.out))
.pipe(
layout1.nunjucks(layoutFilename, {
data: {
css: config.css,
cssFiles: cssFiles,
script: config.script,
scriptFiles: scriptFiles,
title: config.title,
description: config.description,
ogImage: config.ogImage,
ogImageWidth: config.ogImageWidth,
ogImageHeight: config.ogImageHeight,
twitter: config.twitter,
favicon: config.favicon,
url: config.url,
remarkConfig: config.remarkConfig,
scriptFilesAfterCreate: scriptFilesAfterCreate,
livereloadPort: config.livereloadPort,
},
}),
);
// livereload settings
if (config.livereload) {
on("serve", () => {
onLivereloadConfig(slidePipeline, config);
});
}
// open browser if the option specified
if (config["open-browser"]) {
on("serve", () => {
openurl.open("http://localhost:" + config.port);
});
}
asset(config.remarkPath).pipe(rename("remark.js"));
cssFiles
.filter((src) => !/^http/.test(src))
.forEach((src) => {
asset(src).base(process.cwd());
});
scriptFiles
.filter((src) => !/^http/.test(src))
.forEach((src) => {
asset(src).base(process.cwd());
});
config.assets.forEach((src) => {
if (existsSync(src)) {
const stat = statSync(src);
if (stat.isDirectory()) {
asset(join(src, "**/*.*")).base(process.cwd());
} else if (stat.isFile()) {
asset(src).base(process.cwd());
} else {
console.log(
`Warning: asset entry '${src}' has unknown type, skipping this entry`,
);
}
} else if (src === defaultAssetsPath) {
// do nothing, ignore silently
} else {
console.log(
`Warning: asset entry '${src}' not found, skipping this entry`,
);
}
});
};
const livereloadScriptMiddleware = (req, res, next) => {
if (require("url").parse(req.url).pathname !== "/livereload.js") {
next();
return;
}
const livereloadScript = read("vendor/livereload.js");
res.setHeader("Content-Type", "text/javascript");
res.end(livereloadScript);
};
const onLivereloadConfig = (slidePipeline, config) => {
const livereload = require("connect-livereload");
const gulplivereload = require("gulp-livereload");
const port = config.livereloadPort;
addMiddleware(() => livereload({ port, src: "/livereload.js" }));
addMiddleware(() => livereloadScriptMiddleware);
gulplivereload.listen({ port });
slidePipeline.pipe(gulplivereload({ port }));
};
on("config", (config) =>
minimisted((argv) => onConfig(config, argv), {
string: ["source", "out", "dest", "port"],
boolean: ["open-browser"],
alias: {
s: "source",
o: "out",
p: "port",
d: "dest",
b: "open-browser",
},
}));