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

Support functional engine #42

Merged
merged 3 commits into from
Dec 1, 2018
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Added

- Support functional engine ([#42](https://github.com/marp-team/marp-cli/pull/42))

## v0.0.14 - 2018-11-24

### Security
Expand Down
7 changes: 6 additions & 1 deletion src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,12 @@ export class Converter {
// for marp-core
if (html !== undefined) opts.html = html

const engine = new this.options.engine(opts)
const { prototype } = this.options.engine

const engine =
prototype && prototype.hasOwnProperty('constructor')
? new this.options.engine(opts)
: (<any>this.options.engine)(opts)

if (typeof engine.render !== 'function')
error('Specified engine has not implemented render() method.')
Expand Down
6 changes: 6 additions & 0 deletions test/_configs/custom-engine/anonymous.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { Marpit } = require('@marp-team/marpit')

module.exports = {
engine: jest.fn(opts => new Marpit(opts)),
options: { customOption: true },
}
16 changes: 15 additions & 1 deletion test/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ describe('Marp CLI', () => {
expect(html).toContain('@theme a')
})

it('allows custom engine specified in js config', async () => {
it('allows custom engine class specified in js config', async () => {
const stdout = jest
.spyOn(process.stdout, 'write')
.mockImplementation()
Expand All @@ -470,6 +470,20 @@ describe('Marp CLI', () => {
expect(html).toContain('<b>custom</b>')
expect(html).toContain('/* custom */')
})

it('allows custom engine function specified in js config', async () => {
jest.spyOn(process.stdout, 'write').mockImplementation()
jest.spyOn(console, 'warn').mockImplementation()

const conf = assetFn('_configs/custom-engine/anonymous.js')
const md = assetFn('_configs/custom-engine/md.md')
const { engine } = require(conf)

expect(await marpCli(['-c', conf, md, '-o', '-'])).toBe(0)
expect(engine).toBeCalledWith(
expect.objectContaining({ customOption: true })
)
})
})
})
})
Expand Down