markdown2code

extracts code blocks (surrounded by triple backticks) from markdown and streams it out as text or JSON

Installation | API | CLI | License

JavaScript Style Guide

Installation

With npm do

npm install markdown2code

Since there is a markdown2code CLI it does make sense to install globally

npm install markdown2code -g

If you add the following script to your package.json

    "markdown2code": "markdown2code -l javascript README.md > ${npm_package_main}",

then you can do literate programming in your README.md and extract code running

npm run markdown2code

See also Usage and Examples sections.

API

The markdown2code function accepts the following parameters.

For example:

const markdown2code = require('markdown2code')

const fs = require('fs')
const input = fs.createReadStream('README.md')
const output = process.stdout

markdown2code(input, output)

CLI

Usage

markdown2code [--format JSON|text] [--lang <language>] file.md

Options

Examples

Extract all javascript snippets in text format

markdown2code --lang javascript README.md

Extract all code snippets in JSON format

markdown2code --format JSON README.md

Code blocks like the following will be extracted

```javascript
// Hello
console.log("hello world")
```

and streamed to stdout as the following object

[
  {
    "lang": "javascript",
    "code": [
      "// Hello",
      "console.log(\"hello world\")"
    ]
  }
]

License

MIT