Extract fenced code blocks from Markdown streams

npm install markdown-code-blocks
1 downloads in the last week
8 downloads in the last month


A through stream that extracts code from Markdown content, optionally selecting only code blocks of a selected type.

Currently this only works with GFM-style fenced code blocks, I don't have a need for indentation based blocks (yet). This README file makes a nice example:

$ markdown-code-blocks -t javascript < ./

Will output:

var fs = require('fs')
var assert = require('assert')
var concat = require('concat-stream')
var markdownCodeBlocks = require('./')

var readme = fs.createReadStream(__dirname + '/', 'utf8')

readme.pipe(markdownCodeBlocks('javascript')).pipe(concat(function (code) {
  // Writing code that asserts it's own contents is kind of tricky...
  assert.deepEqual(code.toString().split('\n').slice(0, 10), [
    "var fs = require('fs')",
    "var assert = require('assert')",
    "var concat = require('concat-stream')",
    "var markdownCodeBlocks = require('./')",
    "var readme = fs.createReadStream(__dirname + '/', 'utf8')",
    "readme.pipe(markdownCodeBlocks('javascript')).pipe(concat(function (code) {",
    "  // Writing code that asserts it's own contents is kind of tricky...",
    "  assert.deepEqual(code.toString().split('\\n').slice(0, 10), ["

Which you can pipe into node to verify that this module is working:

$ markdown-code-blocks -t javascript < | node


module.exports = (types: String|Array<String>) => Transform<String, String>



npm loves you