erasmus
Version:
A simple tool for literate programming
39 lines (38 loc) • 1.28 kB
JavaScript
;
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var stream_1 = require('stream');
/**
* Extracts code blocks from a Markdown document.
*/
function extractCode(markdown) {
var regex = /^`{3}\w*$/gmu;
return markdown
.split(regex)
.filter(function (_, index) { return index % 2 > 0; })
.map(function (item) { return item.trim(); });
}
exports.extractCode = extractCode;
/**
* Transform stream extracting code blocks from a readable stream of Markdown text.
*/
var CodeExtractor = (function (_super) {
__extends(CodeExtractor, _super);
function CodeExtractor(options) {
_super.call(this, options);
this.markdown = "";
}
CodeExtractor.prototype._transform = function (chunk, _, callback) {
this.markdown += chunk;
callback();
};
CodeExtractor.prototype._flush = function (callback) {
this.push(extractCode(this.markdown).join("\n") + "\n");
callback();
};
return CodeExtractor;
}(stream_1.Transform));
exports.CodeExtractor = CodeExtractor;