@slidev/cli 0.29.2 → 0.30.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict"; function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2
2
 
3
- var _chunkXI447TZRjs = require('./chunk-XI447TZR.js');
3
+ var _chunkE4IOOAKNjs = require('./chunk-E4IOOAKN.js');
4
4
 
5
5
 
6
6
 
@@ -8,19 +8,19 @@ var _chunkXI447TZRjs = require('./chunk-XI447TZR.js');
8
8
 
9
9
 
10
10
 
11
- var _chunkZ6YPUFV4js = require('./chunk-Z6YPUFV4.js');
11
+ var _chunk2LNCAUCDjs = require('./chunk-2LNCAUCD.js');
12
12
 
13
13
 
14
- var _chunkNPCPO6ETjs = require('./chunk-NPCPO6ET.js');
14
+ var _chunkRSHUQSN4js = require('./chunk-RSHUQSN4.js');
15
15
 
16
16
 
17
17
 
18
18
 
19
- var _chunk7QXPSYEZjs = require('./chunk-7QXPSYEZ.js');
19
+ var _chunkIXNT7VMKjs = require('./chunk-IXNT7VMK.js');
20
20
 
21
21
  // node/cli.ts
22
- _chunk7QXPSYEZjs.init_cjs_shims.call(void 0, );
23
- var import_fast_deep_equal = _chunk7QXPSYEZjs.__toModule.call(void 0, _chunkNPCPO6ETjs.require_fast_deep_equal.call(void 0, ));
22
+ _chunkIXNT7VMKjs.init_cjs_shims.call(void 0, );
23
+ var import_fast_deep_equal = _chunkIXNT7VMKjs.__toESM.call(void 0, _chunkRSHUQSN4js.require_fast_deep_equal.call(void 0, ));
24
24
  var _path = require('path'); var _path2 = _interopRequireDefault(_path);
25
25
  var _net = require('net'); var _net2 = _interopRequireDefault(_net);
26
26
  var _os = require('os'); var _os2 = _interopRequireDefault(_os);
@@ -38,7 +38,7 @@ var CONFIG_RESTART_FIELDS = [
38
38
  "routerMode",
39
39
  "fonts"
40
40
  ];
41
- var cli = _yargs2.default.scriptName("slidev").usage("$0 [args]").version(_chunkZ6YPUFV4js.version).strict().showHelpOnFail(false).alias("h", "help").alias("v", "version");
41
+ var cli = _yargs2.default.scriptName("slidev").usage("$0 [args]").version(_chunk2LNCAUCDjs.version).strict().showHelpOnFail(false).alias("h", "help").alias("v", "version");
42
42
  cli.command("* [entry]", "Start a local server for Slidev", (args) => commonOptions(args).option("port", {
43
43
  alias: "p",
44
44
  type: "number",
@@ -82,9 +82,9 @@ cli.command("* [entry]", "Start a local server for Slidev", (args) => commonOpti
82
82
  async function initServer() {
83
83
  if (server)
84
84
  await server.close();
85
- const options = await _chunkZ6YPUFV4js.resolveOptions.call(void 0, { entry, theme }, "dev");
85
+ const options = await _chunk2LNCAUCDjs.resolveOptions.call(void 0, { entry, theme }, "dev");
86
86
  port = userPort || await findFreePort(3030);
87
- server = await _chunkXI447TZRjs.createServer.call(void 0, options, {
87
+ server = await _chunkE4IOOAKNjs.createServer.call(void 0, options, {
88
88
  server: {
89
89
  port,
90
90
  strictPort: true,
@@ -95,7 +95,7 @@ cli.command("* [entry]", "Start a local server for Slidev", (args) => commonOpti
95
95
  logLevel: log
96
96
  }, {
97
97
  onDataReload(newData, data) {
98
- if (!theme && _chunkZ6YPUFV4js.resolveThemeName.call(void 0, newData.config.theme) !== _chunkZ6YPUFV4js.resolveThemeName.call(void 0, data.config.theme)) {
98
+ if (!theme && _chunk2LNCAUCDjs.resolveThemeName.call(void 0, newData.config.theme) !== _chunk2LNCAUCDjs.resolveThemeName.call(void 0, data.config.theme)) {
99
99
  console.log(_kolorist.yellow.call(void 0, "\n restarting on theme change\n"));
100
100
  initServer();
101
101
  } else if (CONFIG_RESTART_FIELDS.some((i) => !(0, import_fast_deep_equal.default)(newData.config[i], data.config[i]))) {
@@ -171,8 +171,8 @@ cli.command("build [entry]", "Build hostable SPA", (args) => commonOptions(args)
171
171
  type: "boolean",
172
172
  describe: "allow download as PDF"
173
173
  }).strict().help(), async ({ entry, theme, watch, base, download, out }) => {
174
- const { build } = await Promise.resolve().then(() => _chunk7QXPSYEZjs.__toModule.call(void 0, _chunk7QXPSYEZjs.__require.call(void 0, "./build-IGNJAAON.js")));
175
- const options = await _chunkZ6YPUFV4js.resolveOptions.call(void 0, { entry, theme }, "build");
174
+ const { build } = await Promise.resolve().then(() => _chunkIXNT7VMKjs.__toESM.call(void 0, _chunkIXNT7VMKjs.__require.call(void 0, "./build-23SNLQAJ.js")));
175
+ const options = await _chunk2LNCAUCDjs.resolveOptions.call(void 0, { entry, theme }, "build");
176
176
  if (download && !options.data.config.download)
177
177
  options.data.config.download = download;
178
178
  printInfo(options);
@@ -185,26 +185,26 @@ cli.command("build [entry]", "Build hostable SPA", (args) => commonOptions(args)
185
185
  });
186
186
  });
187
187
  cli.command("format [entry]", "Format the markdown file", (args) => commonOptions(args).strict().help(), async ({ entry }) => {
188
- const data = await _chunkZ6YPUFV4js.fs_exports.load(entry);
189
- _chunkZ6YPUFV4js.fs_exports.prettify(data);
190
- await _chunkZ6YPUFV4js.fs_exports.save(data);
188
+ const data = await _chunk2LNCAUCDjs.fs_exports.load(entry);
189
+ _chunk2LNCAUCDjs.fs_exports.prettify(data);
190
+ await _chunk2LNCAUCDjs.fs_exports.save(data);
191
191
  });
192
192
  cli.command("theme [subcommand]", "Theme related operations", (command) => {
193
193
  return command.command("eject", "Eject current theme into local file system", (args) => commonOptions(args).option("dir", {
194
194
  type: "string",
195
195
  default: "theme"
196
196
  }), async ({ entry, dir, theme: themeInput }) => {
197
- const data = await _chunkZ6YPUFV4js.fs_exports.load(entry);
198
- const theme = _chunkZ6YPUFV4js.resolveThemeName.call(void 0, themeInput || data.config.theme);
197
+ const data = await _chunk2LNCAUCDjs.fs_exports.load(entry);
198
+ const theme = _chunk2LNCAUCDjs.resolveThemeName.call(void 0, themeInput || data.config.theme);
199
199
  if (theme === "none") {
200
200
  console.error('Can not eject theme "none"');
201
201
  process.exit(1);
202
202
  }
203
- if (_chunkZ6YPUFV4js.isPath.call(void 0, theme)) {
203
+ if (_chunk2LNCAUCDjs.isPath.call(void 0, theme)) {
204
204
  console.error("Theme is already ejected");
205
205
  process.exit(1);
206
206
  }
207
- const roots = _chunkZ6YPUFV4js.getThemeRoots.call(void 0, theme, entry);
207
+ const roots = _chunk2LNCAUCDjs.getThemeRoots.call(void 0, theme, entry);
208
208
  if (!roots.length) {
209
209
  console.error(`Does not found theme "${theme}"`);
210
210
  process.exit(1);
@@ -216,7 +216,7 @@ cli.command("theme [subcommand]", "Theme related operations", (command) => {
216
216
  const dirPath = `./${dir}`;
217
217
  data.slides[0].frontmatter.theme = dirPath;
218
218
  data.slides[0].raw = null;
219
- await _chunkZ6YPUFV4js.fs_exports.save(data);
219
+ await _chunk2LNCAUCDjs.fs_exports.save(data);
220
220
  console.log(`Theme "${theme}" ejected successfully to "${dirPath}"`);
221
221
  });
222
222
  }, () => {
@@ -229,12 +229,12 @@ cli.command("export [entry]", "Export slides to PDF", (args) => commonOptions(ar
229
229
  }).option("format", {
230
230
  default: "pdf",
231
231
  type: "string",
232
- choices: ["pdf", "png"],
232
+ choices: ["pdf", "png", "md"],
233
233
  describe: "output format"
234
234
  }).option("timeout", {
235
- default: 100,
235
+ default: 3e4,
236
236
  type: "number",
237
- describe: "timeout for rendering each page"
237
+ describe: "timeout for rendering the print page"
238
238
  }).option("range", {
239
239
  type: "string",
240
240
  describe: 'page ranges to export, for example "1,4-5,6"'
@@ -257,22 +257,23 @@ cli.command("export [entry]", "Export slides to PDF", (args) => commonOptions(ar
257
257
  dark,
258
258
  "with-clicks": withClicks
259
259
  }) => {
260
- output = output || `${_path2.default.basename(entry, ".md")}-export`;
261
260
  process.env.NODE_ENV = "production";
262
- const { exportSlides } = await Promise.resolve().then(() => _chunk7QXPSYEZjs.__toModule.call(void 0, _chunk7QXPSYEZjs.__require.call(void 0, "./export-XA3FQNS5.js")));
261
+ const { exportSlides } = await Promise.resolve().then(() => _chunkIXNT7VMKjs.__toESM.call(void 0, _chunkIXNT7VMKjs.__require.call(void 0, "./export-AKEY52VC.js")));
263
262
  const port = await findFreePort(12445);
264
- const options = await _chunkZ6YPUFV4js.resolveOptions.call(void 0, { entry, theme }, "build");
265
- const server = await _chunkXI447TZRjs.createServer.call(void 0, options, {
263
+ const options = await _chunk2LNCAUCDjs.resolveOptions.call(void 0, { entry, theme }, "build");
264
+ output = output || options.data.config.exportFilename || `${_path2.default.basename(entry, ".md")}-export`;
265
+ const server = await _chunkE4IOOAKNjs.createServer.call(void 0, options, {
266
266
  server: { port },
267
267
  clearScreen: false
268
268
  });
269
269
  await server.listen(port);
270
270
  printInfo(options);
271
- _chunkZ6YPUFV4js.fs_exports.filterDisabled(options.data);
272
- const width = 1920;
271
+ _chunk2LNCAUCDjs.fs_exports.filterDisabled(options.data);
272
+ const width = options.data.config.canvasWidth;
273
273
  const height = Math.round(width / options.data.config.aspectRatio);
274
274
  output = await exportSlides({
275
275
  port,
276
+ slides: options.data.slides,
276
277
  total: options.data.slides.length,
277
278
  range,
278
279
  format,
@@ -305,7 +306,7 @@ function printInfo(options, port, remote) {
305
306
  console.log();
306
307
  console.log();
307
308
  console.log(` ${_kolorist.cyan.call(void 0, "\u25CF") + _kolorist.blue.call(void 0, "\u25A0") + _kolorist.yellow.call(void 0, "\u25B2")}`);
308
- console.log(`${_kolorist.bold.call(void 0, " Slidev")} ${_kolorist.blue.call(void 0, `v${_chunkZ6YPUFV4js.version}`)} ${_isinstalledglobally2.default ? _kolorist.yellow.call(void 0, "(global)") : ""}`);
309
+ console.log(`${_kolorist.bold.call(void 0, " Slidev")} ${_kolorist.blue.call(void 0, `v${_chunk2LNCAUCDjs.version}`)} ${_isinstalledglobally2.default ? _kolorist.yellow.call(void 0, "(global)") : ""}`);
309
310
  console.log();
310
311
  console.log(_kolorist.dim.call(void 0, " theme ") + (options.theme ? _kolorist.green.call(void 0, options.theme) : _kolorist.gray.call(void 0, "none")));
311
312
  console.log(_kolorist.dim.call(void 0, " entry ") + _kolorist.dim.call(void 0, _path2.default.dirname(options.entry) + _path2.default.sep) + _path2.default.basename(options.entry));
package/dist/cli.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  createServer
3
- } from "./chunk-XMJRAMYY.mjs";
3
+ } from "./chunk-O57F34AM.mjs";
4
4
  import {
5
5
  fs_exports,
6
6
  getThemeRoots,
@@ -8,20 +8,20 @@ import {
8
8
  resolveOptions,
9
9
  resolveThemeName,
10
10
  version
11
- } from "./chunk-XDEB4KBK.mjs";
11
+ } from "./chunk-OII7IQD2.mjs";
12
12
  import {
13
13
  require_fast_deep_equal
14
- } from "./chunk-NQIXBLV4.mjs";
14
+ } from "./chunk-MBZTOLHN.mjs";
15
15
  import {
16
16
  __dirname,
17
17
  __require,
18
- __toModule,
18
+ __toESM,
19
19
  init_esm_shims
20
- } from "./chunk-VYBQLH2X.mjs";
20
+ } from "./chunk-XMETW2MS.mjs";
21
21
 
22
22
  // node/cli.ts
23
23
  init_esm_shims();
24
- var import_fast_deep_equal = __toModule(require_fast_deep_equal());
24
+ var import_fast_deep_equal = __toESM(require_fast_deep_equal());
25
25
  import path from "path";
26
26
  import net from "net";
27
27
  import os from "os";
@@ -172,7 +172,7 @@ cli.command("build [entry]", "Build hostable SPA", (args) => commonOptions(args)
172
172
  type: "boolean",
173
173
  describe: "allow download as PDF"
174
174
  }).strict().help(), async ({ entry, theme, watch, base, download, out }) => {
175
- const { build } = await Promise.resolve().then(() => __toModule(__require("./build-RNBUEMO5.mjs")));
175
+ const { build } = await Promise.resolve().then(() => __toESM(__require("./build-YFDGZUAK.mjs")));
176
176
  const options = await resolveOptions({ entry, theme }, "build");
177
177
  if (download && !options.data.config.download)
178
178
  options.data.config.download = download;
@@ -230,12 +230,12 @@ cli.command("export [entry]", "Export slides to PDF", (args) => commonOptions(ar
230
230
  }).option("format", {
231
231
  default: "pdf",
232
232
  type: "string",
233
- choices: ["pdf", "png"],
233
+ choices: ["pdf", "png", "md"],
234
234
  describe: "output format"
235
235
  }).option("timeout", {
236
- default: 100,
236
+ default: 3e4,
237
237
  type: "number",
238
- describe: "timeout for rendering each page"
238
+ describe: "timeout for rendering the print page"
239
239
  }).option("range", {
240
240
  type: "string",
241
241
  describe: 'page ranges to export, for example "1,4-5,6"'
@@ -258,11 +258,11 @@ cli.command("export [entry]", "Export slides to PDF", (args) => commonOptions(ar
258
258
  dark,
259
259
  "with-clicks": withClicks
260
260
  }) => {
261
- output = output || `${path.basename(entry, ".md")}-export`;
262
261
  process.env.NODE_ENV = "production";
263
- const { exportSlides } = await Promise.resolve().then(() => __toModule(__require("./export-4MUQJ4DB.mjs")));
262
+ const { exportSlides } = await Promise.resolve().then(() => __toESM(__require("./export-RFCVIBYE.mjs")));
264
263
  const port = await findFreePort(12445);
265
264
  const options = await resolveOptions({ entry, theme }, "build");
265
+ output = output || options.data.config.exportFilename || `${path.basename(entry, ".md")}-export`;
266
266
  const server = await createServer(options, {
267
267
  server: { port },
268
268
  clearScreen: false
@@ -270,10 +270,11 @@ cli.command("export [entry]", "Export slides to PDF", (args) => commonOptions(ar
270
270
  await server.listen(port);
271
271
  printInfo(options);
272
272
  fs_exports.filterDisabled(options.data);
273
- const width = 1920;
273
+ const width = options.data.config.canvasWidth;
274
274
  const height = Math.round(width / options.data.config.aspectRatio);
275
275
  output = await exportSlides({
276
276
  port,
277
+ slides: options.data.slides,
277
278
  total: options.data.slides.length,
278
279
  range,
279
280
  format,
@@ -1,21 +1,20 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2
2
 
3
- var _chunkZ6YPUFV4js = require('./chunk-Z6YPUFV4.js');
3
+ var _chunk2LNCAUCDjs = require('./chunk-2LNCAUCD.js');
4
4
 
5
5
 
6
6
 
7
7
 
8
- var _chunk7QXPSYEZjs = require('./chunk-7QXPSYEZ.js');
8
+ var _chunkIXNT7VMKjs = require('./chunk-IXNT7VMK.js');
9
9
 
10
10
  // node/export.ts
11
- _chunk7QXPSYEZjs.init_cjs_shims.call(void 0, );
11
+ _chunkIXNT7VMKjs.init_cjs_shims.call(void 0, );
12
12
  var _path = require('path'); var _path2 = _interopRequireDefault(_path);
13
13
  var _fsextra = require('fs-extra'); var _fsextra2 = _interopRequireDefault(_fsextra);
14
- var _pdflib = require('pdf-lib');
15
14
  var _kolorist = require('kolorist');
16
15
  var _cliprogress = require('cli-progress');
17
16
  var _core = require('@slidev/parser/core');
18
- function createSlidevProgress() {
17
+ function createSlidevProgress(indeterminate = false) {
19
18
  function getSpinner(n = 0) {
20
19
  return [_kolorist.cyan.call(void 0, "\u25CF"), _kolorist.green.call(void 0, "\u25C6"), _kolorist.blue.call(void 0, "\u25A0"), _kolorist.yellow.call(void 0, "\u25B2")][n % 4];
21
20
  }
@@ -25,7 +24,7 @@ function createSlidevProgress() {
25
24
  const progress = new (0, _cliprogress.SingleBar)({
26
25
  clearOnComplete: true,
27
26
  hideCursor: true,
28
- format: ` {spin} ${_kolorist.yellow.call(void 0, "rendering")} {bar} {value}/{total}`,
27
+ format: ` {spin} ${_kolorist.yellow.call(void 0, "rendering")}${indeterminate ? _kolorist.dim.call(void 0, _kolorist.yellow.call(void 0, "...")) : " {bar} {value}/{total}"}`,
29
28
  linewrap: false,
30
29
  barsize: 30
31
30
  }, _cliprogress.Presets.shades_grey);
@@ -54,96 +53,102 @@ async function exportSlides({
54
53
  range,
55
54
  format = "pdf",
56
55
  output = "slides",
56
+ slides,
57
57
  base = "/",
58
- timeout = 500,
58
+ timeout = 3e4,
59
59
  dark = false,
60
60
  routerMode = "history",
61
61
  width = 1920,
62
62
  height = 1080,
63
63
  withClicks = false
64
64
  }) {
65
- if (!_chunkZ6YPUFV4js.packageExists.call(void 0, "playwright-chromium"))
65
+ if (!_chunk2LNCAUCDjs.packageExists.call(void 0, "playwright-chromium"))
66
66
  throw new Error("The exporting for Slidev is powered by Playwright, please installed it via `npm i -D playwright-chromium`");
67
- const { chromium } = await Promise.resolve().then(() => _chunk7QXPSYEZjs.__toModule.call(void 0, _chunk7QXPSYEZjs.__require.call(void 0, "playwright-chromium")));
67
+ const pages = _core.parseRangeString.call(void 0, total, range);
68
+ const { chromium } = await Promise.resolve().then(() => _chunkIXNT7VMKjs.__toESM.call(void 0, _chunkIXNT7VMKjs.__require.call(void 0, "playwright-chromium")));
68
69
  const browser = await chromium.launch();
69
70
  const context = await browser.newContext({
70
71
  viewport: {
71
72
  width,
72
- height
73
+ height: height * pages.length
73
74
  },
74
75
  deviceScaleFactor: 1
75
76
  });
76
77
  const page = await context.newPage();
77
- const progress = createSlidevProgress();
78
+ const progress = createSlidevProgress(true);
78
79
  async function go(no, clicks) {
79
- progress.update(no);
80
80
  const path2 = `${no}?print${withClicks ? "=clicks" : ""}${clicks ? `&clicks=${clicks}` : ""}`;
81
81
  const url = routerMode === "hash" ? `http://localhost:${port}${base}#${path2}` : `http://localhost:${port}${base}${path2}`;
82
82
  await page.goto(url, {
83
- waitUntil: "networkidle"
83
+ waitUntil: "networkidle",
84
+ timeout
84
85
  });
85
- await page.waitForTimeout(timeout);
86
86
  await page.waitForLoadState("networkidle");
87
87
  await page.emulateMedia({ colorScheme: dark ? "dark" : "light", media: "screen" });
88
+ const elements = await page.locator("[data-waitfor]");
89
+ const count = await elements.count();
90
+ for (let index = 0; index < count; index++) {
91
+ const element = await elements.nth(index);
92
+ const attribute = await element.getAttribute("data-waitfor");
93
+ if (attribute)
94
+ await element.locator(attribute).waitFor();
95
+ }
96
+ const frames = await page.frames();
97
+ await Promise.all(frames.map((frame) => frame.waitForLoadState()));
88
98
  }
89
- function getClicks(url) {
90
- var _a;
91
- return (_a = url.match(/clicks=([1-9][0-9]*)/)) == null ? void 0 : _a[1];
99
+ async function genPagePdf() {
100
+ if (!output.endsWith(".pdf"))
101
+ output = `${output}.pdf`;
102
+ await go("print");
103
+ await page.pdf({
104
+ path: output,
105
+ width,
106
+ height,
107
+ margin: {
108
+ left: 0,
109
+ top: 0,
110
+ right: 0,
111
+ bottom: 0
112
+ },
113
+ printBackground: true,
114
+ preferCSSPageSize: true
115
+ });
92
116
  }
93
- async function genPageWithClicks(fn, i, clicks) {
94
- await fn(i, clicks);
95
- if (withClicks) {
96
- await page.keyboard.press("ArrowRight", { delay: 100 });
97
- const _clicks = getClicks(page.url());
98
- if (_clicks && clicks !== _clicks)
99
- await genPageWithClicks(fn, i, _clicks);
117
+ async function genPagePng() {
118
+ await go("print");
119
+ const slides2 = await page.locator(".slide-container");
120
+ const count = await slides2.count();
121
+ for (let i = 0; i < count; i++) {
122
+ progress.update(i + 1);
123
+ const buffer = await slides2.nth(i).screenshot();
124
+ await _fsextra2.default.ensureDir(output);
125
+ await _fsextra2.default.writeFile(_path2.default.join(output, `${(i + 1).toString().padStart(2, "0")}.png`), buffer);
100
126
  }
101
127
  }
102
- const pages = _core.parseRangeString.call(void 0, total, range);
128
+ async function genPageMd(pages2, slides2) {
129
+ var _a, _b, _c;
130
+ const mds = [];
131
+ for (const i of pages2) {
132
+ const mdImg = `![${(_a = slides2[i - 1]) == null ? void 0 : _a.title}](./${output}/${i.toString().padStart(2, "0")}.png)
133
+
134
+ `;
135
+ const mdNote = ((_b = slides2[i - 1]) == null ? void 0 : _b.note) ? `${(_c = slides2[i - 1]) == null ? void 0 : _c.note}
136
+
137
+ ` : "";
138
+ mds.push(`${mdImg}${mdNote}`);
139
+ }
140
+ if (!output.endsWith(".md"))
141
+ output = `${output}.md`;
142
+ await _fsextra2.default.writeFile(output, mds.join(""));
143
+ }
103
144
  progress.start(pages.length);
104
145
  if (format === "pdf") {
105
- const buffers = [];
106
- const genPdfBuffer = async (i, clicks) => {
107
- await go(i, clicks);
108
- const pdf = await page.pdf({
109
- width,
110
- height,
111
- margin: {
112
- left: 0,
113
- top: 0,
114
- right: 0,
115
- bottom: 0
116
- },
117
- pageRanges: "1",
118
- printBackground: true,
119
- preferCSSPageSize: true
120
- });
121
- buffers.push(pdf);
122
- };
123
- for (const i of pages)
124
- await genPageWithClicks(genPdfBuffer, i);
125
- const mergedPdf = await _pdflib.PDFDocument.create({});
126
- for (const pdfBytes of buffers) {
127
- const pdf = await _pdflib.PDFDocument.load(pdfBytes);
128
- const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());
129
- copiedPages.forEach((page2) => {
130
- mergedPdf.addPage(page2);
131
- });
132
- }
133
- const buffer = await mergedPdf.save();
134
- if (!output.endsWith(".pdf"))
135
- output = `${output}.pdf`;
136
- await _fsextra2.default.writeFile(output, buffer);
146
+ await genPagePdf();
137
147
  } else if (format === "png") {
138
- const genScreenshot = async (i, clicks) => {
139
- await go(i, clicks);
140
- await page.screenshot({
141
- omitBackground: false,
142
- path: _path2.default.join(output, `${i.toString().padStart(2, "0")}${clicks ? `-${clicks}` : ""}.png`)
143
- });
144
- };
145
- for (const i of pages)
146
- await genPageWithClicks(genScreenshot, i);
148
+ await genPagePng();
149
+ } else if (format === "md") {
150
+ await genPagePng();
151
+ await genPageMd(pages, slides);
147
152
  } else {
148
153
  throw new Error(`Unsupported exporting format "${format}"`);
149
154
  }
@@ -1,21 +1,20 @@
1
1
  import {
2
2
  packageExists
3
- } from "./chunk-XDEB4KBK.mjs";
3
+ } from "./chunk-OII7IQD2.mjs";
4
4
  import {
5
5
  __require,
6
- __toModule,
6
+ __toESM,
7
7
  init_esm_shims
8
- } from "./chunk-VYBQLH2X.mjs";
8
+ } from "./chunk-XMETW2MS.mjs";
9
9
 
10
10
  // node/export.ts
11
11
  init_esm_shims();
12
12
  import path from "path";
13
13
  import fs from "fs-extra";
14
- import { PDFDocument } from "pdf-lib";
15
- import { blue, cyan, green, yellow } from "kolorist";
14
+ import { blue, cyan, dim, green, yellow } from "kolorist";
16
15
  import { Presets, SingleBar } from "cli-progress";
17
16
  import { parseRangeString } from "@slidev/parser/core";
18
- function createSlidevProgress() {
17
+ function createSlidevProgress(indeterminate = false) {
19
18
  function getSpinner(n = 0) {
20
19
  return [cyan("\u25CF"), green("\u25C6"), blue("\u25A0"), yellow("\u25B2")][n % 4];
21
20
  }
@@ -25,7 +24,7 @@ function createSlidevProgress() {
25
24
  const progress = new SingleBar({
26
25
  clearOnComplete: true,
27
26
  hideCursor: true,
28
- format: ` {spin} ${yellow("rendering")} {bar} {value}/{total}`,
27
+ format: ` {spin} ${yellow("rendering")}${indeterminate ? dim(yellow("...")) : " {bar} {value}/{total}"}`,
29
28
  linewrap: false,
30
29
  barsize: 30
31
30
  }, Presets.shades_grey);
@@ -54,8 +53,9 @@ async function exportSlides({
54
53
  range,
55
54
  format = "pdf",
56
55
  output = "slides",
56
+ slides,
57
57
  base = "/",
58
- timeout = 500,
58
+ timeout = 3e4,
59
59
  dark = false,
60
60
  routerMode = "history",
61
61
  width = 1920,
@@ -64,86 +64,91 @@ async function exportSlides({
64
64
  }) {
65
65
  if (!packageExists("playwright-chromium"))
66
66
  throw new Error("The exporting for Slidev is powered by Playwright, please installed it via `npm i -D playwright-chromium`");
67
- const { chromium } = await Promise.resolve().then(() => __toModule(__require("playwright-chromium")));
67
+ const pages = parseRangeString(total, range);
68
+ const { chromium } = await Promise.resolve().then(() => __toESM(__require("playwright-chromium")));
68
69
  const browser = await chromium.launch();
69
70
  const context = await browser.newContext({
70
71
  viewport: {
71
72
  width,
72
- height
73
+ height: height * pages.length
73
74
  },
74
75
  deviceScaleFactor: 1
75
76
  });
76
77
  const page = await context.newPage();
77
- const progress = createSlidevProgress();
78
+ const progress = createSlidevProgress(true);
78
79
  async function go(no, clicks) {
79
- progress.update(no);
80
80
  const path2 = `${no}?print${withClicks ? "=clicks" : ""}${clicks ? `&clicks=${clicks}` : ""}`;
81
81
  const url = routerMode === "hash" ? `http://localhost:${port}${base}#${path2}` : `http://localhost:${port}${base}${path2}`;
82
82
  await page.goto(url, {
83
- waitUntil: "networkidle"
83
+ waitUntil: "networkidle",
84
+ timeout
84
85
  });
85
- await page.waitForTimeout(timeout);
86
86
  await page.waitForLoadState("networkidle");
87
87
  await page.emulateMedia({ colorScheme: dark ? "dark" : "light", media: "screen" });
88
+ const elements = await page.locator("[data-waitfor]");
89
+ const count = await elements.count();
90
+ for (let index = 0; index < count; index++) {
91
+ const element = await elements.nth(index);
92
+ const attribute = await element.getAttribute("data-waitfor");
93
+ if (attribute)
94
+ await element.locator(attribute).waitFor();
95
+ }
96
+ const frames = await page.frames();
97
+ await Promise.all(frames.map((frame) => frame.waitForLoadState()));
88
98
  }
89
- function getClicks(url) {
90
- var _a;
91
- return (_a = url.match(/clicks=([1-9][0-9]*)/)) == null ? void 0 : _a[1];
99
+ async function genPagePdf() {
100
+ if (!output.endsWith(".pdf"))
101
+ output = `${output}.pdf`;
102
+ await go("print");
103
+ await page.pdf({
104
+ path: output,
105
+ width,
106
+ height,
107
+ margin: {
108
+ left: 0,
109
+ top: 0,
110
+ right: 0,
111
+ bottom: 0
112
+ },
113
+ printBackground: true,
114
+ preferCSSPageSize: true
115
+ });
92
116
  }
93
- async function genPageWithClicks(fn, i, clicks) {
94
- await fn(i, clicks);
95
- if (withClicks) {
96
- await page.keyboard.press("ArrowRight", { delay: 100 });
97
- const _clicks = getClicks(page.url());
98
- if (_clicks && clicks !== _clicks)
99
- await genPageWithClicks(fn, i, _clicks);
117
+ async function genPagePng() {
118
+ await go("print");
119
+ const slides2 = await page.locator(".slide-container");
120
+ const count = await slides2.count();
121
+ for (let i = 0; i < count; i++) {
122
+ progress.update(i + 1);
123
+ const buffer = await slides2.nth(i).screenshot();
124
+ await fs.ensureDir(output);
125
+ await fs.writeFile(path.join(output, `${(i + 1).toString().padStart(2, "0")}.png`), buffer);
100
126
  }
101
127
  }
102
- const pages = parseRangeString(total, range);
128
+ async function genPageMd(pages2, slides2) {
129
+ var _a, _b, _c;
130
+ const mds = [];
131
+ for (const i of pages2) {
132
+ const mdImg = `![${(_a = slides2[i - 1]) == null ? void 0 : _a.title}](./${output}/${i.toString().padStart(2, "0")}.png)
133
+
134
+ `;
135
+ const mdNote = ((_b = slides2[i - 1]) == null ? void 0 : _b.note) ? `${(_c = slides2[i - 1]) == null ? void 0 : _c.note}
136
+
137
+ ` : "";
138
+ mds.push(`${mdImg}${mdNote}`);
139
+ }
140
+ if (!output.endsWith(".md"))
141
+ output = `${output}.md`;
142
+ await fs.writeFile(output, mds.join(""));
143
+ }
103
144
  progress.start(pages.length);
104
145
  if (format === "pdf") {
105
- const buffers = [];
106
- const genPdfBuffer = async (i, clicks) => {
107
- await go(i, clicks);
108
- const pdf = await page.pdf({
109
- width,
110
- height,
111
- margin: {
112
- left: 0,
113
- top: 0,
114
- right: 0,
115
- bottom: 0
116
- },
117
- pageRanges: "1",
118
- printBackground: true,
119
- preferCSSPageSize: true
120
- });
121
- buffers.push(pdf);
122
- };
123
- for (const i of pages)
124
- await genPageWithClicks(genPdfBuffer, i);
125
- const mergedPdf = await PDFDocument.create({});
126
- for (const pdfBytes of buffers) {
127
- const pdf = await PDFDocument.load(pdfBytes);
128
- const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());
129
- copiedPages.forEach((page2) => {
130
- mergedPdf.addPage(page2);
131
- });
132
- }
133
- const buffer = await mergedPdf.save();
134
- if (!output.endsWith(".pdf"))
135
- output = `${output}.pdf`;
136
- await fs.writeFile(output, buffer);
146
+ await genPagePdf();
137
147
  } else if (format === "png") {
138
- const genScreenshot = async (i, clicks) => {
139
- await go(i, clicks);
140
- await page.screenshot({
141
- omitBackground: false,
142
- path: path.join(output, `${i.toString().padStart(2, "0")}${clicks ? `-${clicks}` : ""}.png`)
143
- });
144
- };
145
- for (const i of pages)
146
- await genPageWithClicks(genScreenshot, i);
148
+ await genPagePng();
149
+ } else if (format === "md") {
150
+ await genPagePng();
151
+ await genPageMd(pages, slides);
147
152
  } else {
148
153
  throw new Error(`Unsupported exporting format "${format}"`);
149
154
  }