|
1 | 1 | /* eslint-disable @typescript-eslint/no-non-null-assertion */
|
2 | 2 | import { URL } from 'url'
|
3 |
| -import { MarpOptions } from '@marp-team/marp-core' |
| 3 | +import type { MarpOptions } from '@marp-team/marp-core' |
4 | 4 | import { Marpit, Options as MarpitOptions } from '@marp-team/marpit'
|
5 | 5 | import chalk from 'chalk'
|
6 | 6 | import puppeteer from 'puppeteer-core'
|
7 | 7 | import { silence, warn } from './cli'
|
8 |
| -import { Engine } from './engine' |
| 8 | +import { Engine, ResolvedEngine } from './engine' |
9 | 9 | import infoPlugin, { engineInfo, EngineInfo } from './engine/info-plugin'
|
10 | 10 | import metaPlugin from './engine/meta-plugin'
|
11 | 11 | import { error } from './error'
|
@@ -149,8 +149,8 @@ export class Converter {
|
149 | 149 | isFile(file) && this.options.watch && type === ConvertType.html
|
150 | 150 | ? await notifier.register(file.absolutePath)
|
151 | 151 | : undefined,
|
152 |
| - renderer: (tplOpts) => { |
153 |
| - const engine = this.generateEngine(tplOpts) |
| 152 | + renderer: async (tplOpts) => { |
| 153 | + const engine = await this.generateEngine(tplOpts) |
154 | 154 | tplOpts.modifier?.(engine)
|
155 | 155 |
|
156 | 156 | const ret = engine.render(stripBOM(`${markdown}${additionals}`))
|
@@ -420,28 +420,41 @@ export class Converter {
|
420 | 420 | return ret
|
421 | 421 | }
|
422 | 422 |
|
423 |
| - private generateEngine( |
| 423 | + private async generateEngine( |
424 | 424 | mergeOptions: MarpitOptions
|
425 |
| - ): Marpit & { [engineInfo]: EngineInfo | undefined } { |
| 425 | + ): Promise<Marpit & { [engineInfo]?: EngineInfo }> { |
426 | 426 | const { html, options } = this.options
|
427 | 427 | const { prototype } = this.options.engine
|
428 | 428 | const opts = { ...options, ...mergeOptions, html }
|
429 | 429 |
|
430 |
| - const engine = |
| 430 | + let engine: any |
| 431 | + |
| 432 | + if ( |
431 | 433 | prototype &&
|
432 | 434 | Object.prototype.hasOwnProperty.call(prototype, 'constructor')
|
433 |
| - ? new this.options.engine(opts) |
434 |
| - : (<any>this.options.engine)(opts) |
| 435 | + ) { |
| 436 | + engine = new this.options.engine(opts) |
| 437 | + } else { |
| 438 | + // Expose "marp" getter to allow accessing a bundled Marp Core instance |
| 439 | + const defaultEngine = await ResolvedEngine.resolveDefaultEngine() |
| 440 | + |
| 441 | + Object.defineProperty(opts, 'marp', { |
| 442 | + get: () => new defaultEngine.klass(opts), |
| 443 | + }) |
| 444 | + |
| 445 | + engine = (this.options.engine as any)(opts) |
| 446 | + } |
435 | 447 |
|
436 | 448 | if (typeof engine.render !== 'function')
|
437 | 449 | error('Specified engine has not implemented render() method.')
|
438 | 450 |
|
| 451 | + // Enable HTML tags |
439 | 452 | if (html !== undefined) engine.markdown.set({ html })
|
440 | 453 |
|
441 | 454 | // Marpit plugins
|
442 | 455 | engine.use(metaPlugin).use(infoPlugin)
|
443 | 456 |
|
444 |
| - // Additional themes |
| 457 | + // Themes |
445 | 458 | this.options.themeSet.registerTo(engine)
|
446 | 459 |
|
447 | 460 | return engine
|
|
0 commit comments