Skip to content

Commit 914d04e

Browse files
committed
Refactor examples in README.md to prefer using ESM
1 parent 07e028e commit 914d04e

File tree

1 file changed

+54
-27
lines changed

1 file changed

+54
-27
lines changed

README.md

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -515,25 +515,36 @@ When you specified the path to JavaScript file (`.js`, `.cjs`, or `.mjs`) in `--
515515

516516
The functional engine should export a function as the default export, which should have a single argument representing [the constructor option of Marpit](https://marpit-api.marp.app/marpit)/[Marp Core](https://github.com/marp-team/marp-core#constructor-options).
517517

518-
The function must return a class inherited from Marpit, or an instance of Marpit-based engine made by the parameter passed by argument.
518+
The function must return a class that inherits from Marpit, or an instance of a Marpit-based engine created with the constructor options passed as an argument.
519519

520520
```javascript
521521
// engine.mjs (ES modules)
522522
import { MarpitBasedEngine } from 'marpit-based-engine'
523523

524524
export default () => MarpitBasedEngine // Return a class inherited from Marpit
525+
526+
// or return an instance of Marpit-based engine
527+
// export default (constructorOptions) => new MarpitBasedEngine(constructorOptions)
525528
```
526529

530+
<details>
531+
<summary>Example in CommonJS (<code>.cjs</code>)</summary>
532+
527533
```javascript
528534
// engine.cjs (CommonJS)
529535
const { MarpitBasedEngine } = require('marpit-based-engine')
530536

531-
module.exports = function (constructorOptions) {
532-
// Return an instance of Marpit initialized by passed constructor options
533-
return new MarpitBasedEngine(constructorOptions)
534-
}
537+
module.exports = () => MarpitBasedEngine
538+
539+
// module.exports = function (constructorOptions) {
540+
// return new MarpitBasedEngine(constructorOptions)
541+
// }
535542
```
536543

544+
</details>
545+
546+
#### Async function
547+
537548
This function can return [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) object so you can use [async function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) too.
538549

539550
```javascript
@@ -552,18 +563,17 @@ export default async (constructorOptions) => {
552563
Marp CLI also exposes `marp` getter property to the parameter. It returns a prepared instance of the built-in Marp Core engine, so you can apply several customizations to Marp engine with simple declarations.
553564

554565
```javascript
555-
const marpPlugin = require('marp-plugin-foo')
556-
const andMorePlugin = require('marp-plugin-bar')
566+
import marpPlugin from 'marp-plugin-foo'
567+
import andMorePlugin from 'marp-plugin-bar'
557568

558-
module.exports = ({ marp }) => marp.use(marpPlugin).use(andMorePlugin)
569+
export default ({ marp }) => marp.use(marpPlugin).use(andMorePlugin)
559570
```
560571

561572
It allows converting Markdown with additional syntaxes that were provided by Marp (or compatible markdown-it) plugins.
562573

563-
#### Example: [markdown-it-mark](https://github.com/markdown-it/markdown-it-mark)
574+
#### Example: Use [markdown-it-mark](https://github.com/markdown-it/markdown-it-mark) plugin
564575

565576
```javascript
566-
// engine.mjs
567577
import markdownItMark from 'markdown-it-mark'
568578

569579
export default ({ marp }) => marp.use(markdownItMark)
@@ -784,17 +794,19 @@ export default defineConfig<typeof Marpit>({
784794
You can use Marp CLI through Node.js [if installed Marp CLI into your local project](#local-installation).
785795

786796
```js
787-
const { marpCli } = require('@marp-team/marp-cli')
788-
789-
marpCli(['test.md', '--pdf'])
790-
.then((exitStatus) => {
791-
if (exitStatus > 0) {
792-
console.error(`Failure (Exit status: ${exitStatus})`)
793-
} else {
794-
console.log('Success')
795-
}
796-
})
797-
.catch(console.error)
797+
import { marpCli } from '@marp-team/marp-cli'
798+
799+
try {
800+
const exitStatus = await marpCli(['test.md', '--pdf'])
801+
802+
if (exitStatus > 0) {
803+
console.error(`Failure (Exit status: ${exitStatus})`)
804+
} else {
805+
console.log('Success')
806+
}
807+
} catch (err) {
808+
console.error(err)
809+
}
798810
```
799811

800812
`marpCli()` accepts an argument of CLI options as array, and returns `Promise` to resolve an expected exit status in CLI. It would be rejected with the instance of `Error` if CLI met an error to suspend the conversion process.
@@ -805,25 +817,40 @@ We have exported [`CLIError` class and `CLIErrorCode` enum](https://github.com/m
805817

806818
If `CLIError` instance was thrown, you can identify the reason why CLI threw error by checking `errorCode` member.
807819

820+
```js
821+
try {
822+
await marpCli(['foobar.md', '--pdf'])
823+
} catch (err) {
824+
if (err instanceof CLIError) {
825+
console.error(`Marp CLI Error (Error code: ${err.errorCode}):`, err)
826+
} else {
827+
console.error('Unexpected error:', err)
828+
}
829+
}
830+
```
831+
808832
### Wait for observation
809833

810834
`marpCli()` would not be resolved initiatively if started some observation: Watch mode, server mode, and preview window.
811835

812836
`waitForObservation()` is helpful to handle them. It returns `Promise` that would be resolved with helper object when ready to observe resources in `marpCli()`.
813837

814838
```javascript
815-
const { marpCli, waitForObservation } = require('@marp-team/marp-cli')
839+
import { marpCli, waitForObservation } from '@marp-team/marp-cli'
816840

841+
// This call is not awaited so the server startup will continue in the background
817842
marpCli(['--server', './slides/'])
818843
.then((exitCode) => console.log(`Done with exit code ${exitCode}`))
819844
.catch(console.error)
820845

821-
waitForObservation().then(({ stop }) => {
822-
console.log('Observed')
846+
// Wait for the server to be ready
847+
const { stop } = await waitForObservation()
848+
console.log('Observed')
823849

824-
// Stop observations to resolve marpCli()'s Promise
825-
stop()
826-
})
850+
// ... Do something while observing resources ...
851+
852+
// Stop observing, and resolve the Promise from `marpCli()`
853+
stop()
827854
```
828855

829856
The resolved helper has `stop()` method for telling Marp CLI to stop observation and resolve `Promise`.

0 commit comments

Comments
 (0)