-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
converter.ts
654 lines (553 loc) · 18.7 KB
/
converter.ts
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { URL } from 'url'
import type { MarpOptions } from '@marp-team/marp-core'
import { Marpit, Options as MarpitOptions } from '@marp-team/marpit'
import chalk from 'chalk'
import type { Browser, Page, HTTPRequest } from 'puppeteer-core'
import { silence, warn } from './cli'
import { Engine, ResolvedEngine } from './engine'
import infoPlugin, { engineInfo, EngineInfo } from './engine/info-plugin'
import metaPlugin from './engine/meta-plugin'
import {
generatePDFOutlines,
pdfOutlineAttr,
pdfOutlineInfo,
pdfOutlinePlugin,
pptrOutlinePositionResolver,
OutlineData,
} from './engine/pdf/outline-plugin'
import { engineTransition, EngineTransition } from './engine/transition-plugin'
import { error } from './error'
import { File, FileType } from './file'
import templates, {
bare,
Template,
TemplateMeta,
TemplateOption,
TemplateResult,
} from './templates/'
import { ThemeSet } from './theme'
import { isOfficialImage } from './utils/docker'
import { pdfLib, setOutline } from './utils/pdf'
import {
generatePuppeteerDataDirPath,
generatePuppeteerLaunchArgs,
launchPuppeteer,
} from './utils/puppeteer'
import { isChromeInWSLHost, resolveWSLPathToHost } from './utils/wsl'
import { notifier } from './watcher'
const CREATED_BY_MARP = 'Created by Marp'
export enum ConvertType {
html = 'html',
pdf = 'pdf',
png = 'png',
pptx = 'pptx',
jpeg = 'jpg',
notes = 'notes',
}
export const mimeTypes = {
[ConvertType.html]: 'text/html',
[ConvertType.pdf]: 'application/pdf',
[ConvertType.png]: 'image/png',
[ConvertType.pptx]:
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
[ConvertType.jpeg]: 'image/jpeg',
[ConvertType.notes]: 'text/plain',
}
export interface ConverterOption {
allowLocalFiles: boolean
baseUrl?: string
engine: Engine
globalDirectives: { theme?: string } & Partial<TemplateMeta>
html?: MarpOptions['html']
imageScale?: number
inputDir?: string
lang: string
options: MarpitOptions
output?: string | false
pages?: boolean | number[]
pdfNotes?: boolean
pdfOutlines?:
| false
| {
pages: boolean
headings: boolean
}
preview?: boolean
puppeteerTimeout?: number
jpegQuality?: number
server?: boolean
template: string
templateOption?: TemplateOption
themeSet: ThemeSet
type: ConvertType
watch: boolean
}
export interface ConvertFileOption {
onConverted?: ConvertedCallback
onlyScanning?: boolean
}
export interface ConvertImageOption {
pages?: boolean | number[]
quality?: number
scale?: number
type: ConvertType.png | ConvertType.jpeg
}
export interface ConvertResult {
file: File
newFile?: File
template: TemplateResult
}
export type ConvertedCallback = (result: ConvertResult) => void
const stripBOM = (s: string) => (s.charCodeAt(0) === 0xfeff ? s.slice(1) : s)
export class Converter {
readonly options: ConverterOption
constructor(opts: ConverterOption) {
this.options = opts
}
get template(): Template {
const template = templates[this.options.template]
if (!template) error(`Template "${this.options.template}" is not found.`)
return template
}
get puppeteerTimeout(): number {
return this.options.puppeteerTimeout ?? 30000
}
async convert(
markdown: string,
file?: File,
{
fallbackToPrintableTemplate = false,
}: { fallbackToPrintableTemplate?: boolean } = {}
): Promise<TemplateResult> {
const { lang, globalDirectives, type } = this.options
const isFile = (f: File | undefined): f is File =>
!!f && f.type === FileType.File
const resolveBase = async (f?: File) => {
if (this.options.baseUrl) return this.options.baseUrl
if (isFile(f) && type !== ConvertType.html) {
return isChromeInWSLHost(
(await generatePuppeteerLaunchArgs()).executablePath
)
? `file:${await resolveWSLPathToHost(f.absolutePath)}`
: f.absoluteFileScheme
}
return undefined
}
let additionals = ''
for (const directive of Object.keys(globalDirectives)) {
if (globalDirectives[directive] !== undefined) {
additionals += `\n<!-- ${directive}: ${JSON.stringify(
globalDirectives[directive]
)} -->`
}
}
let template = this.template
if (fallbackToPrintableTemplate && !template.printable) template = bare
return await template({
...(this.options.templateOption || {}),
lang,
base: await resolveBase(file),
notifyWS:
isFile(file) && this.options.watch && type === ConvertType.html
? await notifier.register(file.absolutePath)
: undefined,
renderer: async (tplOpts) => {
const engine = await this.generateEngine(tplOpts)
tplOpts.modifier?.(engine)
const ret = engine.render(stripBOM(`${markdown}${additionals}`))
const info = engine[engineInfo]
const outline = engine[pdfOutlineInfo]
const transition: EngineTransition | undefined =
engine[engineTransition]
if (isFile(file))
this.options.themeSet.observe(file.absolutePath, info?.theme)
return { ...ret, ...info!, outline, transition }
},
})
}
async convertFile(
file: File,
opts: ConvertFileOption = {}
): Promise<ConvertResult> {
let template: TemplateResult
const useTemplate = async (
fallbackToPrintableTemplate?: boolean
): Promise<TemplateResult> => {
try {
silence(!!opts.onlyScanning)
return await this.convert((await file.load()).toString(), file, {
fallbackToPrintableTemplate,
})
} finally {
silence(false)
}
}
if (!opts.onlyScanning) {
const files: File[] = []
switch (this.options.type) {
case ConvertType.pdf:
template = await useTemplate(true)
files.push(await this.convertFileToPDF(template, file))
break
case ConvertType.png:
case ConvertType.jpeg:
template = await useTemplate(true)
files.push(
...(await this.convertFileToImage(template, file, {
pages: this.options.pages,
quality: this.options.jpegQuality,
scale: this.options.imageScale,
type: this.options.type,
}))
)
break
case ConvertType.pptx:
template = await useTemplate(true)
files.push(
await this.convertFileToPPTX(template, file, {
scale: this.options.imageScale ?? 2,
})
)
break
case ConvertType.notes:
template = await useTemplate(false)
files.push(await this.convertFileToNotes(template, file))
break
default:
template = await useTemplate()
files.push(this.convertFileToHTML(template, file))
}
for (const newFile of files) {
await newFile.save()
if (opts.onConverted) opts.onConverted({ file, newFile, template })
}
// #convertFile must return a single file to serve in server
return { file, template, newFile: files[0] }
} else {
// Try conversion with specific template to scan using resources
template = await useTemplate()
}
return { file, template }
}
async convertFiles(files: File[], opts: ConvertFileOption = {}) {
const { inputDir, output } = this.options
if (!inputDir && output && output !== '-' && files.length > 1)
error('Output path cannot specify with processing multiple files.')
for (const file of files) await this.convertFile(file, opts)
}
private convertFileToHTML(tpl: TemplateResult, file: File): File {
const ret = file.convert(this.options.output, { extension: 'html' })
ret.buffer = Buffer.from(tpl.result)
return ret
}
private convertFileToNotes(tpl: TemplateResult, file: File): File {
const ret = file.convert(this.options.output, { extension: 'txt' })
const comments = tpl.rendered.comments
if (comments.flat().length === 0) {
warn(`${file.relativePath()} contains no notes.`)
ret.buffer = Buffer.from('')
} else {
ret.buffer = Buffer.from(
comments.map((c) => c.join('\n\n')).join('\n\n---\n\n')
)
}
return ret
}
private async convertFileToPDF(
tpl: TemplateResult,
file: File
): Promise<File> {
const html = new File(file.absolutePath)
html.buffer = Buffer.from(tpl.result)
const ret = file.convert(this.options.output, { extension: 'pdf' })
let outlineData: OutlineData | undefined
ret.buffer = await this.usePuppeteer(html, async (page, uri) => {
await page.goto(uri, { waitUntil: ['domcontentloaded', 'networkidle0'] })
if (tpl.rendered.outline) {
outlineData = await page.evaluate(
pptrOutlinePositionResolver,
tpl.rendered.outline.flatMap((o) => o.headings),
pdfOutlineAttr
)
}
return await page.pdf({
printBackground: true,
preferCSSPageSize: true,
timeout: this.puppeteerTimeout,
})
})
// Apply PDF metadata and annotations
const creationDate = new Date()
const { PDFDocument, PDFHexString, PDFString } = await pdfLib()
const pdfDoc = await PDFDocument.load(ret.buffer)
pdfDoc.setCreator(CREATED_BY_MARP)
pdfDoc.setProducer(CREATED_BY_MARP)
pdfDoc.setCreationDate(creationDate)
pdfDoc.setModificationDate(creationDate)
if (tpl.rendered.title) pdfDoc.setTitle(tpl.rendered.title)
if (tpl.rendered.description) pdfDoc.setSubject(tpl.rendered.description)
if (tpl.rendered.author) pdfDoc.setAuthor(tpl.rendered.author)
if (tpl.rendered.keywords)
pdfDoc.setKeywords([tpl.rendered.keywords.join('; ')])
if (this.options.pdfOutlines && tpl.rendered.outline) {
await setOutline(
pdfDoc,
generatePDFOutlines(tpl.rendered.outline, {
...this.options.pdfOutlines,
data: outlineData,
size: tpl.rendered.size,
})
)
}
if (this.options.pdfNotes) {
const pages = pdfDoc.getPages()
for (let i = 0, len = pages.length; i < len; i += 1) {
const notes = tpl.rendered.comments[i].join('\n\n')
if (notes) {
const noteAnnot = pdfDoc.context.obj({
Type: 'Annot',
Subtype: 'Text',
Rect: [0, 20, 20, 20],
Contents: PDFHexString.fromText(notes),
T: tpl.rendered.author
? PDFHexString.fromText(tpl.rendered.author)
: undefined,
Name: 'Note',
Subj: PDFString.of('Note'),
C: [1, 0.92, 0.42], // RGB
CA: 0.25, // Alpha
})
pages[i].node.addAnnot(pdfDoc.context.register(noteAnnot))
}
}
}
// Apply modified PDF to buffer
ret.buffer = Buffer.from(await pdfDoc.save())
return ret
}
private async convertFileToImage(
tpl: TemplateResult,
file: File,
opts: ConvertImageOption
): Promise<File[]> {
const html = new File(file.absolutePath)
html.buffer = Buffer.from(tpl.result)
const files: File[] = []
await this.usePuppeteer(html, async (page, uri) => {
await page.setViewport({
...tpl.rendered.size,
deviceScaleFactor: opts.scale ?? 1,
})
await page.goto(uri, { waitUntil: ['domcontentloaded', 'networkidle0'] })
await page.emulateMediaType('print')
const screenshot = async (pageNumber = 1) => {
// for Chrome < 89 (TODO: Remove this script evaluation in future)
await page.evaluate(
`window.scrollTo(0,${(pageNumber - 1) * tpl.rendered.size.height})`
)
const clip = {
x: 0,
y: (pageNumber - 1) * tpl.rendered.size.height,
...tpl.rendered.size,
} as const
if (opts.type === ConvertType.jpeg)
return (await page.screenshot({
clip,
quality: opts.quality,
type: 'jpeg',
})) as Buffer
return (await page.screenshot({ clip, type: 'png' })) as Buffer
}
if (opts.pages) {
// Multiple images
for (let page = 1; page <= tpl.rendered.length; page += 1) {
const ret = file.convert(this.options.output, {
page,
extension: opts.type,
})
ret.buffer = await screenshot(page)
files.push(ret)
}
} else {
// Title image
const ret = file.convert(this.options.output, { extension: opts.type })
ret.buffer = await screenshot()
files.push(ret)
}
})
return files
}
private async convertFileToPPTX(
tpl: TemplateResult,
file: File,
opts: Partial<ConvertImageOption> = {}
) {
const imageFiles = await this.convertFileToImage(tpl, file, {
...opts,
pages: true,
type: ConvertType.png,
})
const pptx = new (await import('pptxgenjs')).default()
const layoutName = `${tpl.rendered.size.width}x${tpl.rendered.size.height}`
pptx.author = tpl.rendered.author ?? CREATED_BY_MARP
pptx.company = CREATED_BY_MARP
pptx.defineLayout({
name: layoutName,
width: tpl.rendered.size.width / 96,
height: tpl.rendered.size.height / 96,
})
pptx.layout = layoutName
if (tpl.rendered.title) pptx.title = tpl.rendered.title
if (tpl.rendered.description) pptx.subject = tpl.rendered.description
imageFiles.forEach((imageFile, page) => {
const slide = pptx.addSlide()
slide.background = {
data: `data:image/png;base64,${imageFile.buffer!.toString('base64')}`,
}
const notes = tpl.rendered.comments[page].join('\n\n')
if (notes) slide.addNotes(notes)
})
const ret = file.convert(this.options.output, { extension: 'pptx' })
ret.buffer = (await pptx.write({ outputType: 'nodebuffer' })) as Buffer
return ret
}
private async generateEngine(
mergeOptions: MarpitOptions
): Promise<Marpit & { [engineInfo]?: EngineInfo }> {
const { html, options } = this.options
const { prototype } = this.options.engine
const opts = { ...options, ...mergeOptions, html }
let engine: any
if (
prototype &&
Object.prototype.hasOwnProperty.call(prototype, 'constructor')
) {
engine = new this.options.engine(opts)
} else {
// Expose "marp" getter to allow accessing a bundled Marp Core instance
const defaultEngine = await ResolvedEngine.resolveDefaultEngine()
Object.defineProperty(opts, 'marp', {
get: () => new defaultEngine.klass(opts),
})
engine = (this.options.engine as any)(opts)
}
if (typeof engine.render !== 'function')
error('Specified engine has not implemented render() method.')
// Enable HTML tags
if (html !== undefined) engine.markdown.set({ html })
// Marpit plugins
engine.use(metaPlugin).use(infoPlugin)
if (this.options.type === ConvertType.pdf && this.options.pdfOutlines)
engine.use(pdfOutlinePlugin)
// Themes
this.options.themeSet.registerTo(engine)
return engine
}
private async usePuppeteer<T>(
baseFile: File,
processer: (page: Page, uri: string) => Promise<T>
) {
const tmpFile: File.TmpFileInterface | undefined = await (() => {
if (!this.options.allowLocalFiles) return undefined
warn(
`Insecure local file accessing is enabled for conversion from ${baseFile.relativePath()}.`
)
// Snapd Chromium cannot access from sandbox container to user-land `/tmp`
// directory so always create tmp file to home directory if in Linux.
// (There is an exception for an official docker image)
return baseFile.saveTmpFile({
home: process.platform === 'linux' && !isOfficialImage(),
extension: '.html',
})
})()
try {
const browser = await Converter.runBrowser({
timeout: this.puppeteerTimeout,
})
const uri = await (async () => {
if (tmpFile) {
if (isChromeInWSLHost(browser.process()?.spawnfile)) {
// Windows Chrome should read file from WSL environment
return `file:${await resolveWSLPathToHost(tmpFile.path)}`
}
return `file://${tmpFile.path}`
}
return `data:text/html;base64,${baseFile.buffer!.toString('base64')}`
})()
const page = await browser.newPage()
page.setDefaultTimeout(this.puppeteerTimeout)
const { missingFileSet, failedFileSet } =
this.trackFailedLocalFileAccess(page)
try {
return await processer(page, uri)
} finally {
if (missingFileSet.size > 0) {
warn(
`${missingFileSet.size > 1 ? 'Some of t' : 'T'}he local file${
missingFileSet.size > 1 ? 's are' : ' is'
} missing and will be ignored. Make sure the file path${
missingFileSet.size > 1 ? 's are' : ' is'
} correct.`
)
}
if (failedFileSet.size > 0) {
warn(
`Marp CLI has detected accessing to local file${
failedFileSet.size > 1 ? 's' : ''
}. ${
failedFileSet.size > 1 ? 'They are' : 'That is'
} blocked by security reason. Instead we recommend using assets uploaded to online. (Or you can use ${chalk.yellow(
'--allow-local-files'
)} option if you are understood of security risk)`
)
}
await page.close()
}
} finally {
if (tmpFile) await tmpFile.cleanup()
}
}
private trackFailedLocalFileAccess(page: Page): {
missingFileSet: Set<string>
failedFileSet: Set<string>
} {
const missingFileSet = new Set<string>()
const failedFileSet = new Set<string>()
page.on('requestfailed', (req: HTTPRequest) => {
try {
const url = new URL(req.url())
if (url.protocol === 'file:') {
if (req.failure()?.errorText === 'net::ERR_FILE_NOT_FOUND') {
missingFileSet.add(url.href)
} else {
failedFileSet.add(url.href)
}
}
} catch (e: unknown) {
// No ops
}
})
return { missingFileSet, failedFileSet }
}
static async closeBrowser() {
if (Converter.browser) await Converter.browser.close()
}
private static browser?: Browser
private static async runBrowser({ timeout }: { timeout?: number }) {
if (!Converter.browser) {
const baseArgs = await generatePuppeteerLaunchArgs()
Converter.browser = await launchPuppeteer({
...baseArgs,
timeout,
userDataDir: await generatePuppeteerDataDirPath('marp-cli-conversion', {
wslHost: isChromeInWSLHost(baseArgs.executablePath),
}),
})
Converter.browser.once('disconnected', () => {
Converter.browser = undefined
})
}
return Converter.browser
}
}