forked from ulivz/vuepress-plugin-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (100 loc) · 2.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const puppeteer = require('puppeteer')
const PDFMerge = require('easy-pdf-merge')
const { join } = require('path')
const { dev } = require('vuepress')
const { fs, logger, chalk } = require('@vuepress/shared-utils')
const { red, yellow, gray } = chalk
// Keep silent before running custom command.
logger.setOptions({ logLevel: 1 })
module.exports = (opts = {}, ctx) => ({
name: 'vuepress-plugin-export',
chainWebpack(config) {
config.plugins.delete('bar')
// TODO vuepress should give plugin the ability to remove this plugin
config.plugins.delete('vuepress-log')
},
extendCli(cli) {
cli
.command('export [targetDir]', 'export current vuepress site to a PDF file')
.allowUnknownOptions()
.action(async (dir = '.') => {
dir = join(process.cwd(), dir)
try {
const nCtx = await dev({
sourceDir: dir,
clearScreen: false,
theme: opts.theme || '@vuepress/default'
})
logger.setOptions({ logLevel: 3 })
logger.info(`Start to generate current site to PDF ...`)
try {
await generatePDF(nCtx, {
port: nCtx.devProcess.port,
host: nCtx.devProcess.host,
sorter: opts.sorter
})
} catch (error) {
console.error(red(error))
}
nCtx.devProcess.server.close()
process.exit(0)
} catch (e) {
throw e
}
})
}
})
async function generatePDF(ctx, {
port,
host,
sorter,
}) {
const { pages, tempPath, siteConfig } = ctx
const tempDir = join(tempPath, 'pdf')
fs.ensureDirSync(tempDir)
let exportPages = pages.slice(0)
if (typeof sorter === 'function') {
exportPages = exportPages.sort(sorter)
}
exportPages = exportPages.map(page => {
return {
url: page.path,
title: page.title,
location: `http://${host}:${port}${page.path}`,
path: `${tempDir}/${page.key}.pdf`
}
})
const browser = await puppeteer.launch()
const browserPage = await browser.newPage()
for (let i = 0; i < exportPages.length; i++) {
const {
location,
path: pagePath,
url,
title
} = exportPages[i]
await browserPage.goto(
location,
{ waitUntil: 'networkidle2' }
)
await browserPage.pdf({
path: pagePath,
format: 'A4'
})
logger.success(`Generated ${yellow(title)} ${gray(`${url}`)}`)
}
const files = exportPages.map(({ path }) => path)
const outputFilename = siteConfig.title || 'site'
const outputFile = `${outputFilename}.pdf`
await new Promise(resolve => {
PDFMerge(files, outputFile, err => {
if (err) {
throw err
}
logger.success(`Export ${yellow(outputFile)} file!`)
resolve()
})
})
await browser.close()
fs.removeSync(tempDir)
}