Last active
August 29, 2015 14:14
-
-
Save mjyut/cda4c04fde7eed55120f to your computer and use it in GitHub Desktop.
eval JavaScript code in Markdown text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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