'use strict'; var assign = require('./common/utils').assign; var has = require('./common/utils').has; var unescapeMd = require('./common/utils').unescapeMd; var replaceEntities = require('./common/utils').replaceEntities; var escapeHtml = require('./common/utils').escapeHtml; //////////////////////////////////////////////////////////////////////////////// // Helpers function nextToken(tokens, idx) { if (++idx >= tokens.length - 2) { return idx; } if ((tokens[idx].type === 'paragraph_open' && tokens[idx].tight) && (tokens[idx + 1].type === 'inline' && tokens[idx + 1].content.length === 0) && (tokens[idx + 2].type === 'paragraph_close' && tokens[idx + 2].tight)) { return nextToken(tokens, idx + 2); } return idx; } // check if we need to hide '\n' before next token function getBreak(tokens, idx) { idx = nextToken(tokens, idx); if (idx < tokens.length && tokens[idx].type === 'list_item_close') { return ''; } return '\n'; } //////////////////////////////////////////////////////////////////////////////// var rules = {}; rules.blockquote_open = function (/* tokens, idx, options, env */) { return '
\n'; }; rules.blockquote_close = function (tokens, idx /*, options, env */) { return '
' + getBreak(tokens, idx); }; rules.code = function (tokens, idx /*, options, env */) { if (tokens[idx].block) { return '
' + escapeHtml(tokens[idx].content) + '
' + getBreak(tokens, idx); } return '' + escapeHtml(tokens[idx].content) + ''; }; rules.fence = function (tokens, idx, options, env, self) { var token = tokens[idx]; var langClass = ''; var langPrefix = options.langPrefix; var langName = '', fenceName; var highlighted; if (token.params) { // // ```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 (has(self.rules.fence_custom, fenceName)) { return self.rules.fence_custom[fenceName](tokens, idx, options, env, self); } langName = escapeHtml(replaceEntities(unescapeMd(fenceName))); langClass = ' class="' + langPrefix + langName + '"'; } if (options.highlight) { highlighted = options.highlight(token.content, langName) || escapeHtml(token.content); } else { highlighted = escapeHtml(token.content); } return '
'
        + highlighted
        + '
' + getBreak(tokens, idx); }; rules.fence_custom = {}; rules.heading_open = function (tokens, idx /*, options, env */) { return ''; }; rules.heading_close = function (tokens, idx /*, options, env */) { return '\n'; }; rules.hr = function (tokens, idx, options /*, env */) { return (options.xhtmlOut ? '
' : '
') + getBreak(tokens, idx); }; rules.bullet_list_open = function (/* tokens, idx, options, env */) { return '' + getBreak(tokens, idx); }; rules.list_item_open = function (/* tokens, idx, options, env */) { return '
  • '; }; rules.list_item_close = function (/* tokens, idx, options, env */) { return '
  • \n'; }; 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 /*, options, env */) { return '' + getBreak(tokens, idx); }; rules.paragraph_open = function (tokens, idx /*, options, env */) { return tokens[idx].tight ? '' : '

    '; }; 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 /*, options, env */) { var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; return ''; }; rules.link_close = function (/* tokens, idx, options, env */) { return ''; }; rules.image = function (tokens, idx, options /*, env */) { var src = ' src="' + escapeHtml(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)) : '') + '"'; var suffix = options.xhtmlOut ? ' /' : ''; return ''; }; rules.table_open = function (/* tokens, idx, options, env */) { return '\n'; }; rules.table_close = function (/* tokens, idx, options, env */) { return '
    \n'; }; rules.thead_open = function (/* tokens, idx, options, env */) { return '\n'; }; rules.thead_close = function (/* tokens, idx, options, env */) { return '\n'; }; rules.tbody_open = function (/* tokens, idx, options, env */) { return '\n'; }; rules.tbody_close = function (/* tokens, idx, options, env */) { return '\n'; }; rules.tr_open = function (/* tokens, idx, options, env */) { return ''; }; rules.tr_close = function (/* tokens, idx, options, env */) { return '\n'; }; rules.th_open = function (tokens, idx /*, options, env */) { var token = tokens[idx]; return ''; }; rules.th_close = function (/* tokens, idx, options, env */) { return ''; }; rules.td_open = function (tokens, idx /*, options, env */) { var token = tokens[idx]; return ''; }; rules.td_close = function (/* tokens, idx, options, env */) { return ''; }; rules.strong_open = function (/* tokens, idx, options, env */) { return ''; }; rules.strong_close = function (/* tokens, idx, options, env */) { return ''; }; rules.em_open = function (/* tokens, idx, options, env */) { return ''; }; rules.em_close = function (/* tokens, idx, options, env */) { return ''; }; rules.del_open = function (/* tokens, idx, options, env */) { return ''; }; rules.del_close = function (/* tokens, idx, options, env */) { return ''; }; rules.ins_open = function (/* tokens, idx, options, env */) { return ''; }; rules.ins_close = function (/* tokens, idx, options, env */) { return ''; }; rules.mark_open = function (/* tokens, idx, options, env */) { return ''; }; rules.mark_close = function (/* tokens, idx, options, env */) { return ''; }; rules.sub = function (tokens, idx /*, options, env */) { return '' + escapeHtml(tokens[idx].content) + ''; }; rules.sup = function (tokens, idx /*, options, env */) { return '' + escapeHtml(tokens[idx].content) + ''; }; rules.hardbreak = function (tokens, idx, options /*, env */) { return options.xhtmlOut ? '
    \n' : '
    \n'; }; rules.softbreak = function (tokens, idx, options /*, env */) { return options.breaks ? (options.xhtmlOut ? '
    \n' : '
    \n') : '\n'; }; rules.text = function (tokens, idx /*, options, env */) { return escapeHtml(tokens[idx].content); }; rules.htmlblock = function (tokens, idx /*, options, env */) { return tokens[idx].content; }; rules.htmltag = function (tokens, idx /*, options, env */) { return tokens[idx].content; }; rules.abbr_open = function (tokens, idx /*, options, env */) { return ''; }; rules.abbr_close = function (/* tokens, idx, options, env */) { return ''; }; rules.footnote_ref = function (tokens, idx) { var n = Number(tokens[idx].id + 1).toString(); var id = 'fnref' + n; if (tokens[idx].subId > 0) { id += ':' + tokens[idx].subId; } return '[' + n + ']'; }; rules.footnote_block_open = function (tokens, idx, options) { return (options.xhtmlOut ? '
    \n' : '
    \n') + '
    \n' + '
      \n'; }; rules.footnote_block_close = function () { return '
    \n
    \n'; }; rules.footnote_open = function (tokens, idx) { var id = Number(tokens[idx].id + 1).toString(); return '
  • '; }; rules.footnote_close = function () { return '
  • \n'; }; rules.footnote_anchor = function (tokens, idx) { var n = Number(tokens[idx].id + 1).toString(); var id = 'fnref' + n; if (tokens[idx].subId > 0) { id += ':' + tokens[idx].subId; } return ' \u21a9'; /* ↩ */ }; rules.dl_open = function() { return '
    \n'; }; rules.dt_open = function() { return '
    '; }; rules.dd_open = function() { return '
    '; }; rules.dl_close = function() { return '
    \n'; }; rules.dt_close = function() { return '\n'; }; rules.dd_close = function() { return '\n'; }; // Renderer class function Renderer() { // Clone rules object to allow local modifications this.rules = assign({}, rules); // exported helper, for custom rules only this.getBreak = getBreak; } Renderer.prototype.renderInline = function (tokens, options, env) { var result = '', _rules = this.rules; for (var i = 0, len = tokens.length; i < len; i++) { result += _rules[tokens[i].type](tokens, i, options, env, this); } return result; }; Renderer.prototype.render = function (tokens, options, env) { var i, len, result = '', _rules = this.rules; for (i = 0, len = tokens.length; i < len; i++) { if (tokens[i].type === 'inline') { result += this.renderInline(tokens[i].children, options, env); } else { result += _rules[tokens[i].type](tokens, i, options, env, this); } } return result; }; module.exports = Renderer;