mos-plugin-example
A mos plugin that combines example code files with their output
Installation
npm install --save mos-plugin-example
Usage
There is an example/index.js file in this module. Its content is:
'use strict'console { return a + b} console //! Comments that start with an exclamation will be inserted into the markdown outside the code block. { for let i = 0; i < textlength; i++ console }
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.log
s 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.log
s inside comments:
'use strict'console//> Hello world! { return a + b} console//> 3
Comments that start with an exclamation will be inserted into the markdown outside the code block.
{ for var i = 0; i < textlength; i++ console //> H //> e //> l //> l //> o //> //> w //> o //> r //> l //> d //> ! }
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 const numbers = 1 2console//> 3
API
example(relativePathToFile)
example.es6(relativePathToFile)
- process an example written in ES6