|
@ -15,30 +15,23 @@ var escapeHtml = require('./common/utils').escapeHtml; |
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
var rules = {}; |
|
|
var default_rules = {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rules.blockquote_open = function () { return '<blockquote>\n'; }; |
|
|
default_rules.code_block = function (tokens, idx /*, options, env */) { |
|
|
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'; |
|
|
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 token = tokens[idx]; |
|
|
var langClass = ''; |
|
|
var langClass = ''; |
|
|
var langPrefix = options.langPrefix; |
|
|
var langPrefix = options.langPrefix; |
|
|
var langName = ''; |
|
|
var langName = ''; |
|
|
var highlighted; |
|
|
var highlighted; |
|
|
|
|
|
|
|
|
if (token.params) { |
|
|
if (token.info) { |
|
|
langName = escapeHtml(unescapeAll(token.params.split(/\s+/g)[0])); |
|
|
langName = escapeHtml(unescapeAll(token.info.split(/\s+/g)[0])); |
|
|
langClass = ' class="' + langPrefix + langName + '"'; |
|
|
langClass = ' class="' + langPrefix + langName + '"'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -55,123 +48,120 @@ rules.fence = function (tokens, idx, options /*, env, self*/) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rules.heading_open = function (tokens, idx /*, options, env */) { |
|
|
default_rules.image = function (tokens, idx, options, env, self) { |
|
|
return '<h' + tokens[idx].hLevel + '>'; |
|
|
var i, token = tokens[idx], |
|
|
}; |
|
|
alt = self.renderInlineAsText(tokens[idx].children, options, env); |
|
|
rules.heading_close = function (tokens, idx /*, options, env */) { |
|
|
|
|
|
return '</h' + tokens[idx].hLevel + '>\n'; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rules.hr = function (tokens, idx, options /*, env */) { |
|
|
if (!token.attrs) { token.attrs = []; } |
|
|
return (options.xhtmlOut ? '<hr />\n' : '<hr>\n'); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Replace "alt" tag with children tags rendered as text
|
|
|
rules.bullet_list_open = function () { return '<ul>\n'; }; |
|
|
//
|
|
|
rules.bullet_list_close = function () { return '</ul>\n'; }; |
|
|
for (i = 0; i < token.attrs.length; i++) { |
|
|
rules.list_item_open = function (tokens, idx /*, options, env */) { |
|
|
if (token.attrs[i][0] === 'alt') { |
|
|
var next = tokens[idx + 1]; |
|
|
token.attrs[i][1] = alt; |
|
|
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 self.rules.default_token(tokens, idx, options, env, self); |
|
|
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 */) { |
|
|
default_rules.hardbreak = function (tokens, idx, options /*, env */) { |
|
|
var title = tokens[idx].title ? (' title="' + escapeHtml(tokens[idx].title) + '"') : ''; |
|
|
return options.xhtmlOut ? '<br />\n' : '<br>\n'; |
|
|
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 */) { |
|
|
default_rules.softbreak = function (tokens, idx, options /*, env */) { |
|
|
return '</a>'; |
|
|
return options.breaks ? (options.xhtmlOut ? '<br />\n' : '<br>\n') : '\n'; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rules.image = function (tokens, idx, options, env, self) { |
|
|
default_rules.text = function (tokens, idx /*, options, env */) { |
|
|
var src = ' src="' + escapeHtml(tokens[idx].src) + '"'; |
|
|
return escapeHtml(tokens[idx].content); |
|
|
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 + '>'; |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rules.table_open = function () { return '<table>\n'; }; |
|
|
default_rules.html_block = function (tokens, idx /*, options, env */) { |
|
|
rules.table_close = function () { return '</table>\n'; }; |
|
|
return tokens[idx].content; |
|
|
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>'; }; |
|
|
default_rules.html_inline = function (tokens, idx /*, options, env */) { |
|
|
rules.td_open = function (tokens, idx /*, options, env */) { |
|
|
return tokens[idx].content; |
|
|
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>'; }; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>'; }; |
|
|
// Self-contained token with no tag name, e.g. `inline` or hidden paragraph.
|
|
|
rules.s_close = function () { return '</s>'; }; |
|
|
//
|
|
|
|
|
|
// 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 */) { |
|
|
// Encode attributes, e.g. `<img src="foo"`
|
|
|
return options.xhtmlOut ? '<br />\n' : '<br>\n'; |
|
|
// ^^^^^^^^^^
|
|
|
}; |
|
|
if (token.attrs) { |
|
|
rules.softbreak = function (tokens, idx, options /*, env */) { |
|
|
for (i = 0, l = token.attrs.length; i < l; i++) { |
|
|
return options.breaks ? (options.xhtmlOut ? '<br />\n' : '<br>\n') : '\n'; |
|
|
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 */) { |
|
|
// Check if we need to add a newline after this tag
|
|
|
return escapeHtml(tokens[idx].content); |
|
|
//
|
|
|
}; |
|
|
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 */) { |
|
|
result += needLf ? '>\n' : '>'; |
|
|
return tokens[idx].content; |
|
|
return result; |
|
|
}; |
|
|
|
|
|
rules.html_inline = function (tokens, idx /*, options, env */) { |
|
|
|
|
|
return tokens[idx].content; |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -210,7 +200,7 @@ function Renderer() { |
|
|
* See [source code](https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.js)
|
|
|
* See [source code](https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.js)
|
|
|
* for more details and examples. |
|
|
* 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. |
|
|
* The same as [[Renderer.render]], but for single token of `inline` type. |
|
|
**/ |
|
|
**/ |
|
|
Renderer.prototype.renderInline = function (tokens, options, env) { |
|
|
Renderer.prototype.renderInline = function (tokens, options, env) { |
|
|
var result = '', |
|
|
var type, |
|
|
_rules = this.rules; |
|
|
result = '', |
|
|
|
|
|
rules = this.rules; |
|
|
|
|
|
|
|
|
for (var i = 0, len = tokens.length; i < len; i++) { |
|
|
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; |
|
|
return result; |
|
@ -246,13 +243,13 @@ Renderer.prototype.renderInline = function (tokens, options, env) { |
|
|
**/ |
|
|
**/ |
|
|
Renderer.prototype.renderInlineAsText = function (tokens, options, env) { |
|
|
Renderer.prototype.renderInlineAsText = function (tokens, options, env) { |
|
|
var result = '', |
|
|
var result = '', |
|
|
_rules = this.rules; |
|
|
rules = this.rules; |
|
|
|
|
|
|
|
|
for (var i = 0, len = tokens.length; i < len; i++) { |
|
|
for (var i = 0, len = tokens.length; i < len; i++) { |
|
|
if (tokens[i].type === 'text') { |
|
|
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') { |
|
|
} 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. |
|
|
* this method directly. |
|
|
**/ |
|
|
**/ |
|
|
Renderer.prototype.render = function (tokens, options, env) { |
|
|
Renderer.prototype.render = function (tokens, options, env) { |
|
|
var i, len, |
|
|
var i, len, type, |
|
|
result = '', |
|
|
result = '', |
|
|
_rules = this.rules; |
|
|
rules = this.rules; |
|
|
|
|
|
|
|
|
for (i = 0, len = tokens.length; i < len; i++) { |
|
|
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); |
|
|
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 { |
|
|
} else { |
|
|
result += _rules[tokens[i].type](tokens, i, options, env, this); |
|
|
result += rules.default_token(tokens, i, options, env); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|