Browse Source

Added extension to renderer

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
dc960ed60a
  1. 4
      CHANGELOG.md
  2. 28
      lib/renderer.js
  3. 21
      test/misc.js

4
CHANGELOG.md

@ -3,7 +3,9 @@
- Added footnotes support. - Added footnotes support.
- Added definitions lists support. - Added definitions lists support.
- Exposed `./common/utils` to simplify custom renderer write. - Added `fence_custom` renderer extension to easy override
named fenced blocks (useful for diagrams and so on).
- Exposed `./common/utils` to simplify custom renderers.
1.4.1 / 2014-11-13 1.4.1 / 2014-11-13

28
lib/renderer.js

@ -55,16 +55,29 @@ rules.code = function (tokens, idx /*, options, env */) {
}; };
rules.fence = function (tokens, idx, options /*, env */) { rules.fence = function (tokens, idx, options, env, self) {
var token = tokens[idx]; var token = tokens[idx];
var langClass = ''; var langClass = '';
var langPrefix = options.langPrefix; var langPrefix = options.langPrefix;
var params, langName = ''; var langName = '', fenceName;
var highlighted; var highlighted;
if (token.params) { if (token.params) {
params = token.params.split(/ +/g);
langName = escapeHtml(replaceEntities(unescapeMd(params[0]))); //
// ```foo bar
//
// Try custom renderer "foo" first. That will simplify overwrite
// for diagrams, latex, and any other fenced block with custom look
//
fenceName = token.params.split(/\s+/g)[0];
if (self.rules.fence_custom.hasOwnProperty(fenceName)) {
return self.rules.fence_custom[fenceName](tokens, idx, options, env, self);
}
langName = escapeHtml(replaceEntities(unescapeMd(fenceName)));
langClass = ' class="' + langPrefix + langName + '"'; langClass = ' class="' + langPrefix + langName + '"';
} }
@ -80,6 +93,7 @@ rules.fence = function (tokens, idx, options /*, env */) {
+ '</code></pre>' + getBreak(tokens, idx); + '</code></pre>' + getBreak(tokens, idx);
}; };
rules.fence_custom = {};
rules.heading_open = function (tokens, idx /*, options, env */) { rules.heading_open = function (tokens, idx /*, options, env */) {
return '<h' + tokens[idx].hLevel + '>'; return '<h' + tokens[idx].hLevel + '>';
@ -310,6 +324,8 @@ rules.dd_close = function() {
function Renderer() { function Renderer() {
// Clone rules object to allow local modifications // Clone rules object to allow local modifications
this.rules = assign({}, rules); this.rules = assign({}, rules);
// exported helper, for custom rules only
this.getBreak = getBreak;
} }
@ -318,7 +334,7 @@ Renderer.prototype.renderInline = function (tokens, options, env) {
_rules = this.rules; _rules = this.rules;
for (var i = 0, len = tokens.length; i < len; i++) { for (var i = 0, len = tokens.length; i < len; i++) {
result += _rules[tokens[i].type](tokens, i, options, env); result += _rules[tokens[i].type](tokens, i, options, env, this);
} }
return result; return result;
@ -334,7 +350,7 @@ Renderer.prototype.render = function (tokens, options, env) {
if (tokens[i].type === 'inline') { if (tokens[i].type === 'inline') {
result += this.renderInline(tokens[i].children, options, env); result += this.renderInline(tokens[i].children, options, env);
} else { } else {
result += _rules[tokens[i].type](tokens, i, options, env); result += _rules[tokens[i].type](tokens, i, options, env, this);
} }
} }

21
test/misc.js

@ -4,6 +4,7 @@
var assert = require('assert'); var assert = require('assert');
var Remarkable = require('../'); var Remarkable = require('../');
var utils = require('../').utils;
describe('Utils', function () { describe('Utils', function () {
@ -181,3 +182,23 @@ describe('Links validation', function () {
}); });
}); });
describe('Custom fences', function () {
it('should render differently overriden rule', function () {
var md = new Remarkable();
md.renderer.rules.fence_custom.foo = function (tokens, idx, options, env, self) {
return '<div class="foo">' +
utils.escapeHtml(tokens[idx].content) +
'</div>' + self.getBreak(tokens, idx);
};
var text = '```foo bar\n' +
'123&45\n' +
'```';
assert.strictEqual(md.render(text), '<div class="foo">123&amp;45\n</div>\n');
});
});

Loading…
Cancel
Save