|
|
@ -11,17 +11,11 @@ function escapeHtml(str) { |
|
|
|
.replace(/"/g, '"'); |
|
|
|
} |
|
|
|
|
|
|
|
var MD_UNESCAPE_RE = /\\([!"#$%&\'()*+,.\/:;<=>?@[\\\]^_`{|}~-])/g; |
|
|
|
|
|
|
|
function unescapeMd(str) { |
|
|
|
return str.replace(MD_UNESCAPE_RE, '$1'); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// check if we need to hide '\n' before next token
|
|
|
|
function getBreak(state, idx) { |
|
|
|
if (++idx < state.tokens.length && |
|
|
|
state.tokens[idx].type === 'list_item_close') { |
|
|
|
function getBreak(tokens, idx) { |
|
|
|
if (++idx < tokens.length && |
|
|
|
tokens[idx].type === 'list_item_close') { |
|
|
|
return ''; |
|
|
|
} |
|
|
|
|
|
|
@ -32,113 +26,122 @@ function getBreak(state, idx) { |
|
|
|
var rules = {}; |
|
|
|
|
|
|
|
|
|
|
|
rules.blockquote_open = function (state /*, token, idx*/) { |
|
|
|
state.result += '<blockquote>\n'; |
|
|
|
rules.blockquote_open = function (/*tokens, idx, options*/) { |
|
|
|
return '<blockquote>\n'; |
|
|
|
}; |
|
|
|
rules.blockquote_close = function (state, token, idx) { |
|
|
|
state.result += '</blockquote>' + getBreak(state, idx); |
|
|
|
rules.blockquote_close = function (tokens, idx /*, options*/) { |
|
|
|
return '</blockquote>' + getBreak(tokens, idx); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.code = function (state, token, idx) { |
|
|
|
state.result += '<pre><code>' + escapeHtml(token.content) + '</code></pre>' + getBreak(state, idx); |
|
|
|
rules.code = function (tokens, idx /*, options*/) { |
|
|
|
return '<pre><code>' + escapeHtml(tokens[idx].content) + '</code></pre>' + getBreak(tokens, idx); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.fence = function (state, token, idx) { |
|
|
|
rules.fence = function (tokens, idx, options) { |
|
|
|
var token = tokens[idx]; |
|
|
|
var langMark = ''; |
|
|
|
var langPrefix = state.options.codeLangPrefix || ''; |
|
|
|
var langPrefix = options.codeLangPrefix || ''; |
|
|
|
|
|
|
|
if (token.params.length) { |
|
|
|
langMark = ' class="' + langPrefix + escapeHtml(token.params[0]) + '"'; |
|
|
|
} |
|
|
|
|
|
|
|
state.result += '<pre><code' + langMark + '>' |
|
|
|
return '<pre><code' + langMark + '>' |
|
|
|
+ escapeHtml(token.content) |
|
|
|
+ '</code></pre>' + getBreak(state, idx); |
|
|
|
+ '</code></pre>' + getBreak(tokens, idx); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.heading_open = function (state, token) { |
|
|
|
state.result += '<h' + token.level + '>'; |
|
|
|
rules.heading_open = function (tokens, idx /*, options*/) { |
|
|
|
return '<h' + tokens[idx].level + '>'; |
|
|
|
}; |
|
|
|
rules.heading_close = function (state, token) { |
|
|
|
state.result += '</h' + token.level + '>\n'; |
|
|
|
rules.heading_close = function (tokens, idx /*, options*/) { |
|
|
|
return '</h' + tokens[idx].level + '>\n'; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.hr = function (state, token, idx) { |
|
|
|
state.result += (state.options.xhtml ? '<hr />' : '<hr>') + getBreak(state, idx); |
|
|
|
rules.hr = function (tokens, idx, options) { |
|
|
|
return (options.xhtml ? '<hr />' : '<hr>') + getBreak(tokens, idx); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.bullet_list_open = function (state /*, token, idx*/) { |
|
|
|
state.result += '<ul>\n'; |
|
|
|
rules.bullet_list_open = function (/*tokens, idx, options*/) { |
|
|
|
return '<ul>\n'; |
|
|
|
}; |
|
|
|
rules.bullet_list_close = function (state, token, idx) { |
|
|
|
state.result += '</ul>' + getBreak(state, idx); |
|
|
|
rules.bullet_list_close = function (tokens, idx /*, options*/) { |
|
|
|
return '</ul>' + getBreak(tokens, idx); |
|
|
|
}; |
|
|
|
rules.list_item_open = function (state /*, token, idx*/) { |
|
|
|
state.result += '<li>'; |
|
|
|
rules.list_item_open = function (/*tokens, idx, options*/) { |
|
|
|
return '<li>'; |
|
|
|
}; |
|
|
|
rules.list_item_close = function (state /*, token, idx*/) { |
|
|
|
state.result += '</li>\n'; |
|
|
|
rules.list_item_close = function (/*tokens, idx, options*/) { |
|
|
|
return '</li>\n'; |
|
|
|
}; |
|
|
|
rules.ordered_list_open = function (state, token /*, idx */) { |
|
|
|
state.result += '<ol' |
|
|
|
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 (state, token, idx) { |
|
|
|
state.result += '</ol>' + getBreak(state, idx); |
|
|
|
rules.ordered_list_close = function (tokens, idx /*, options*/) { |
|
|
|
return '</ol>' + getBreak(tokens, idx); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.paragraph_open = function (state /*, token, idx*/) { |
|
|
|
state.result += '<p>'; |
|
|
|
rules.paragraph_open = function (/*tokens, idx, options*/) { |
|
|
|
return '<p>'; |
|
|
|
}; |
|
|
|
rules.paragraph_close = function (state, token, idx) { |
|
|
|
state.result += '</p>' + getBreak(state, idx); |
|
|
|
rules.paragraph_close = function (tokens, idx /*, options*/) { |
|
|
|
return '</p>' + getBreak(tokens, idx); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.table_open = function (state /*, token, idx*/) { |
|
|
|
state.result += '<table>\n'; |
|
|
|
rules.table_open = function (/*tokens, idx, options*/) { |
|
|
|
return '<table>\n'; |
|
|
|
}; |
|
|
|
rules.table_close = function (state /*, token, idx*/) { |
|
|
|
state.result += '</table>\n'; |
|
|
|
rules.table_close = function (/*tokens, idx, options*/) { |
|
|
|
return '</table>\n'; |
|
|
|
}; |
|
|
|
rules.tr_open = function (state /*, token, idx*/) { |
|
|
|
state.result += '<tr>\n'; |
|
|
|
rules.tr_open = function (/*tokens, idx, options*/) { |
|
|
|
return '<tr>\n'; |
|
|
|
}; |
|
|
|
rules.tr_close = function (state /*, token, idx*/) { |
|
|
|
state.result += '</tr>\n'; |
|
|
|
rules.tr_close = function (/*tokens, idx, options*/) { |
|
|
|
return '</tr>\n'; |
|
|
|
}; |
|
|
|
rules.th_open = function (state, token) { |
|
|
|
state.result += '<th' |
|
|
|
rules.th_open = function (tokens, idx /*, options*/) { |
|
|
|
var token = tokens[idx]; |
|
|
|
return '<th' |
|
|
|
+ (token.align ? ' align="' + token.align + '"' : '') |
|
|
|
+ '>'; |
|
|
|
}; |
|
|
|
rules.th_close = function (state /*, token, idx*/) { |
|
|
|
state.result += '</th>\n'; |
|
|
|
rules.th_close = function (/*tokens, idx, options*/) { |
|
|
|
return '</th>\n'; |
|
|
|
}; |
|
|
|
rules.td_open = function (state, token) { |
|
|
|
state.result += '<td' |
|
|
|
rules.td_open = function (tokens, idx /*, options*/) { |
|
|
|
var token = tokens[idx]; |
|
|
|
return '<td' |
|
|
|
+ (token.align ? ' align="' + token.align + '"' : '') |
|
|
|
+ '>'; |
|
|
|
}; |
|
|
|
rules.td_close = function (state /*, token, idx*/) { |
|
|
|
state.result += '</td>\n'; |
|
|
|
rules.td_close = function (/*tokens, idx, options*/) { |
|
|
|
return '</td>\n'; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.text = function (state, token /*, idx*/) { |
|
|
|
state.result += escapeHtml(unescapeMd(token.content)); |
|
|
|
rules.hardbreak = function (tokens, idx, options) { |
|
|
|
return (options.xhtml ? '<br />' : '<br>') + '\n'; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.html = function (state, token /*, idx*/) { |
|
|
|
state.result += token.content; |
|
|
|
rules.text = function (tokens, idx /*, options*/) { |
|
|
|
return tokens[idx].content; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.html = function (tokens, idx /*, options*/) { |
|
|
|
return tokens[idx].content; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -148,63 +151,67 @@ function Renderer() { |
|
|
|
this.rules = assign({}, rules); |
|
|
|
} |
|
|
|
|
|
|
|
Renderer.prototype.render = function (state) { |
|
|
|
Renderer.prototype.render = function (tokens, options) { |
|
|
|
var i, len, rule, name, next, |
|
|
|
tokens = state.tokens, |
|
|
|
result = '', |
|
|
|
rules = this.rules, |
|
|
|
tightStack = []; |
|
|
|
|
|
|
|
// wrap paragraphs on top level by default
|
|
|
|
state.tight = false; |
|
|
|
var tight = false; |
|
|
|
|
|
|
|
for (i = 0, len = tokens.length; i < len; i++) { |
|
|
|
name = tokens[i].type; |
|
|
|
rule = rules[name]; |
|
|
|
|
|
|
|
// TODO: temporary check
|
|
|
|
if (!rule) { |
|
|
|
throw new Error('Renderer error: unknown token ' + name); |
|
|
|
} |
|
|
|
|
|
|
|
// Dirty stack machine to track lists style (loose/tight)
|
|
|
|
if (name === 'ordered_list_open' || name === 'bullet_list_open') { |
|
|
|
tightStack.push(state.tight); |
|
|
|
state.tight = tokens[i].tight; |
|
|
|
tightStack.push(tight); |
|
|
|
tight = tokens[i].tight; |
|
|
|
} |
|
|
|
if (name === 'ordered_list_close' || name === 'bullet_list_close') { |
|
|
|
state.tight = tightStack.pop(); |
|
|
|
tight = tightStack.pop(); |
|
|
|
} |
|
|
|
if (name === 'blockquote_open') { |
|
|
|
tightStack.push(state.tight); |
|
|
|
state.tight = false; |
|
|
|
tightStack.push(tight); |
|
|
|
tight = false; |
|
|
|
} |
|
|
|
if (name === 'blockquote_close') { |
|
|
|
state.tight = tightStack.pop(); |
|
|
|
tight = tightStack.pop(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// in tight mode just ignore paragraphs for lists
|
|
|
|
// TODO - track right nesting to blockquotes
|
|
|
|
if (name === 'paragraph_open' && state.tight) { |
|
|
|
if (name === 'paragraph_open' && tight) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
if (name === 'paragraph_close' && state.tight) { |
|
|
|
if (name === 'paragraph_close' && tight) { |
|
|
|
// Quick hack - texts should have LF if followed by blocks
|
|
|
|
if (i + 1 < state.tokens.length) { |
|
|
|
next = state.tokens[i + 1].type; |
|
|
|
if (i + 1 < tokens.length) { |
|
|
|
next = tokens[i + 1].type; |
|
|
|
if (next === 'bullet_list_open' || |
|
|
|
next === 'ordered_list_open' || |
|
|
|
next === 'blockquote_open') { |
|
|
|
state.result += '\n'; |
|
|
|
result += '\n'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
continue; |
|
|
|
} |
|
|
|
rule(state, tokens[i], i); |
|
|
|
|
|
|
|
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 state.result; |
|
|
|
return result; |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports = Renderer; |
|
|
|