|
|
@ -15,30 +15,23 @@ var escapeHtml = require('./common/utils').escapeHtml; |
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
var rules = {}; |
|
|
|
var default_rules = {}; |
|
|
|
|
|
|
|
|
|
|
|
rules.blockquote_open = function () { return '<blockquote>\n'; }; |
|
|
|
rules.blockquote_close = function () { return '</blockquote>\n'; }; |
|
|
|
|
|
|
|
|
|
|
|
rules.code_block = function (tokens, idx /*, options, env */) { |
|
|
|
default_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*/) { |
|
|
|
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.params) { |
|
|
|
langName = escapeHtml(unescapeAll(token.params.split(/\s+/g)[0])); |
|
|
|
if (token.info) { |
|
|
|
langName = escapeHtml(unescapeAll(token.info.split(/\s+/g)[0])); |
|
|
|
langClass = ' class="' + langPrefix + langName + '"'; |
|
|
|
} |
|
|
|
|
|
|
@ -55,123 +48,120 @@ rules.fence = function (tokens, idx, options /*, env, self*/) { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
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'; |
|
|
|
}; |
|
|
|
|
|
|
|
default_rules.image = function (tokens, idx, options, env, self) { |
|
|
|
var i, token = tokens[idx], |
|
|
|
alt = self.renderInlineAsText(tokens[idx].children, options, env); |
|
|
|
|
|
|
|
rules.hr = function (tokens, idx, options /*, env */) { |
|
|
|
return (options.xhtmlOut ? '<hr />\n' : '<hr>\n'); |
|
|
|
}; |
|
|
|
if (!token.attrs) { token.attrs = []; } |
|
|
|
|
|
|
|
|
|
|
|
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'; |
|
|
|
// 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 '<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'; |
|
|
|
return self.rules.default_token(tokens, idx, options, env, self); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.link_open = function (tokens, idx /*, options, env */) { |
|
|
|
var title = tokens[idx].title ? (' title="' + escapeHtml(tokens[idx].title) + '"') : ''; |
|
|
|
var target = tokens[idx].target ? (' target="' + escapeHtml(tokens[idx].target) + '"') : ''; |
|
|
|
return '<a href="' + escapeHtml(tokens[idx].href) + '"' + title + target + '>'; |
|
|
|
default_rules.hardbreak = function (tokens, idx, options /*, env */) { |
|
|
|
return options.xhtmlOut ? '<br />\n' : '<br>\n'; |
|
|
|
}; |
|
|
|
rules.link_close = function (/* tokens, idx, options, env */) { |
|
|
|
return '</a>'; |
|
|
|
default_rules.softbreak = function (tokens, idx, options /*, env */) { |
|
|
|
return options.breaks ? (options.xhtmlOut ? '<br />\n' : '<br>\n') : '\n'; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
rules.image = function (tokens, idx, options, env, self) { |
|
|
|
var src = ' src="' + escapeHtml(tokens[idx].src) + '"'; |
|
|
|
var title = tokens[idx].title ? (' title="' + escapeHtml(tokens[idx].title) + '"') : ''; |
|
|
|
var alt = ' alt="' + self.renderInlineAsText(tokens[idx].tokens, options, env) + '"'; |
|
|
|
var suffix = options.xhtmlOut ? ' /' : ''; |
|
|
|
return '<img' + src + alt + title + suffix + '>'; |
|
|
|
default_rules.text = function (tokens, idx /*, options, env */) { |
|
|
|
return escapeHtml(tokens[idx].content); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
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>'; |
|
|
|
default_rules.html_block = function (tokens, idx /*, options, env */) { |
|
|
|
return tokens[idx].content; |
|
|
|
}; |
|
|
|
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>'; |
|
|
|
default_rules.html_inline = function (tokens, idx /*, options, env */) { |
|
|
|
return tokens[idx].content; |
|
|
|
}; |
|
|
|
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>'; }; |
|
|
|
|
|
|
|
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'; |
|
|
|
} |
|
|
|
|
|
|
|
rules.s_open = function () { return '<s>'; }; |
|
|
|
rules.s_close = function () { return '</s>'; }; |
|
|
|
// 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. `<img`
|
|
|
|
//
|
|
|
|
result += (token.nesting === -1 ? '</' : '<') + token.tag; |
|
|
|
|
|
|
|
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'; |
|
|
|
}; |
|
|
|
// Encode attributes, e.g. `<img src="foo"`
|
|
|
|
// ^^^^^^^^^^
|
|
|
|
if (token.attrs) { |
|
|
|
for (i = 0, l = token.attrs.length; i < l; i++) { |
|
|
|
result += ' ' + escapeHtml(token.attrs[i][0]) + '="' + escapeHtml(token.attrs[i][1]) + '"'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Add a slash for self-closing tags, e.g. `<img src="foo" /`
|
|
|
|
// ^^
|
|
|
|
if (token.nesting === 0 && options.xhtmlOut && token.content === null) { |
|
|
|
result += ' /'; |
|
|
|
} |
|
|
|
|
|
|
|
rules.text = function (tokens, idx /*, options, env */) { |
|
|
|
return escapeHtml(tokens[idx].content); |
|
|
|
}; |
|
|
|
// Check if we need to add a newline after this tag
|
|
|
|
//
|
|
|
|
if (token.block) { |
|
|
|
needLf = true; |
|
|
|
|
|
|
|
if (token.nesting === 1) { |
|
|
|
if (idx + 1 < tokens.length) { |
|
|
|
nextToken = tokens[idx + 1]; |
|
|
|
|
|
|
|
if (nextToken.type === 'inline' || nextToken.hidden) { |
|
|
|
// Block-level tag containing an inline tag.
|
|
|
|
//
|
|
|
|
needLf = false; |
|
|
|
|
|
|
|
} else if (nextToken.nesting === -1 && nextToken.tag === token.tag) { |
|
|
|
// Opening tag + closing tag of the same type. E.g. `<li></li>`.
|
|
|
|
//
|
|
|
|
needLf = false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// If it's self-contained token, add its contents + closing tag
|
|
|
|
//
|
|
|
|
if (token.nesting === 0 && token.content !== null) { |
|
|
|
result += '>' + escapeHtml(token.content) + '</' + token.tag; |
|
|
|
} |
|
|
|
|
|
|
|
rules.html_block = function (tokens, idx /*, options, env */) { |
|
|
|
return tokens[idx].content; |
|
|
|
}; |
|
|
|
rules.html_inline = function (tokens, idx /*, options, env */) { |
|
|
|
return tokens[idx].content; |
|
|
|
result += needLf ? '>\n' : '>'; |
|
|
|
return result; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -210,7 +200,7 @@ function Renderer() { |
|
|
|
* See [source code](https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.js)
|
|
|
|
* for more details and examples. |
|
|
|
**/ |
|
|
|
this.rules = assign({}, rules); |
|
|
|
this.rules = assign({}, default_rules); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -223,11 +213,18 @@ function Renderer() { |
|
|
|
* The same as [[Renderer.render]], but for single token of `inline` type. |
|
|
|
**/ |
|
|
|
Renderer.prototype.renderInline = function (tokens, options, env) { |
|
|
|
var result = '', |
|
|
|
_rules = this.rules; |
|
|
|
var type, |
|
|
|
result = '', |
|
|
|
rules = this.rules; |
|
|
|
|
|
|
|
for (var i = 0, len = tokens.length; i < len; i++) { |
|
|
|
result += _rules[tokens[i].type](tokens, i, options, env, this); |
|
|
|
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; |
|
|
@ -246,13 +243,13 @@ Renderer.prototype.renderInline = function (tokens, options, env) { |
|
|
|
**/ |
|
|
|
Renderer.prototype.renderInlineAsText = function (tokens, options, env) { |
|
|
|
var result = '', |
|
|
|
_rules = this.rules; |
|
|
|
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); |
|
|
|
result += rules.text(tokens, i, options, env, this); |
|
|
|
} else if (tokens[i].type === 'image') { |
|
|
|
result += this.renderInlineAsText(tokens[i].tokens, options, env); |
|
|
|
result += this.renderInlineAsText(tokens[i].children, options, env); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -270,15 +267,19 @@ Renderer.prototype.renderInlineAsText = function (tokens, options, env) { |
|
|
|
* this method directly. |
|
|
|
**/ |
|
|
|
Renderer.prototype.render = function (tokens, options, env) { |
|
|
|
var i, len, |
|
|
|
var i, len, type, |
|
|
|
result = '', |
|
|
|
_rules = this.rules; |
|
|
|
rules = this.rules; |
|
|
|
|
|
|
|
for (i = 0, len = tokens.length; i < len; i++) { |
|
|
|
if (tokens[i].type === 'inline') { |
|
|
|
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[tokens[i].type](tokens, i, options, env, this); |
|
|
|
result += rules.default_token(tokens, i, options, env); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|