From f8e270f6e0afc8cb82c210879a1843243942c4f7 Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Sat, 8 Nov 2014 22:16:02 +0300 Subject: [PATCH] Added renderer rules rewrite test --- lib/renderer.js | 98 ++++++++++++++++++++++++------------------------- test/misc.js | 12 ++++++ 2 files changed, 61 insertions(+), 49 deletions(-) diff --git a/lib/renderer.js b/lib/renderer.js index e2a8bb3..f0f8b8e 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -57,15 +57,15 @@ var rules = {}; -rules.blockquote_open = function () { +rules.blockquote_open = function (/* tokens, idx, options, env */) { return '
\n'; }; -rules.blockquote_close = function (tokens, idx) { +rules.blockquote_close = function (tokens, idx /*, options, env */) { return '
' + getBreak(tokens, idx); }; -rules.code = function (tokens, idx) { +rules.code = function (tokens, idx /*, options, env */) { if (tokens[idx].block) { return '
' + escapeHtml(tokens[idx].content) + '
' + getBreak(tokens, idx); } @@ -74,7 +74,7 @@ rules.code = function (tokens, idx) { }; -rules.fence = function (tokens, idx, options) { +rules.fence = function (tokens, idx, options /*, env */) { var token = tokens[idx]; var langClass = ''; var langPrefix = options.langPrefix; @@ -100,61 +100,61 @@ rules.fence = function (tokens, idx, options) { }; -rules.heading_open = function (tokens, idx) { +rules.heading_open = function (tokens, idx /*, options, env */) { return ''; }; -rules.heading_close = function (tokens, idx) { +rules.heading_close = function (tokens, idx /*, options, env */) { return '\n'; }; -rules.hr = function (tokens, idx, options) { +rules.hr = function (tokens, idx, options /*, env */) { return (options.xhtmlOut ? '
' : '
') + getBreak(tokens, idx); }; -rules.bullet_list_open = function () { +rules.bullet_list_open = function (/* tokens, idx, options, env */) { return '' + getBreak(tokens, idx); }; -rules.list_item_open = function () { +rules.list_item_open = function (/* tokens, idx, options, env */) { return '
  • '; }; -rules.list_item_close = function () { +rules.list_item_close = function (/* tokens, idx, options, env */) { return '
  • \n'; }; -rules.ordered_list_open = function (tokens, idx) { +rules.ordered_list_open = function (tokens, idx /*, options, env */) { var token = tokens[idx]; return ' 1 ? ' start="' + token.order + '"' : '') + '>\n'; }; -rules.ordered_list_close = function (tokens, idx) { +rules.ordered_list_close = function (tokens, idx /*, options, env */) { return '' + getBreak(tokens, idx); }; -rules.paragraph_open = function (tokens, idx) { +rules.paragraph_open = function (tokens, idx /*, options, env */) { return tokens[idx].tight ? '' : '

    '; }; -rules.paragraph_close = function (tokens, idx) { +rules.paragraph_close = function (tokens, idx /*, options, env */) { var addBreak = !(tokens[idx].tight && idx && tokens[idx - 1].type === 'inline' && !tokens[idx - 1].content); return (tokens[idx].tight ? '' : '

    ') + (addBreak ? getBreak(tokens, idx) : ''); }; -rules.link_open = function (tokens, idx) { +rules.link_open = function (tokens, idx /*, options, env */) { var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; return ''; }; -rules.link_close = function () { +rules.link_close = function (/* tokens, idx, options, env */) { return ''; }; -rules.image = function (tokens, idx, options) { +rules.image = function (tokens, idx, options /*, env */) { var src = ' src="' + escapeHtml(encodeURI(tokens[idx].src)) + '"'; var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; var alt = ' alt="' + (tokens[idx].alt ? escapeHtml(replaceEntities(tokens[idx].alt)) : '') + '"'; @@ -163,121 +163,121 @@ rules.image = function (tokens, idx, options) { }; -rules.table_open = function () { +rules.table_open = function (/* tokens, idx, options, env */) { return '\n'; }; -rules.table_close = function () { +rules.table_close = function (/* tokens, idx, options, env */) { return '
    \n'; }; -rules.thead_open = function () { +rules.thead_open = function (/* tokens, idx, options, env */) { return '\n'; }; -rules.thead_close = function () { +rules.thead_close = function (/* tokens, idx, options, env */) { return '\n'; }; -rules.tbody_open = function () { +rules.tbody_open = function (/* tokens, idx, options, env */) { return '\n'; }; -rules.tbody_close = function () { +rules.tbody_close = function (/* tokens, idx, options, env */) { return '\n'; }; -rules.tr_open = function () { +rules.tr_open = function (/* tokens, idx, options, env */) { return ''; }; -rules.tr_close = function () { +rules.tr_close = function (/* tokens, idx, options, env */) { return '\n'; }; -rules.th_open = function (tokens, idx) { +rules.th_open = function (tokens, idx /*, options, env */) { var token = tokens[idx]; return ''; }; -rules.th_close = function () { +rules.th_close = function (/* tokens, idx, options, env */) { return ''; }; -rules.td_open = function (tokens, idx) { +rules.td_open = function (tokens, idx /*, options, env */) { var token = tokens[idx]; return ''; }; -rules.td_close = function () { +rules.td_close = function (/* tokens, idx, options, env */) { return ''; }; -rules.strong_open = function() { +rules.strong_open = function (/* tokens, idx, options, env */) { return ''; }; -rules.strong_close = function() { +rules.strong_close = function (/* tokens, idx, options, env */) { return ''; }; -rules.em_open = function() { +rules.em_open = function (/* tokens, idx, options, env */) { return ''; }; -rules.em_close = function() { +rules.em_close = function (/* tokens, idx, options, env */) { return ''; }; -rules.del_open = function() { +rules.del_open = function (/* tokens, idx, options, env */) { return ''; }; -rules.del_close = function() { +rules.del_close = function (/* tokens, idx, options, env */) { return ''; }; -rules.ins_open = function() { +rules.ins_open = function (/* tokens, idx, options, env */) { return ''; }; -rules.ins_close = function() { +rules.ins_close = function (/* tokens, idx, options, env */) { return ''; }; -rules.mark_open = function() { +rules.mark_open = function (/* tokens, idx, options, env */) { return ''; }; -rules.mark_close = function() { +rules.mark_close = function (/* tokens, idx, options, env */) { return ''; }; -rules.sub = function(tokens, idx/*, options*/) { +rules.sub = function (tokens, idx /*, options, env */) { return '' + escapeHtml(tokens[idx].content) + ''; }; -rules.sup = function(tokens, idx/*, options*/) { +rules.sup = function (tokens, idx /*, options, env */) { return '' + escapeHtml(tokens[idx].content) + ''; }; -rules.hardbreak = function (tokens, idx, options) { +rules.hardbreak = function (tokens, idx, options /*, env */) { return options.xhtmlOut ? '
    \n' : '
    \n'; }; -rules.softbreak = function (tokens, idx, options) { +rules.softbreak = function (tokens, idx, options /*, env */) { return options.breaks ? (options.xhtmlOut ? '
    \n' : '
    \n') : '\n'; }; -rules.text = function (tokens, idx) { +rules.text = function (tokens, idx /*, options, env */) { return escapeHtml(tokens[idx].content); }; -rules.htmlblock = function (tokens, idx) { +rules.htmlblock = function (tokens, idx /*, options, env */) { return tokens[idx].content; }; -rules.htmltag = function (tokens, idx) { +rules.htmltag = function (tokens, idx /*, options, env */) { return tokens[idx].content; }; -rules.abbr_open = function (tokens, idx) { +rules.abbr_open = function (tokens, idx /*, options, env */) { return ''; }; -rules.abbr_close = function () { +rules.abbr_close = function (/* tokens, idx, options, env */) { return ''; }; diff --git a/test/misc.js b/test/misc.js index deacd3c..e8f380c 100644 --- a/test/misc.js +++ b/test/misc.js @@ -127,6 +127,7 @@ describe('API', function () { assert.strictEqual(md.render('![]()'), '

    \n'); assert.strictEqual(md.render('a \\\nb'), '

    a
    \nb

    \n'); }); + }); @@ -151,6 +152,17 @@ describe('Misc', function () { assert.strictEqual(md.renderInline('a *b* c'), 'a b c'); }); + it('Renderer should have pluggable inline and block rules', function () { + var md = new Remarkable(); + + md.renderer.rules.em_open = function () { return ''; }; + md.renderer.rules.em_close = function () { return ''; }; + md.renderer.rules.paragraph_open = function () { return ''; }; + md.renderer.rules.paragraph_close = function () { return ''; }; + + assert.strictEqual(md.render('*b*'), 'b'); + }); + });