/** * class Renderer * * Generates HTML from parsed token stream. Each instance has independent * copy of rules. Those can be rewritten with ease. Also, you can add new * rules if you create plugin and adds new token types. **/ 'use strict'; var assign = require('./common/utils').assign; var unescapeAll = require('./common/utils').unescapeAll; var escapeHtml = require('./common/utils').escapeHtml; //////////////////////////////////////////////////////////////////////////////// var default_rules = {}; default_rules.code_block = function (tokens, idx /*, options, env */) { return '
' + escapeHtml(tokens[idx].content) + '
\n'; }; default_rules.fence = function (tokens, idx, options /*, env, self*/) { var token = tokens[idx]; var langClass = ''; var langPrefix = options.langPrefix; var langName = ''; var highlighted; if (token.info) { langName = escapeHtml(unescapeAll(token.info.trim().split(/\s+/g)[0])); langClass = ' class="' + langPrefix + langName + '"'; } if (options.highlight) { highlighted = options.highlight(token.content, langName) || escapeHtml(token.content); } else { highlighted = escapeHtml(token.content); } return '
'
        + highlighted
        + '
\n'; }; default_rules.image = function (tokens, idx, options, env, self) { var i, token = tokens[idx], alt = self.renderInlineAsText(tokens[idx].children, options, env); if (!token.attrs) { token.attrs = []; } // Replace "alt" tag with children tags rendered as text // for (i = 0; i < token.attrs.length; i++) { if (token.attrs[i][0] === 'alt') { token.attrs[i][1] = alt; } } return self.rules.default_token(tokens, idx, options, env, self); }; default_rules.hardbreak = function (tokens, idx, options /*, env */) { return options.xhtmlOut ? '
\n' : '
\n'; }; default_rules.softbreak = function (tokens, idx, options /*, env */) { return options.breaks ? (options.xhtmlOut ? '
\n' : '
\n') : '\n'; }; default_rules.text = function (tokens, idx /*, options, env */) { return escapeHtml(tokens[idx].content); }; default_rules.html_block = function (tokens, idx /*, options, env */) { return tokens[idx].content; }; default_rules.html_inline = function (tokens, idx /*, options, env */) { return tokens[idx].content; }; default_rules.default_token = function (tokens, idx, options /*, env, self */) { var i, l, nextToken, result = '', needLf = false, token = tokens[idx]; // Insert a newline between hidden paragraph and subsequent opening // block-level tag. // // For example, here we should insert a newline before blockquote: // - a // > // if (token.block && idx && tokens[idx - 1].hidden && token.nesting !== -1) { result += '\n'; } // Self-contained token with no tag name, e.g. `inline` or hidden paragraph. // // We should return content if it exists and an empty string otherwise. // if (!token.tag) { result += (token.content ? escapeHtml(token.content) : ''); return result; } // Add token name, e.g. ``. // needLf = false; } } } } // If it's self-contained token, add its contents + closing tag // if (token.nesting === 0 && token.content !== null) { result += '>' + escapeHtml(token.content) + '\n' : '>'; return result; }; /** * new Renderer() * * Creates new [[Renderer]] instance and fill [[Renderer#rules]] with defaults. **/ function Renderer() { /** * Renderer#rules -> Object * * Contains render rules for tokens. Can be updated and extended. * * ##### Example * * ```javascript * var md = require('markdown-it')(); * * md.renderer.rules.strong_open = function () { return ''; }; * md.renderer.rules.strong_close = function () { return ''; }; * * var result = md.renderInline(...); * ``` * * Each rule is called as independed static function with fixed signature: * * ```javascript * function my_token_render(tokens, idx, options, env, renderer) { * // ... * return renderedHTML; * } * ``` * * See [source code](https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.js) * for more details and examples. **/ this.rules = assign({}, default_rules); } /** * Renderer.renderInline(tokens, options, env) -> String * - tokens (Array): list on block tokens to renter * - options (Object): params of parser instance * - env (Object): additional data from parsed input (references, for example) * * The same as [[Renderer.render]], but for single token of `inline` type. **/ Renderer.prototype.renderInline = function (tokens, options, env) { var type, result = '', rules = this.rules; for (var i = 0, len = tokens.length; i < len; i++) { type = tokens[i].type; if (typeof rules[type] !== 'undefined') { result += rules[type](tokens, i, options, env, this); } else { result += rules.default_token(tokens, i, options, env); } } return result; }; /** internal * Renderer.renderInlineAsText(tokens, options, env) -> String * - tokens (Array): list on block tokens to renter * - options (Object): params of parser instance * - env (Object): additional data from parsed input (references, for example) * * Special kludge for image `alt` attributes to conform CommonMark spec. * Don't try to use it! Spec requires to show `alt` content with stripped markup, * instead of simple escaping. **/ Renderer.prototype.renderInlineAsText = function (tokens, options, env) { var result = '', rules = this.rules; for (var i = 0, len = tokens.length; i < len; i++) { if (tokens[i].type === 'text') { result += rules.text(tokens, i, options, env, this); } else if (tokens[i].type === 'image') { result += this.renderInlineAsText(tokens[i].children, options, env); } } return result; }; /** * Renderer.render(tokens, options, env) -> String * - tokens (Array): list on block tokens to renter * - options (Object): params of parser instance * - env (Object): additional data from parsed input (references, for example) * * Takes token stream and generates HTML. Probably, you will never need to call * this method directly. **/ Renderer.prototype.render = function (tokens, options, env) { var i, len, type, result = '', rules = this.rules; for (i = 0, len = tokens.length; i < len; i++) { type = tokens[i].type; if (type === 'inline') { result += this.renderInline(tokens[i].children, options, env); } else if (typeof rules[type] !== 'undefined') { result += rules[tokens[i].type](tokens, i, options, env, this); } else { result += rules.default_token(tokens, i, options, env); } } return result; }; module.exports = Renderer;