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 CLI option to allow transparent pngs #1797

Merged
merged 7 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/builtin/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Options:
- `--dark` (`boolean`, default: `false`): export as dark theme.
- `--with-clicks`, `-c` (`boolean`, default: `false`): export pages for every click animation (see https://sli.dev/guide/animations.html#click-animation).
- `--theme`, `-t` (`string`): override theme.
- `--transparent` (`boolean`, default: `false`): remove the default browser background

## `slidev format [entry]` {#format}

Expand Down
15 changes: 15 additions & 0 deletions docs/guide/exporting.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,18 @@ You can generate the PDF outline by passing the `--with-toc` option:
```bash
$ slidev export --with-toc
```
### Transparent PNGs

You can remove the default browser background by passing `--transparent`:

```bash
$ slidev export --transparent
```
The default browser background is the white background visible on all browser windows and is different than other backgrounds applied throughout the application using CSS styling. [See Playwright docs](https://playwright.dev/docs/api/class-page#page-screenshot-option-omit-background). You will then need to apply additional CSS styling to the application to reveal the transparency.

Here is a basic example that covers all backgrounds in the application:
```css
* {
background: transparent !important;
}
```
4 changes: 4 additions & 0 deletions packages/slidev/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,10 @@ function exportOptions<T>(args: Argv<T>) {
type: 'number',
describe: 'scale factor for image export',
})
.option('transparent', {
type: 'boolean',
describe: 'export png pages without the default browser background',
})
}

function printInfo(
Expand Down
12 changes: 10 additions & 2 deletions packages/slidev/node/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface ExportOptions {
*/
perSlide?: boolean
scale?: number
transparent?: boolean
}

interface ExportPngResult {
Expand Down Expand Up @@ -183,6 +184,7 @@ export async function exportSlides({
perSlide = false,
scale = 1,
waitUntil,
transparent = false,
}: ExportOptions) {
const pages: number[] = parseRangeString(total, range)

Expand Down Expand Up @@ -402,7 +404,9 @@ export async function exportSlides({
progress.update(i + 1)
const id = (await slideContainers.nth(i).getAttribute('id')) || ''
const slideNo = +id.split('-')[0]
const buffer = await slideContainers.nth(i).screenshot()
const buffer = await slideContainers.nth(i).screenshot({
omitBackground: transparent,
})
result.push({ slideIndex: slideNo - 1, buffer })
if (writeToDisk)
await fs.writeFile(path.join(output, `${withClicks ? id : slideNo}.png`), buffer)
Expand All @@ -414,7 +418,9 @@ export async function exportSlides({
const result: ExportPngResult[] = []
const genScreenshot = async (no: number, clicks?: string) => {
await go(no, clicks)
const buffer = await page.screenshot()
const buffer = await page.screenshot({
omitBackground: transparent,
})
result.push({ slideIndex: no - 1, buffer })
if (writeToDisk) {
await fs.writeFile(
Expand Down Expand Up @@ -585,6 +591,7 @@ export function getExportOptions(args: ExportArgs, options: ResolvedSlidevOption
withToc,
perSlide,
scale,
transparent,
} = config
outFilename = output || options.data.config.exportFilename || outFilename || `${path.basename(entry, '.md')}-export`
if (outDir)
Expand All @@ -607,6 +614,7 @@ export function getExportOptions(args: ExportArgs, options: ResolvedSlidevOption
withToc: withToc || false,
perSlide: perSlide || false,
scale: scale || 2,
transparent: transparent ?? false,
}
}

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 @@ -16,6 +16,7 @@ export interface ExportArgs extends CommonArgs {
'with-toc'?: boolean
'per-slide'?: boolean
'scale'?: number
'transparent'?: boolean
}

export interface BuildArgs extends ExportArgs {
Expand Down
Loading