Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed
https://markdown-it.github.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
289 lines
8.6 KiB
289 lines
8.6 KiB
/**
|
|
* 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 unescapeMd = require('./common/utils').unescapeMd;
|
|
var replaceEntities = require('./common/utils').replaceEntities;
|
|
var escapeHtml = require('./common/utils').escapeHtml;
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
var rules = {};
|
|
|
|
|
|
rules.blockquote_open = function () { return '<blockquote>\n'; };
|
|
rules.blockquote_close = function () { return '</blockquote>\n'; };
|
|
|
|
|
|
rules.code_block = function (tokens, idx /*, options, env */) {
|
|
return '<pre><code>' + escapeHtml(tokens[idx].content) + '</code></pre>\n';
|
|
};
|
|
rules.code_inline = function (tokens, idx /*, options, env */) {
|
|
return '<code>' + escapeHtml(tokens[idx].content) + '</code>';
|
|
};
|
|
|
|
|
|
rules.fence = function (tokens, idx, options /*, env, self*/) {
|
|
var token = tokens[idx];
|
|
var langClass = '';
|
|
var langPrefix = options.langPrefix;
|
|
var langName = '';
|
|
var highlighted;
|
|
|
|
if (token.params) {
|
|
langName = escapeHtml(replaceEntities(unescapeMd(token.params.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 '<pre><code' + langClass + '>'
|
|
+ highlighted
|
|
+ '</code></pre>\n';
|
|
};
|
|
|
|
|
|
rules.heading_open = function (tokens, idx /*, options, env */) {
|
|
return '<h' + tokens[idx].hLevel + '>';
|
|
};
|
|
rules.heading_close = function (tokens, idx /*, options, env */) {
|
|
return '</h' + tokens[idx].hLevel + '>\n';
|
|
};
|
|
|
|
|
|
rules.hr = function (tokens, idx, options /*, env */) {
|
|
return (options.xhtmlOut ? '<hr />\n' : '<hr>\n');
|
|
};
|
|
|
|
|
|
rules.bullet_list_open = function () { return '<ul>\n'; };
|
|
rules.bullet_list_close = function () { return '</ul>\n'; };
|
|
rules.list_item_open = function (tokens, idx /*, options, env */) {
|
|
var next = tokens[idx + 1];
|
|
if ((next.type === 'list_item_close') ||
|
|
(next.type === 'paragraph_open' && next.tight)) {
|
|
return '<li>';
|
|
}
|
|
return '<li>\n';
|
|
};
|
|
rules.list_item_close = function () { return '</li>\n'; };
|
|
rules.ordered_list_open = function (tokens, idx /*, options, env */) {
|
|
if (tokens[idx].order > 1) {
|
|
return '<ol start="' + tokens[idx].order + '">\n';
|
|
}
|
|
return '<ol>\n';
|
|
};
|
|
rules.ordered_list_close = function () { return '</ol>\n'; };
|
|
|
|
|
|
rules.paragraph_open = function (tokens, idx /*, options, env */) {
|
|
return tokens[idx].tight ? '' : '<p>';
|
|
};
|
|
rules.paragraph_close = function (tokens, idx /*, options, env */) {
|
|
if (tokens[idx].tight === true) {
|
|
return tokens[idx + 1].type.slice(-5) === 'close' ? '' : '\n';
|
|
}
|
|
return '</p>\n';
|
|
};
|
|
|
|
|
|
rules.link_open = function (tokens, idx /*, options, env */) {
|
|
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : '';
|
|
var target = tokens[idx].target ? (' target="' + escapeHtml(tokens[idx].target) + '"') : '';
|
|
return '<a href="' + escapeHtml(tokens[idx].href) + '"' + title + target + '>';
|
|
};
|
|
rules.link_close = function (/* tokens, idx, options, env */) {
|
|
return '</a>';
|
|
};
|
|
|
|
|
|
rules.image = function (tokens, idx, options, env, self) {
|
|
var src = ' src="' + escapeHtml(tokens[idx].src) + '"';
|
|
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : '';
|
|
var alt = ' alt="' + self.renderInlineAsText(tokens[idx].tokens, options, env) + '"';
|
|
var suffix = options.xhtmlOut ? ' /' : '';
|
|
return '<img' + src + alt + title + suffix + '>';
|
|
};
|
|
|
|
|
|
rules.table_open = function () { return '<table>\n'; };
|
|
rules.table_close = function () { return '</table>\n'; };
|
|
rules.thead_open = function () { return '<thead>\n'; };
|
|
rules.thead_close = function () { return '</thead>\n'; };
|
|
rules.tbody_open = function () { return '<tbody>\n'; };
|
|
rules.tbody_close = function () { return '</tbody>\n'; };
|
|
rules.tr_open = function () { return '<tr>'; };
|
|
rules.tr_close = function () { return '</tr>\n'; };
|
|
rules.th_open = function (tokens, idx /*, options, env */) {
|
|
if (tokens[idx].align) {
|
|
return '<th style="text-align:' + tokens[idx].align + '">';
|
|
}
|
|
return '<th>';
|
|
};
|
|
rules.th_close = function () { return '</th>'; };
|
|
rules.td_open = function (tokens, idx /*, options, env */) {
|
|
if (tokens[idx].align) {
|
|
return '<td style="text-align:' + tokens[idx].align + '">';
|
|
}
|
|
return '<td>';
|
|
};
|
|
rules.td_close = function () { return '</td>'; };
|
|
|
|
|
|
rules.strong_open = function () { return '<strong>'; };
|
|
rules.strong_close = function () { return '</strong>'; };
|
|
|
|
|
|
rules.em_open = function () { return '<em>'; };
|
|
rules.em_close = function () { return '</em>'; };
|
|
|
|
|
|
rules.s_open = function () { return '<s>'; };
|
|
rules.s_close = function () { return '</s>'; };
|
|
|
|
|
|
rules.hardbreak = function (tokens, idx, options /*, env */) {
|
|
return options.xhtmlOut ? '<br />\n' : '<br>\n';
|
|
};
|
|
rules.softbreak = function (tokens, idx, options /*, env */) {
|
|
return options.breaks ? (options.xhtmlOut ? '<br />\n' : '<br>\n') : '\n';
|
|
};
|
|
|
|
|
|
rules.text = function (tokens, idx /*, options, env */) {
|
|
return escapeHtml(tokens[idx].content);
|
|
};
|
|
|
|
|
|
rules.html_block = function (tokens, idx /*, options, env */) {
|
|
return tokens[idx].content;
|
|
};
|
|
rules.html_inline = function (tokens, idx /*, options, env */) {
|
|
return tokens[idx].content;
|
|
};
|
|
|
|
|
|
/**
|
|
* 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 '<b>'; };
|
|
* md.renderer.rules.strong_close = function () { return '</b>'; };
|
|
*
|
|
* 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({}, 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 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;
|
|
};
|
|
|
|
|
|
/** 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].tokens, 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,
|
|
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;
|
|
|