An open API service indexing awesome lists of open source software.

https://github.com/ktsn/markdown-it-demo-renderer

markdown-it renderer to render live demo over each code block fence
https://github.com/ktsn/markdown-it-demo-renderer

live-demo markdown-it markdown-it-plugin preview renderer

Last synced: 9 months ago
JSON representation

markdown-it renderer to render live demo over each code block fence

Awesome Lists containing this project

README

          

# markdown-it-demo-renderer

[markdown-it](https://github.com/markdown-it/markdown-it) renderer to render live demo over each code block fence

## Outline

This markdown-it plugin allows you to render live demo over each code block fence. For example, if you have the following HTML code block:

````md
```html

Hello!


```
````

The renderer will generate the following HTML (added some comments and indentations for readability):

```html


Hello!




<h1>Hello!</h1>

```

## Usage

Install it via npm:

```sh
$ npm install markdown-it-demo-renderer
```

Then apply it to your markdown-it instance.

```js
const MarkdownIt = require('markdown-it')
const md = new MarkdownIt()
.use(require('markdown-it-demo-renderer'))

const code = `
\`\`\`html

Hello!


\`\`\`
`

console.log(md.render(code))
```

### Customize Wrapper Element

Sometimes you may want to custom the wrapper element of a live demo and a code example. In that case, you specify `wrap` option when apply the renderer. The `wrap` option should be a function that receives a live demo and a code example html strings and return wrapped entire html string.

```js
const MarkdownIt = require('markdown-it')
const md = new MarkdownIt()
.use(require('markdown-it-demo-renderer'), {
wrap: (demo, code) => {
return (
// Wrap demo html string with `.example-demo`
'

' + demo + '
' +
// Wrap code html string with `.example-code`
'
' + code + '
'
)
}
})
```

### Preprocessor

markdown-it-demo-renderer supports `html` code block to render live demo in default. You can add other html preprocessors by passing `preprocessors` option. `preprocessors` expects transform functions that receive the original code string and expect to return transformed html string for each language.

```js
const MarkdownIt = require('markdown-it')
const pug = require('pug')

const md = new MarkdownIt()
.use(require('markdown-it-demo-renderer'), {
preprocessors: {
// Enable to show live demo for `pug` code block
pug: code => {
// Return compiled html code
return pug.render(code)
}
}
})

const code = `
\`\`\`pug
h1 Hello!
\`\`\`
`

console.log(md.render(code))
```

## License

MIT