Codedown

October 31, 2016

This is a basic demo of codedown. To try it out, type some Markdown in the textarea below, choose a language to extract, and hit Extract to run it through codedown.

Source:
Language:
Output:

This demo is built from the following service:

var codedown = require('codedown');
var http     = require('http');
var url      = require('url');

http.createServer(function(req, res) {
  try {
    var lang = url.parse(req.url, true).query.lang;
    if (!lang) {
      res.writeHead(400, {'Content-Type': 'text/plain'});
      res.end('Missing query parameter: lang');
    } else {
      var data = [];
      req.on('data', function(chunk) {
        data.push(chunk.toString());
      });
      req.on('end', function() {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        var input = data.join('\n');
        var output = codedown(input, lang);
        res.end(output);
      });
    }
  } catch (e) {
    console.log('Caught error', e);
    res.writeHead(500, {'Content-Type': 'text/plain'});
    res.end('Something went wrong.');
  }
}).listen(process.env.CODEDOWN_WEB_PORT);

To use it, submit a POST request with a ?lang=<lang> query parameter and arbitrary Markdown in the body to earldouglas.com/posts/codedown/demo, and get back the just the code:

$ curl -sL github.com/earldouglas/codedown/raw/master/README.md |
  curl -sL --post301 --data-binary @- earldouglas.com/posts/codedown/demo?lang=javascript
var x = 42;

console.log(x);

Optionally, pass the resulting code to an interpreter:

$ curl -sL github.com/earldouglas/codedown/raw/master/README.md |
  curl -sL --post301 --data-binary @- earldouglas.com/posts/codedown/demo?lang=javascript |
  node
42