Skip to content

Instantly share code, notes, and snippets.

@mjyut
Last active August 29, 2015 14:14
Show Gist options
  • Save mjyut/cda4c04fde7eed55120f to your computer and use it in GitHub Desktop.
Save mjyut/cda4c04fde7eed55120f to your computer and use it in GitHub Desktop.
eval JavaScript code in Markdown text
"use strict";
// eval_markdown.js for node.js
// $ node eval_markdown.js Markdown.md
//
// eval JavaScript code in Markdown text
// from
// ```javascript
// to
// ```
console.log('eval markdown:', process.argv[2]);
eval((function(file_name){
var fs = require('fs');
var lines = fs.readFileSync(file_name, {encoding: 'utf-8'}).split("\n")
var line;
var js = false;
var js_code = '';
for(var num = 0; num < lines.length; num++){
line = lines[num];
if (line.match(/^```javascript/)) {
js = true;
continue;
}
else if (line.match(/^```/)) {
js = false;
continue;
}
if (js) {
js_code = js_code + line +
' //' + (num + 1) + ': ' + file_name + '\n';
}
}
return js_code;
})(process.argv[2]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment