1.1.2 • Published 8 years ago

mos-plugin-example v1.1.2

Weekly downloads
131
License
MIT
Repository
github
Last release
8 years ago

mos-plugin-example

A mos plugin that combines example code files with their output

NPM version Build status for master Test coverage for master

Installation

This module is installed via npm:

npm install mos-plugin-example --save

Usage

There is an example/index.js file in this module. Its content is:

'use strict'
console.log('Hello world!')

function sum (a, b) {
  return a + b
}

console.log(sum(1, 2))

//! Comments that start with an exclamation will be inserted into the markdown outside the code block.

function printEachLetter (text) {
  for (let i = 0; i < text.length; i++) {
    console.log(text[i])
  }
}

printEachLetter('Hello world!')

We can load this file via the example plugin. The mos plugin will execute the code in the file and combine the output of the console.logs with the code.

In the current README.md we have this code snippet:

<!--@example('./example/index.js')-->
<!--/@-->

It produces this code block, with the outputs written under the console.logs inside comments:

'use strict'
console.log('Hello world!')
//> Hello world!

function sum (a, b) {
  return a + b
}

console.log(sum(1, 2))
//> 3

Comments that start with an exclamation will be inserted into the markdown outside the code block.

function printEachLetter (text) {
  for (let i = 0; i < text.length; i++) {
    console.log(text[i])
    //> H
    //> e
    //> l
    //> l
    //> o
    //>  
    //> w
    //> o
    //> r
    //> l
    //> d
    //> !
  }
}

printEachLetter('Hello world!')

ES6

example/ can be written in ES6, but they have to be loaded with example.es6.

Mos uses rollup and babel to transpile the example written ES6. However, babel is not installed with mos, so you'll have to install babel manually. Put a .babelrc file in your example folder and configure babel the way it is described here.

Here is an example that is using ES6 in the current package:

const sum = (a, b) => a + b

const numbers = [1, 2]
console.log(sum(...numbers))
//> 3

API

  • example(relativePathToFile)
  • example.es6(relativePathToFile) - process an example written in ES6

License

MIT © Zoltan Kochan


Dependencies Dependency status for master

Dev Dependencies devDependency status for master

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago

0.0.0

8 years ago