@marp-team/marp-react
v0.0.4
Published
Marp renderer component for React
Downloads
1,215
Readme
@marp-team/marp-react
Marp renderer component for React.
Before using Marp React
This component is suited to create presentation tools integrated with Marp by React. Marp would create the static slide contents consist of plain HTML and CSS, so you have to notice that it's not suited to control the content of your slide via React.
React community has more appropriate and awesome tools for such that purpose. Typically these tools should help if you want to create a beautiful slide deck via React:
- Spectacle can create and control your slide deck with React's some flexibilities.
- mdx-deck is the best alternative for creating slide deck based on MDX: Markdown + React components.
If you really think to need, you can even use Marp React within these frameworks.
Install
# yarn
yarn add @marp-team/marp-core @marp-team/marp-react
# npm
npm install --save @marp-team/marp-core @marp-team/marp-react
Usage
<Marp>
component
This is a simple usage of <Marp>
renderer component. It renders slides via inline SVG to <div>
elements.
import { Marp } from '@marp-team/marp-react'
import React from 'react'
import ReactDOM from 'react-dom'
const markdown = `
# Page 1
---
## Page 2`
ReactDOM.render(<Marp markdown={markdown} />, document.getElementById('app'))
/* <div id="app">
* <div class="marp-xxxxxxxx">
* <svg data-marpit-svg viewBox="0 0 1280 960">
* <foreignObject width="1280" height="960">
* <section><h1>Page 1</h1></section>
* </foreignObject>
* </svg>
* </div>
* <div class="marp-xxxxxxxx">
* <svg data-marpit-svg viewBox="0 0 1280 960">
* <foreignObject width="1280" height="960">
* <section><h2>Page 2</h2></section>
* </foreignObject>
* </svg>
* </div>
* </div>
*/
Constructor option
Marp constructor options can change in options
prop.
<Marp
markdown=":+1:"
options={{
inlineSVG: false,
emoji: {
shortcode: true,
unicode: true,
},
}}
/>
Custom renderer
You can use a custom renderer by passing render
prop or children
prop.
// Use `render` prop
<Marp markdown="# Hello, Marp!" render={customRenderer} />
// or children
<Marp markdown="# Hello, Marp!">{customRenderer}</Marp>
The example of custom renderer is here:
const customRenderer = slides => (
<div className="marp">
{slides.map(({ slide, comments }, i) => (
<div className="slide" key={i}>
{slide}
{comments.map((comment, ci) => (
<p className="comment" key={ci}>
{comment}
</p>
))}
</div>
))}
</div>
)
:information_source: See also Render Props in the document of React.
markdown-it plugins
You can use markdown-it plugins by configuring Marp
object via init
prop.
<Marp
markdown={text(
'Markdown',
`
::: columns
The delimiter \`:::\` should not be shown here.
:::
`
)}
init={marp => marp.use(markdownItContainer, 'columns')}
/>
<MarpWorker>
component (Experimental)
For the best performance of the integrated web app, <MarpWorker>
allows using Web Worker for Markdown conversion. It has a lot of clear advantages over a regular <Marp>
component.
- It does not block UI thread while converting large Markdown.
- A blazing fast live preview by a simple but clever queueing system is available.
- No longer need to include a huge Marp Core into main JS.
- Web Worker will be loaded asynchronously, so the first paint will not block.
The renderer using worker may be default component of Marp React in future.
Basic usage
You can use it just by swapping from <Marp>
to <MarpWorker>
. By default, <MarpWorker>
will use a pre-built worker via jsDelivr CDN.
import { MarpWorker } from '@marp-team/marp-react'
import React from 'react'
import ReactDOM from 'react-dom'
ReactDOM.render(
<MarpWorker markdown="# Hello, Marp Worker!" />,
document.getElementById('app')
)
Use custom worker
The custom worker may specify via worker
prop.
<MarpWorker worker={new Worker('worker.js')} markdown="# Hello, Marp Worker!" />
// worker.js
require('@marp-team/marp-react/lib/worker')()
Initial rendering
<MarpWorker>
's custom renderer might be called with undefined
slides argument, unlike <Marp>
. It means an initial rendering of the component while preparing worker.
You may show waiting user a loading message as follows:
<MarpWorker worker={new Worker('worker.js')} markdown="# Hello, Marp Worker!">
{slides =>
slides ? (
<div className="marp">
{slides.map(({ slide }) => (
<div className="slide" key={i}>
{slide}
</div>
))}
</div>
) : (
<p>Loading Marp Worker...</p>
)
}
</MarpWorker>
Author
Managed by @marp-team.
- Yuki Hattori (@yhatt)