|
|
@ -56,23 +56,16 @@ function getBreak(tokens, idx) { |
|
|
|
var rules = {}; |
|
|
|
|
|
|
|
|
|
|
|
rules.abbr_open = function (tokens, idx/*, options*/) { |
|
|
|
return '<abbr title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '">'; |
|
|
|
}; |
|
|
|
rules.abbr_close = function (/*tokens, idx, options*/) { |
|
|
|
return '</abbr>'; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.blockquote_open = function (/*tokens, idx, options*/) { |
|
|
|
rules.blockquote_open = function () { |
|
|
|
return '<blockquote>\n'; |
|
|
|
}; |
|
|
|
rules.blockquote_close = function (tokens, idx /*, options*/) { |
|
|
|
rules.blockquote_close = function (tokens, idx) { |
|
|
|
return '</blockquote>' + getBreak(tokens, idx); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.code = function (tokens, idx /*, options*/) { |
|
|
|
rules.code = function (tokens, idx) { |
|
|
|
if (tokens[idx].block) { |
|
|
|
return '<pre><code>' + escapeHtml(tokens[idx].content) + '</code></pre>' + getBreak(tokens, idx); |
|
|
|
} |
|
|
@ -107,10 +100,10 @@ rules.fence = function (tokens, idx, options) { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.heading_open = function (tokens, idx /*, options*/) { |
|
|
|
rules.heading_open = function (tokens, idx) { |
|
|
|
return '<h' + tokens[idx].hLevel + '>'; |
|
|
|
}; |
|
|
|
rules.heading_close = function (tokens, idx /*, options*/) { |
|
|
|
rules.heading_close = function (tokens, idx) { |
|
|
|
return '</h' + tokens[idx].hLevel + '>\n'; |
|
|
|
}; |
|
|
|
|
|
|
@ -120,43 +113,43 @@ rules.hr = function (tokens, idx, options) { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.bullet_list_open = function (/*tokens, idx, options*/) { |
|
|
|
rules.bullet_list_open = function () { |
|
|
|
return '<ul>\n'; |
|
|
|
}; |
|
|
|
rules.bullet_list_close = function (tokens, idx /*, options*/) { |
|
|
|
rules.bullet_list_close = function (tokens, idx) { |
|
|
|
return '</ul>' + getBreak(tokens, idx); |
|
|
|
}; |
|
|
|
rules.list_item_open = function (/*tokens, idx, options*/) { |
|
|
|
rules.list_item_open = function () { |
|
|
|
return '<li>'; |
|
|
|
}; |
|
|
|
rules.list_item_close = function (/*tokens, idx, options*/) { |
|
|
|
rules.list_item_close = function () { |
|
|
|
return '</li>\n'; |
|
|
|
}; |
|
|
|
rules.ordered_list_open = function (tokens, idx /*, options*/) { |
|
|
|
rules.ordered_list_open = function (tokens, idx) { |
|
|
|
var token = tokens[idx]; |
|
|
|
return '<ol' |
|
|
|
+ (token.order > 1 ? ' start="' + token.order + '"' : '') |
|
|
|
+ '>\n'; |
|
|
|
}; |
|
|
|
rules.ordered_list_close = function (tokens, idx /*, options*/) { |
|
|
|
rules.ordered_list_close = function (tokens, idx) { |
|
|
|
return '</ol>' + getBreak(tokens, idx); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.paragraph_open = function (tokens, idx/*, options*/) { |
|
|
|
rules.paragraph_open = function (tokens, idx) { |
|
|
|
return tokens[idx].tight ? '' : '<p>'; |
|
|
|
}; |
|
|
|
rules.paragraph_close = function (tokens, idx /*, options*/) { |
|
|
|
rules.paragraph_close = function (tokens, idx) { |
|
|
|
var addBreak = !(tokens[idx].tight && idx && tokens[idx - 1].type === 'inline' && !tokens[idx - 1].content); |
|
|
|
return (tokens[idx].tight ? '' : '</p>') + (addBreak ? getBreak(tokens, idx) : ''); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.link_open = function (tokens, idx /*, options*/) { |
|
|
|
rules.link_open = function (tokens, idx) { |
|
|
|
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; |
|
|
|
return '<a href="' + escapeHtml(encodeURI(decodeURI(replaceEntities(tokens[idx].href)))) + '"' + title + '>'; |
|
|
|
}; |
|
|
|
rules.link_close = function (/*tokens, idx, options*/) { |
|
|
|
rules.link_close = function () { |
|
|
|
return '</a>'; |
|
|
|
}; |
|
|
|
|
|
|
@ -170,84 +163,84 @@ rules.image = function (tokens, idx, options) { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.table_open = function (/*tokens, idx, options*/) { |
|
|
|
rules.table_open = function () { |
|
|
|
return '<table>\n'; |
|
|
|
}; |
|
|
|
rules.table_close = function (/*tokens, idx, options*/) { |
|
|
|
rules.table_close = function () { |
|
|
|
return '</table>\n'; |
|
|
|
}; |
|
|
|
rules.thead_open = function (/*tokens, idx, options*/) { |
|
|
|
rules.thead_open = function () { |
|
|
|
return '<thead>\n'; |
|
|
|
}; |
|
|
|
rules.thead_close = function (/*tokens, idx, options*/) { |
|
|
|
rules.thead_close = function () { |
|
|
|
return '</thead>\n'; |
|
|
|
}; |
|
|
|
rules.tbody_open = function (/*tokens, idx, options*/) { |
|
|
|
rules.tbody_open = function () { |
|
|
|
return '<tbody>\n'; |
|
|
|
}; |
|
|
|
rules.tbody_close = function (/*tokens, idx, options*/) { |
|
|
|
rules.tbody_close = function () { |
|
|
|
return '</tbody>\n'; |
|
|
|
}; |
|
|
|
rules.tr_open = function (/*tokens, idx, options*/) { |
|
|
|
rules.tr_open = function () { |
|
|
|
return '<tr>'; |
|
|
|
}; |
|
|
|
rules.tr_close = function (/*tokens, idx, options*/) { |
|
|
|
rules.tr_close = function () { |
|
|
|
return '</tr>\n'; |
|
|
|
}; |
|
|
|
rules.th_open = function (tokens, idx /*, options*/) { |
|
|
|
rules.th_open = function (tokens, idx) { |
|
|
|
var token = tokens[idx]; |
|
|
|
return '<th' |
|
|
|
+ (token.align ? ' style="text-align:' + token.align + '"' : '') |
|
|
|
+ '>'; |
|
|
|
}; |
|
|
|
rules.th_close = function (/*tokens, idx, options*/) { |
|
|
|
rules.th_close = function () { |
|
|
|
return '</th>'; |
|
|
|
}; |
|
|
|
rules.td_open = function (tokens, idx /*, options*/) { |
|
|
|
rules.td_open = function (tokens, idx) { |
|
|
|
var token = tokens[idx]; |
|
|
|
return '<td' |
|
|
|
+ (token.align ? ' style="text-align:' + token.align + '"' : '') |
|
|
|
+ '>'; |
|
|
|
}; |
|
|
|
rules.td_close = function (/*tokens, idx, options*/) { |
|
|
|
rules.td_close = function () { |
|
|
|
return '</td>'; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.strong_open = function(/*tokens, idx, options*/) { |
|
|
|
rules.strong_open = function() { |
|
|
|
return '<strong>'; |
|
|
|
}; |
|
|
|
rules.strong_close = function(/*tokens, idx, options*/) { |
|
|
|
rules.strong_close = function() { |
|
|
|
return '</strong>'; |
|
|
|
}; |
|
|
|
rules.em_open = function(/*tokens, idx, options*/) { |
|
|
|
rules.em_open = function() { |
|
|
|
return '<em>'; |
|
|
|
}; |
|
|
|
rules.em_close = function(/*tokens, idx, options*/) { |
|
|
|
rules.em_close = function() { |
|
|
|
return '</em>'; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.del_open = function(/*tokens, idx, options*/) { |
|
|
|
rules.del_open = function() { |
|
|
|
return '<del>'; |
|
|
|
}; |
|
|
|
rules.del_close = function(/*tokens, idx, options*/) { |
|
|
|
rules.del_close = function() { |
|
|
|
return '</del>'; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.ins_open = function(/*tokens, idx, options*/) { |
|
|
|
rules.ins_open = function() { |
|
|
|
return '<ins>'; |
|
|
|
}; |
|
|
|
rules.ins_close = function(/*tokens, idx, options*/) { |
|
|
|
rules.ins_close = function() { |
|
|
|
return '</ins>'; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.mark_open = function(/*tokens, idx, options*/) { |
|
|
|
rules.mark_open = function() { |
|
|
|
return '<mark>'; |
|
|
|
}; |
|
|
|
rules.mark_close = function(/*tokens, idx, options*/) { |
|
|
|
rules.mark_close = function() { |
|
|
|
return '</mark>'; |
|
|
|
}; |
|
|
|
|
|
|
@ -268,19 +261,27 @@ rules.softbreak = function (tokens, idx, options) { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.text = function (tokens, idx /*, options*/) { |
|
|
|
rules.text = function (tokens, idx) { |
|
|
|
return escapeHtml(tokens[idx].content); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.htmlblock = function (tokens, idx /*, options*/) { |
|
|
|
rules.htmlblock = function (tokens, idx) { |
|
|
|
return tokens[idx].content; |
|
|
|
}; |
|
|
|
rules.htmltag = function (tokens, idx /*, options*/) { |
|
|
|
rules.htmltag = function (tokens, idx) { |
|
|
|
return tokens[idx].content; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.abbr_open = function (tokens, idx) { |
|
|
|
return '<abbr title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '">'; |
|
|
|
}; |
|
|
|
rules.abbr_close = function () { |
|
|
|
return '</abbr>'; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// Renderer class
|
|
|
|
function Renderer() { |
|
|
|
// Clone rules object to allow local modifications
|
|
|
@ -288,28 +289,28 @@ function Renderer() { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Renderer.prototype.renderInline = function (tokens, options) { |
|
|
|
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); |
|
|
|
result += _rules[tokens[i].type](tokens, i, options, env); |
|
|
|
} |
|
|
|
|
|
|
|
return result; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Renderer.prototype.render = function (tokens, options) { |
|
|
|
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); |
|
|
|
result += this.renderInline(tokens[i].children, options, env); |
|
|
|
} else { |
|
|
|
result += _rules[tokens[i].type](tokens, i, options); |
|
|
|
result += _rules[tokens[i].type](tokens, i, options, env); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|