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.

274 lines
7.0 KiB

'use strict';
var assign = require('object-assign');
var escapeHtml = require('./helpers').escapeHtml;
var unescapeMd = require('./helpers').unescapeMd;
var replaceEntities = require('./helpers').replaceEntities;
function escapeUrl(str) {
try {
return encodeURI(str);
} catch (__) {}
return '';
}
function unescapeUrl(str) {
try {
return decodeURI(str);
} catch (__) {}
return '';
}
// 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 '<blockquote>\n';
};
rules.blockquote_close = function (tokens, idx /*, options*/) {
return '</blockquote>' + getBreak(tokens, idx);
};
rules.code = function (tokens, idx /*, options*/) {
if (tokens[idx].block) {
return '<pre><code>' + escapeHtml(tokens[idx].content) + '</code></pre>' + getBreak(tokens, idx);
}
return '<code>' + escapeHtml(tokens[idx].content) + '</code>';
};
rules.fence = function (tokens, idx, options) {
var token = tokens[idx];
var langClass = '';
10 years ago
var langPrefix = options.langPrefix || '';
var params, langName = '';
var highlighted;
if (token.params) {
params = token.params.split(/ +/g);
langName = escapeHtml(replaceEntities(unescapeMd(params[0])));
langClass = ' class="' + langPrefix + langName + '"';
}
highlighted = options.highlight(token.content, langName) || escapeHtml(token.content);
return '<pre><code' + langClass + '>'
+ highlighted
+ '</code></pre>' + getBreak(tokens, idx);
};
rules.heading_open = function (tokens, idx /*, options*/) {
return '<h' + tokens[idx].hLevel + '>';
};
rules.heading_close = function (tokens, idx /*, options*/) {
return '</h' + tokens[idx].hLevel + '>\n';
};
rules.hr = function (tokens, idx, options) {
return (options.xhtml ? '<hr />' : '<hr>') + getBreak(tokens, idx);
};
rules.bullet_list_open = function (/*tokens, idx, options*/) {
return '<ul>\n';
};
rules.bullet_list_close = function (tokens, idx /*, options*/) {
return '</ul>' + getBreak(tokens, idx);
};
rules.list_item_open = function (/*tokens, idx, options*/) {
return '<li>';
};
rules.list_item_close = function (/*tokens, idx, options*/) {
return '</li>\n';
};
rules.ordered_list_open = function (tokens, idx /*, options*/) {
var token = tokens[idx];
return '<ol'
+ (token.order > 1 ? ' start="' + token.order + '"' : '')
+ '>\n';
};
rules.ordered_list_close = function (tokens, idx /*, options*/) {
return '</ol>' + getBreak(tokens, idx);
};
rules.paragraph_open = function (/*tokens, idx, options*/) {
return '<p>';
};
rules.paragraph_close = function (tokens, idx /*, options*/) {
return '</p>' + getBreak(tokens, idx);
};
rules.link_open = function (tokens, idx /*, options*/) {
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : '';
return '<a href="' + escapeHtml(escapeUrl(unescapeUrl(replaceEntities(tokens[idx].href)))) + '"' + title + '>';
};
rules.link_close = function (/*tokens, idx, options*/) {
return '</a>';
};
rules.image = function (tokens, idx, options) {
var src = ' src="' + escapeHtml(escapeUrl(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.xhtml ? ' /' : '';
return '<img' + src + alt + title + suffix + '>';
};
rules.table_open = function (/*tokens, idx, options*/) {
return '<table>\n';
};
rules.table_close = function (/*tokens, idx, options*/) {
return '</table>\n';
};
rules.tr_open = function (/*tokens, idx, options*/) {
return '<tr>\n';
};
rules.tr_close = function (/*tokens, idx, options*/) {
return '</tr>\n';
};
rules.th_open = function (tokens, idx /*, options*/) {
var token = tokens[idx];
return '<th'
+ (token.align ? ' align="' + token.align + '"' : '')
+ '>';
};
rules.th_close = function (/*tokens, idx, options*/) {
return '</th>\n';
};
rules.td_open = function (tokens, idx /*, options*/) {
var token = tokens[idx];
return '<td'
+ (token.align ? ' align="' + token.align + '"' : '')
+ '>';
};
rules.td_close = function (/*tokens, idx, options*/) {
return '</td>\n';
};
rules.strong_open = function(/*tokens, idx, options*/) {
return '<strong>';
};
rules.strong_close = function(/*tokens, idx, options*/) {
return '</strong>';
};
rules.em_open = function(/*tokens, idx, options*/) {
return '<em>';
};
rules.em_close = function(/*tokens, idx, options*/) {
return '</em>';
};
rules.hardbreak = function (tokens, idx, options) {
return options.xhtml ? '<br />\n' : '<br>\n';
};
rules.softbreak = function (tokens, idx, options) {
return options.breaks ? (options.xhtml ? '<br />\n' : '<br>\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;