Skip to content

Commit

Permalink
feat: support custom download url for build
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 29, 2021
1 parent 204105f commit 06901fc
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions demo/slides.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
layout: cover
download: https://antfu.me/talks/2021-04-29
---

# Composable Vue
Expand Down
12 changes: 10 additions & 2 deletions docs/guide/exporting.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Exporting

> Exporting is relying on [Playwright](https://playwright.dev) to do the rendering, you will need to install [`playwright-chromium`](https://playwright.dev/docs/installation#download-single-browser-binary) for using this feature.
> If you are doing exporting in a CI environment, [this guide](https://playwright.dev/docs/ci) could also be helpful.
## PDF

Exporting your slides into PDF by the following command
Expand Down Expand Up @@ -35,11 +38,16 @@ You can provide a downloadable PDF to the viewers of your SPA. You can enable it

```md
---
allow-download: true
download: true
---
```

It will generate a pdf file along with the build. And a download button will appear in the SPA.

If you are building it in a CI environment, you might need to have [`playwright-chromium`](https://playwright.dev/docs/installation#download-single-browser-binary) installed as well. Learn more [here](https://playwright.dev/docs/ci)
You can also provide a custom url to the PDF. And if so the rendering process will be skipped.

```md
---
download: 'https://myside.com/my-talk.pdf'
---
```
14 changes: 10 additions & 4 deletions packages/client/internals/NavControls.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { useFullscreen } from '@vueuse/core'
import { computed, defineProps } from 'vue'
import { isString } from '@antfu/utils'
import { isDark, toggleDark } from '../logic/dark'
import { hasNext, hasPrev, prev, next, isPresenter, currentPage } from '../logic/nav'
import { showOverview, showEditor } from '../state'
Expand All @@ -18,9 +19,14 @@ const { isFullscreen, toggle: toggleFullscreen } = useFullscreen(document.body)
const presenterLink = computed(() => `${location.origin}/presenter/${currentPage.value}`)
const nonPresenterLink = computed(() => `${location.origin}/${currentPage.value}`)
function downloadPDF() {
import('file-saver')
.then(i => i.saveAs(`${import.meta.env.BASE_URL}slidev-exported.pdf`, `${configs.title}.pdf`))
async function downloadPDF() {
const { saveAs } = await import('file-saver')
saveAs(
isString(configs.download)
? configs.download
: `${import.meta.env.BASE_URL}slidev-exported.pdf`,
`${configs.title}.pdf`,
)
}
</script>

Expand Down Expand Up @@ -68,7 +74,7 @@ function downloadPDF() {
</button>
</template>
<template v-else>
<button v-if="configs.allowDownload" class="icon-btn" @click="downloadPDF">
<button v-if="configs.download" class="icon-btn" @click="downloadPDF">
<carbon:download />
</button>
</template>
Expand Down
2 changes: 1 addition & 1 deletion packages/slidev/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function build(
await fs.unlink(indexPath)
}

if (options.data.config.allowDownload) {
if (options.data.config.download === true || options.data.config.download === 'auto') {
const port = 12445
const app = connect()
const server = http.createServer(app)
Expand Down
8 changes: 4 additions & 4 deletions packages/slidev/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,18 @@ cli.command(
type: 'string',
describe: 'output base',
})
.option('allowDownload', {
.option('download', {
alias: 'd',
type: 'boolean',
describe: 'allow download as PDF',
})
.help(),
async({ entry, theme, watch, base, allowDownload }) => {
async({ entry, theme, watch, base, download }) => {
const { build } = await import('./build')

const options = await resolveOptions({ entry, theme }, 'build')
if (allowDownload)
options.data.config.allowDownload = allowDownload
if (download && !options.data.config.download)
options.data.config.download = download

printInfo(options)
await build(options, {}, {
Expand Down
2 changes: 1 addition & 1 deletion packages/slidev/node/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function parse(
config.title ??= headmatter.title ?? (slides[0].content.match(/^# (.*)$/m)?.[1] || '').trim()
config.remoteAssets ??= headmatter.remoteAssets ?? true
config.monaco ??= headmatter.monaco ?? 'dev'
config.allowDownload ??= (<any>config)['allow-download'] ?? headmatter.allowDownload ?? headmatter['allow-download'] ?? true
config.download ??= headmatter.download ?? false

return {
raw: markdown,
Expand Down
2 changes: 1 addition & 1 deletion packages/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface SlidevConfig {
/**
* @default true
*/
allowDownload: boolean
download: boolean | string
}

export interface SlidevMarkdown {
Expand Down

0 comments on commit 06901fc

Please sign in to comment.