Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --wait option when exporting #1472

Merged
merged 4 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/guide/exporting.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ For big presentations you might want to increase the Playwright timeout with `--
$ slidev export --timeout 60000
```

### Wait

Some parts of your slides may require a longer time to render. You can use the `--wait` option to
have an extra delay before exporting.

```bash
$ slidev export --wait 10000
```

### Executable path

You can set the browser executable path for Playwright using `--executable-path`
Expand Down
11 changes: 11 additions & 0 deletions packages/slidev/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,18 @@ cli.command(
type: 'number',
describe: 'timeout for rendering the print page',
})
.option('wait', {
default: 0,
type: 'number',
describe: 'wait for the specified ms before exporting',
})
.strict()
.help(),
async ({
entry,
output,
timeout,
wait,
}) => {
const { exportNotes } = await import('./commands/export')
const port = await getPort(12445)
Expand All @@ -528,6 +534,7 @@ cli.command(
port,
output: output || (options.data.config.exportFilename ? `${options.data.config.exportFilename}-notes` : `${path.basename(entryFile, '.md')}-export-notes`),
timeout,
wait,
})
console.log(`${green(' ✓ ')}${dim('exported to ')}./${result}\n`)

Expand Down Expand Up @@ -571,6 +578,10 @@ function exportOptions<T>(args: Argv<T>) {
type: 'number',
describe: 'timeout for rendering the print page',
})
.option('wait', {
type: 'number',
describe: 'wait for the specified ms before exporting',
})
.option('range', {
type: 'string',
describe: 'page ranges to export, for example "1,4-5,6"',
Expand Down
12 changes: 12 additions & 0 deletions packages/slidev/node/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface ExportOptions {
format?: 'pdf' | 'png' | 'md'
output?: string
timeout?: number
wait?: number
dark?: boolean
routerMode?: 'hash' | 'history'
width?: number
Expand Down Expand Up @@ -69,6 +70,7 @@ export interface ExportNotesOptions {
base?: string
output?: string
timeout?: number
wait?: number
}

function createSlidevProgress(indeterminate = false) {
Expand Down Expand Up @@ -112,6 +114,7 @@ export async function exportNotes({
base = '/',
output = 'notes',
timeout = 30000,
wait = 0,
}: ExportNotesOptions): Promise<string> {
const { chromium } = await importPlaywright()
const browser = await chromium.launch()
Expand All @@ -129,6 +132,9 @@ export async function exportNotes({
await page.waitForLoadState('networkidle')
await page.emulateMedia({ media: 'screen' })

if (wait)
await page.waitForTimeout(wait)

await page.pdf({
path: output,
margin: {
Expand Down Expand Up @@ -156,6 +162,7 @@ export async function exportSlides({
slides,
base = '/',
timeout = 30000,
wait = 0,
dark = false,
routerMode = 'history',
width = 1920,
Expand Down Expand Up @@ -260,6 +267,9 @@ export async function exportSlides({
await element.evaluate(node => node.style.display = 'none')
}
}
// Wait for the given time
if (wait)
await page.waitForTimeout(wait)
}

async function getSlidesIndex() {
Expand Down Expand Up @@ -503,6 +513,7 @@ export function getExportOptions(args: ExportArgs, options: ResolvedSlidevOption
output,
format,
timeout,
wait,
range,
dark,
withClicks,
Expand All @@ -521,6 +532,7 @@ export function getExportOptions(args: ExportArgs, options: ResolvedSlidevOption
range,
format: (format || 'pdf') as 'pdf' | 'png' | 'md',
timeout: timeout ?? 30000,
wait: wait ?? 0,
dark: dark || options.data.config.colorSchema === 'dark',
routerMode: options.data.config.routerMode,
width: options.data.config.canvasWidth,
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface ExportArgs extends CommonArgs {
'output'?: string
'format'?: string
'timeout'?: number
'wait'?: number
'range'?: string
'dark'?: boolean
'with-clicks'?: boolean
Expand Down