From 2cc37e20d0efbb565fa88756b67e9b4a37cfdc73 Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Sun, 19 Oct 2014 07:31:12 +0400 Subject: [PATCH] Splitted .render() methods to simplify monkeypatching --- lib/index.js | 19 +++++++++++++------ lib/renderer.js | 13 ++++++++++++- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/index.js b/lib/index.js index 803c3b3..10733d4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -68,11 +68,11 @@ Remarkable.prototype.use = function (plugin, opts) { }; -// Main method that does all magic :) +// Parse input string, returns tokens array. Modify `env` with +// definitions data. // -Remarkable.prototype.render = function (src) { - var tokens, tok, i, l, env = { references: Object.create(null) }; - +Remarkable.prototype.parse = function (src, env) { + var tokens, tok, i, l; // Parse blocks tokens = this.block.parse(src, this.options, env); @@ -84,8 +84,15 @@ Remarkable.prototype.render = function (src) { } } - // Render - return this.renderer.render(tokens, this.options, env); + return tokens; +}; + +// Main method that does all magic :) +// +Remarkable.prototype.render = function (src) { + var env = { references: {} }; + + return this.renderer.render(this.parse(src, env), this.options, env); }; diff --git a/lib/renderer.js b/lib/renderer.js index 35a3627..483d72d 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -228,6 +228,17 @@ function Renderer() { } +Renderer.prototype.renderInline = function (tokens, options) { + var result = ''; + + for (var i = 0, len = tokens.length; i < len; i++) { + result += rules[tokens[i].type](tokens, i, options); + } + + return result; +}; + + Renderer.prototype.render = function (tokens, options) { var i, len, name, result = '', @@ -272,7 +283,7 @@ Renderer.prototype.render = function (tokens, options) { } if (tokens[i].type === 'inline') { - result += this.render(tokens[i].children, options); + result += this.renderInline(tokens[i].children, options); } else { result += rules[name](tokens, i, options); }