Skip to content

Commit

Permalink
Merge pull request #341 from marp-team/improved-plugin-interface
Browse files Browse the repository at this point in the history
Improve module interface of `@marp-team/marpit/plugin`
  • Loading branch information
yhatt authored Sep 10, 2022
2 parents 82eaae5 + f224f58 commit 44663c9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
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]

### Fixed

- Improve module interface of `@marp-team/marpit/plugin` to make compatible with CJS ([#341](https://github.com/marp-team/marpit/pull/341))

## v2.4.0 - 2022-08-11

### Added
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,6 @@ declare module '@marp-team/marpit' {
}

declare module '@marp-team/marpit/plugin' {
const pluginFactory: Marpit.PluginFactory
export default pluginFactory
export const marpitPlugin: Marpit.PluginFactory
export default marpitPlugin
}
6 changes: 5 additions & 1 deletion src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ function marpitPlugin(plugin) {
}
}

export default marpitPlugin
Object.defineProperty(marpitPlugin, '__esModule', { value: true })
Object.defineProperty(marpitPlugin, 'default', { value: marpitPlugin })
Object.defineProperty(marpitPlugin, 'marpitPlugin', { value: marpitPlugin })

module.exports = marpitPlugin
36 changes: 36 additions & 0 deletions test/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import markdownIt from 'markdown-it'
import pluginAsDefaultExport from '../src/plugin'
import { marpitPlugin } from '../src/plugin'
import { Marpit } from '../src/index'

describe('Plugin interface', () => {
it('is compatible as CommonJS module', () => {
expect(require('../src/plugin')).toBeInstanceOf(Function)
})

it('is compatible as ES Modules and able to use through default export', () => {
expect(pluginAsDefaultExport).toBeInstanceOf(Function)
})

it('is compatible as ES Modules and able to use through named export', () => {
expect(marpitPlugin).toBeInstanceOf(Function)
})

it('generates plugin function that is able to use in Marpit instance', () => {
const pluginFn = jest.fn((md) => md)
const plugin = marpitPlugin(pluginFn)
expect(plugin).toBeInstanceOf(Function)

const marpit = new Marpit().use(plugin)
expect(pluginFn).toHaveLastReturnedWith(marpit.markdown)
expect(marpit.markdown.marpit).toStrictEqual(marpit)
})

it('throws error if a generated plugin was used in markdown-it instance', () => {
const plugin = marpitPlugin(jest.fn())

expect(() => new markdownIt().use(plugin)).toThrowError(
'Marpit plugin has detected incompatible markdown-it instance.'
)
})
})

0 comments on commit 44663c9

Please sign in to comment.