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(export): export pdf with outlines if --with-toc option is specified #669

Merged
merged 4 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions packages/slidev/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ cli.command(
type: 'string',
describe: 'executable to override playwright bundled browser',
})
.option('with-toc', {
default: false,
type: 'boolean',
describe: 'export pages with outline',
})
.strict()
.help(),
async ({
Expand All @@ -348,6 +353,7 @@ cli.command(
dark,
'with-clicks': withClicks,
'executable-path': executablePath,
'with-toc': withTOC,
}) => {
process.env.NODE_ENV = 'production'
const { exportSlides } = await import('./export')
Expand Down Expand Up @@ -380,6 +386,7 @@ cli.command(
height,
withClicks,
executablePath,
withTOC,
})
console.log(`${green(' ✓ ')}${dim('exported to ')}./${output}\n`)
server.close()
Expand Down
54 changes: 54 additions & 0 deletions packages/slidev/node/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { blue, cyan, dim, green, yellow } from 'kolorist'
import { Presets, SingleBar } from 'cli-progress'
import { parseRangeString } from '@slidev/parser/core'
import type { SlideInfo } from '@slidev/types'
import { outlinePdfFactory } from '@lillallol/outline-pdf'
import * as pdfLib from 'pdf-lib'
import { packageExists } from './utils'

export interface ExportOptions {
Expand All @@ -21,6 +23,40 @@ export interface ExportOptions {
height?: number
withClicks?: boolean
executablePath?: string
withTOC?: boolean
}

interface TocItem {
children: TocItem[]
level: number
index: number
title?: string
}

function addToTree(tree: TocItem[], info: SlideInfo, level = 1) {
const titleLevel = info.level
if (titleLevel && titleLevel > level && tree.length > 0) {
addToTree(tree[tree.length - 1].children, info, level + 1)
}
else {
tree.push({
children: [],
level,
index: info.index,
title: info.title,
})
}
}

function makeOutline(tree: TocItem[]): string {
return tree.map(({ title, index, level, children }) => {
const rootOutline = title ? `${index + 1}|${'-'.repeat(level - 1)}|${title}` : null

const childrenOutline = makeOutline(children)

return childrenOutline.length > 0 ? `${rootOutline}\n${childrenOutline}` : rootOutline
},
).filter(outline => !!outline).join('\n')
}

export interface ExportNotesOptions {
Expand Down Expand Up @@ -122,6 +158,7 @@ export async function exportSlides({
height = 1080,
withClicks = false,
executablePath = undefined,
withTOC = false,
}: ExportOptions) {
if (!packageExists('playwright-chromium'))
throw new Error('The exporting for Slidev is powered by Playwright, please installed it via `npm i -D playwright-chromium`')
Expand Down Expand Up @@ -185,6 +222,23 @@ export async function exportSlides({
printBackground: true,
preferCSSPageSize: true,
})

if (withTOC) {
const outlinePdf = outlinePdfFactory(pdfLib)
const pdf = fs.readFileSync(output, { encoding: 'base64' })

const tocTree = slides.filter(slide => slide.title)
.reduce((acc: TocItem[], slide) => {
addToTree(acc, slide)
return acc
}, [])

const outline = makeOutline(tocTree)

const outlinedPdfDocument = await outlinePdf({ outline, pdf })
const outlinedPdf = await outlinedPdfDocument.save()
fs.writeFileSync(output, outlinedPdf)
}
}

async function genPagePng() {
Expand Down
2 changes: 2 additions & 0 deletions packages/slidev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@hedgedoc/markdown-it-task-lists": "^1.0.5",
"@iconify-json/carbon": "^1.1.8",
"@iconify-json/ph": "^1.1.2",
"@lillallol/outline-pdf": "^4.0.0",
"@slidev/client": "workspace:*",
"@slidev/parser": "workspace:*",
"@slidev/types": "workspace:*",
Expand All @@ -75,6 +76,7 @@
"monaco-editor": "^0.33.0",
"nanoid": "^4.0.0",
"open": "^8.4.0",
"pdf-lib": "^1.17.1",
"plantuml-encoder": "^1.4.0",
"postcss-nested": "^6.0.0",
"prismjs": "^1.29.0",
Expand Down
Loading