Code Monkey home page Code Monkey logo

docute-iframe's Introduction

docute-iframe

NPM version NPM downloads Build Status donate

A docute plugin to run code in docs with an iframe.

Usage

This plugin requires docute^2.9.0

Include the scripts before running docute.init:

<script src="https://unpkg.com/docute-iframe"></script>

Then activate the plugin:

docute.init({
  plugins: [
    docuteIframe()
  ]
})

Finally, you can write some fancy code examples which are runnable:

This is a **markdown** file, here it to demonstrate some code:

````html
<div id="example"></div>
<script>
  document.getElementById('example').innerHTML = '<strong>it works</strong>'
</script>
````

Note: It's similar to gfm code blocks, which use triple backticks, but here you need to use 4 backticks to mark it as runnable code while still have syntax highlight in your editor.

If the language of code block is js or javascript, it will be automatically wrapped in <script> tag and inserted into iframe body, you can customize it though.

API

docuteIframe([options])

options

match

Type: RegExp
Default: /^`{4}(.*?)[\n\r]+([\s\S]*?)[\n\r]+`{4}/gm

The regular expression we use to find runnable code.

showSourceCode

Type: boolean
Default: true

Show highlighted source code before iframe.

prepend

Type: string

Prepend string to iframe body, eg:

docuteIframe({
  prepend: '<script src="https://unpkg.com/[email protected]/dist/vue.min.js"></script>'
})

Then you can use Vue in your code.

append

Same as prepend but append to iframe body.

sandbox

Type: string
Default: allow-modals allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts

The sandbox attribute of iframe element.

surfaceAPI

Type: Array
Default: ['Prism', 'fetch']

Deliver some global variables of parent window to iframe, don't deliver variables that rely on window.document, since the parent window and iframe have different document.

parseContent

Type: function
Default:

function defaultParseContent(lang, content) {
  if (lang === 'js' || lang === 'javascript') {
    return `<script>${content}</script>`
  }
  return content
}

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

docute-iframe © egoist, Released under the MIT License.
Authored and maintained by egoist with help from contributors (list).

egoistian.com · GitHub @egoist · Twitter @rem_rin_rin

docute-iframe's People

Contributors

andrewmcc avatar egoist avatar sinchang avatar znck avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

docute-iframe's Issues

Iframe doesnt work on underlying folders

I have the following setup:
"docute": "^3.4.8",
"docute-iframe": "^0.3.1"

And a folder structure:

  • Application root
    -- components
    --- component_name
    ----- readme.md

In the readme file i try to run some Vuejs code. but the iframe won't load. I just get the normal '<pre>' notation

It does work on de the readme.md on the root of the application.

Add iframe and display code block too

In most cases displayed code & demo code is same. This would prevent code duplication in demos.

````vue+demo

<div id="app-1"> {{ foo }} </div>

<script> new App ({ el: '#app-1', data: () => ({ foo: 'Hello' }) })</script>

`` ``

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.