Skip to content
This repository has been archived by the owner on Nov 7, 2019. It is now read-only.
/ mos-plugin-example Public archive

A mos plugin that combines example code files with their output

License

Notifications You must be signed in to change notification settings

mosjs/mos-plugin-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A mos plugin that combines example code files with their output

Resources

License

Stars

Watchers

Forks

Packages

No packages published