'use strict'; var assign = require('object-assign'); function escapeHtml(str) { return str.replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"'); } var MD_UNESCAPE_RE = /\\([!"#$%&\'()*+,.\/:;<=>?@[\\\]^_`{|}~-])/g; function unescapeMd(str) { return str.replace(MD_UNESCAPE_RE, '$1'); } var rules = {}; rules.blockquote_open = function (state /*, token*/) { state.result += '
\n'; }; rules.blockquote_close = function (state /*, token*/) { state.result += '
\n'; }; rules.code = function (state, token) { state.result += '
' + escapeHtml(token.content) + '
\n'; }; rules.fence = function (state, token) { var langMark = ''; var langPrefix = state.options.codeLangPrefix || ''; if (token.params.length) { langMark = ' class="' + langPrefix + escapeHtml(token.params[0]) + '"'; } state.result += '
' + escapeHtml(token.content) + '
\n'; }; rules.heading_open = function (state, token) { state.result += ''; }; rules.heading_close = function (state, token) { state.result += '\n'; }; rules.hr = function (state/*, token*/) { state.result += state.options.xhtml ? '
\n' : '
\n'; }; rules.bullet_list_open = function (state /*, token*/) { state.result += '\n'; }; rules.list_item_open = function (state /*, token*/) { state.result += '
  • '; }; rules.list_item_close = function (state /*, token*/) { state.result += '
  • \n'; }; rules.ordered_list_open = function (state, token) { state.result += ' 1 ? ' start="' + token.order + '"' : '') + '>\n'; }; rules.ordered_list_close = function (state /*, token*/) { state.result += '\n'; }; rules.paragraph_open = function (state /*, token*/) { state.result += '

    '; }; rules.paragraph_close = function (state /*, token*/) { state.result += '

    \n'; }; rules.table_open = function (state /*, token*/) { state.result += '\n'; }; rules.table_close = function (state /*, token*/) { state.result += '
    \n'; }; rules.tr_open = function (state /*, token*/) { state.result += '\n'; }; rules.tr_close = function (state /*, token*/) { state.result += '\n'; }; rules.th_open = function (state, token) { state.result += ''; }; rules.th_close = function (state /*, token*/) { state.result += '\n'; }; rules.td_open = function (state, token) { state.result += ''; }; rules.td_close = function (state /*, token*/) { state.result += '\n'; }; rules.text = function (state, token) { state.result += escapeHtml(unescapeMd(token.content)); }; // Renderer class function Renderer() { // Clone rules object to allow local modifications this.rules = assign({}, rules); } Renderer.prototype.render = function (state) { var i, len, rule, tokens = state.tokens, rules = this.rules; for (i = 0, len = tokens.length; i < len; i++) { rule = rules[tokens[i].type]; // TODO: temporary check if (!rule) { throw new Error('Renderer error: unknown token ' + tokens[i].type); } rule(state, tokens[i]); } return state.result; }; module.exports = Renderer;