'use strict'; var assign = require('object-assign'); var escapeHtml = require('./helpers').escapeHtml; var escapeHtmlKeepEntities = require('./helpers').escapeHtmlKeepEntities; var unescapeMd = require('./helpers').unescapeMd; // check if we need to hide '\n' before next token function getBreak(tokens, idx) { if (++idx < tokens.length && tokens[idx].type === 'list_item_close') { return ''; } return '\n'; } var rules = {}; rules.blockquote_open = function (/*tokens, idx, options*/) { return '
\n'; }; rules.blockquote_close = function (tokens, idx /*, options*/) { return '
' + getBreak(tokens, idx); }; rules.code = function (tokens, idx /*, options*/) { if (tokens[idx].block) { return '
' + escapeHtml(tokens[idx].content) + '
' + getBreak(tokens, idx); } return '' + escapeHtml(tokens[idx].content) + ''; }; rules.fence = function (tokens, idx, options) { var token = tokens[idx]; var langMark = ''; var langPrefix = options.langprefix || ''; var params; if (token.params) { params = token.params.split(/ +/g); langMark = ' class="' + langPrefix + escapeHtmlKeepEntities(unescapeMd(params[0])) + '"'; } return '
'
        + escapeHtml(token.content)
        + '
' + getBreak(tokens, idx); }; rules.heading_open = function (tokens, idx /*, options*/) { return ''; }; rules.heading_close = function (tokens, idx /*, options*/) { return '\n'; }; rules.hr = function (tokens, idx, options) { return (options.xhtml ? '
' : '
') + getBreak(tokens, idx); }; rules.bullet_list_open = function (/*tokens, idx, options*/) { return '' + getBreak(tokens, idx); }; rules.list_item_open = function (/*tokens, idx, options*/) { return '
  • '; }; rules.list_item_close = function (/*tokens, idx, options*/) { return '
  • \n'; }; rules.ordered_list_open = function (tokens, idx /*, options*/) { var token = tokens[idx]; return ' 1 ? ' start="' + token.order + '"' : '') + '>\n'; }; rules.ordered_list_close = function (tokens, idx /*, options*/) { return '' + getBreak(tokens, idx); }; rules.paragraph_open = function (/*tokens, idx, options*/) { return '

    '; }; rules.paragraph_close = function (tokens, idx /*, options*/) { return '

    ' + getBreak(tokens, idx); }; rules.link_open = function (tokens, idx /*, options*/) { var title = tokens[idx].title ? (' title="' + escapeHtmlKeepEntities(tokens[idx].title) + '"') : ''; return ''; }; rules.link_close = function (/*tokens, idx, options*/) { return ''; }; rules.image = function (tokens, idx, options) { var src = ' src="' + escapeHtmlKeepEntities(tokens[idx].src) + '"'; var title = tokens[idx].title ? (' title="' + escapeHtmlKeepEntities(tokens[idx].title) + '"') : ''; var alt = tokens[idx].alt ? (' alt="' + escapeHtmlKeepEntities(tokens[idx].alt) + '"') : ''; var suffix = options.xhtml ? ' /' : ''; return ''; }; rules.table_open = function (/*tokens, idx, options*/) { return '\n'; }; rules.table_close = function (/*tokens, idx, options*/) { return '
    \n'; }; rules.tr_open = function (/*tokens, idx, options*/) { return '\n'; }; rules.tr_close = function (/*tokens, idx, options*/) { return '\n'; }; rules.th_open = function (tokens, idx /*, options*/) { var token = tokens[idx]; return ''; }; rules.th_close = function (/*tokens, idx, options*/) { return '\n'; }; rules.td_open = function (tokens, idx /*, options*/) { var token = tokens[idx]; return ''; }; rules.td_close = function (/*tokens, idx, options*/) { return '\n'; }; rules.strong_open = function(/*tokens, idx, options*/) { return ''; }; rules.strong_close = function(/*tokens, idx, options*/) { return ''; }; rules.em_open = function(/*tokens, idx, options*/) { return ''; }; rules.em_close = function(/*tokens, idx, options*/) { return ''; }; rules.hardbreak = function (tokens, idx, options) { return options.xhtml ? '
    \n' : '
    \n'; }; rules.softbreak = function (tokens, idx, options) { return options.breaks ? (options.xhtml ? '
    \n' : '
    \n') : '\n'; }; rules.text = function (tokens, idx /*, options*/) { return tokens[idx].content; }; rules.htmlblock = function (tokens, idx /*, options*/) { return tokens[idx].content; }; rules.htmltag = function (tokens, idx /*, options*/) { return tokens[idx].content; }; // Renderer class function Renderer() { // Clone rules object to allow local modifications this.rules = assign({}, rules); } Renderer.prototype.render = function (tokens, options) { var i, len, rule, name, next, result = '', rules = this.rules, tightStack = []; // wrap paragraphs on top level by default var tight = false; for (i = 0, len = tokens.length; i < len; i++) { name = tokens[i].type; rule = rules[name]; // Dirty stack machine to track lists style (loose/tight) if (name === 'ordered_list_open' || name === 'bullet_list_open') { tightStack.push(tight); tight = tokens[i].tight; } if (name === 'ordered_list_close' || name === 'bullet_list_close') { tight = tightStack.pop(); } if (name === 'blockquote_open') { tightStack.push(tight); tight = false; } if (name === 'blockquote_close') { tight = tightStack.pop(); } // in tight mode just ignore paragraphs for lists // TODO - track right nesting to blockquotes if (name === 'paragraph_open' && tight) { continue; } if (name === 'paragraph_close' && tight) { // Quick hack - texts should have LF if followed by blocks if (i + 1 < tokens.length) { next = tokens[i + 1].type; if (next === 'bullet_list_open' || next === 'ordered_list_open' || next === 'blockquote_open') { result += '\n'; } } continue; } if (tokens[i].type === 'inline') { result += this.render(tokens[i].children, options); } else { // TODO: temporary check if (!rule) { throw new Error('Renderer error: unknown token ' + name); } result += rule(tokens, i, options); } } return result; }; module.exports = Renderer;