|
@ -1,3 +1,10 @@ |
|
|
|
|
|
/** |
|
|
|
|
|
* class Renderer |
|
|
|
|
|
* |
|
|
|
|
|
* Generates HTML from parsed token stream. Each instance has independent |
|
|
|
|
|
* copy of rules. Those can be rewritten with ease. Also, you can add new |
|
|
|
|
|
* rules if you create plugin and adds new token types. |
|
|
|
|
|
**/ |
|
|
'use strict'; |
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -324,13 +331,53 @@ rules.dd_close = function() { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Renderer class
|
|
|
/** |
|
|
|
|
|
* new Renderer() |
|
|
|
|
|
* |
|
|
|
|
|
* Creates new [[Renderer]] instance and fill [[Renderer#rules]] with defaults. |
|
|
|
|
|
**/ |
|
|
function Renderer() { |
|
|
function Renderer() { |
|
|
// Clone rules object to allow local modifications
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Renderer#rules -> Object |
|
|
|
|
|
* |
|
|
|
|
|
* Contains render rules for tokens. Can be updated and extended. |
|
|
|
|
|
* |
|
|
|
|
|
* ##### Example |
|
|
|
|
|
* |
|
|
|
|
|
* ```javascript
|
|
|
|
|
|
* var md = require('markdown-it')(); |
|
|
|
|
|
* |
|
|
|
|
|
* md.renderer.rules.strong_open = function () { return '<b>'; }; |
|
|
|
|
|
* md.renderer.rules.strong_close = function () { return '</b>'; }; |
|
|
|
|
|
* |
|
|
|
|
|
* var result = md.renderInline(...); |
|
|
|
|
|
* ``` |
|
|
|
|
|
* |
|
|
|
|
|
* Each rule is called as independed static function with fixed signature: |
|
|
|
|
|
* |
|
|
|
|
|
* ```javascript
|
|
|
|
|
|
* function my_token_render(tokens, idx, options, env, self) { |
|
|
|
|
|
* // ...
|
|
|
|
|
|
* return renderedHTML; |
|
|
|
|
|
* } |
|
|
|
|
|
* ``` |
|
|
|
|
|
* |
|
|
|
|
|
* 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({}, rules); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Renderer.renderInline(tokens, options, env) -> String |
|
|
|
|
|
* - tokens (Array): list on block tokens to renter |
|
|
|
|
|
* - options (Object): params of parser instance |
|
|
|
|
|
* - env (Object): additional data from parsed input (references, for example) |
|
|
|
|
|
* |
|
|
|
|
|
* 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 result = '', |
|
|
_rules = this.rules; |
|
|
_rules = this.rules; |
|
@ -343,6 +390,16 @@ Renderer.prototype.renderInline = function (tokens, options, env) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** internal |
|
|
|
|
|
* Renderer.renderInlineAsText(tokens, options, env) -> String |
|
|
|
|
|
* - tokens (Array): list on block tokens to renter |
|
|
|
|
|
* - options (Object): params of parser instance |
|
|
|
|
|
* - env (Object): additional data from parsed input (references, for example) |
|
|
|
|
|
* |
|
|
|
|
|
* Special kludge for image `alt` attributes to conform CommonMark spec. |
|
|
|
|
|
* Don't try to use it! Spec requires to show `alt` content with stripped markup, |
|
|
|
|
|
* instead of simple escaping. |
|
|
|
|
|
**/ |
|
|
Renderer.prototype.renderInlineAsText = function (tokens, options, env) { |
|
|
Renderer.prototype.renderInlineAsText = function (tokens, options, env) { |
|
|
var result = '', |
|
|
var result = '', |
|
|
_rules = this.rules; |
|
|
_rules = this.rules; |
|
@ -359,6 +416,15 @@ Renderer.prototype.renderInlineAsText = function (tokens, options, env) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Renderer.render(tokens, options, env) -> String |
|
|
|
|
|
* - tokens (Array): list on block tokens to renter |
|
|
|
|
|
* - options (Object): params of parser instance |
|
|
|
|
|
* - env (Object): additional data from parsed input (references, for example) |
|
|
|
|
|
* |
|
|
|
|
|
* Takes token stream and generates HTML. Probably, you will never need to call |
|
|
|
|
|
* this method directly. |
|
|
|
|
|
**/ |
|
|
Renderer.prototype.render = function (tokens, options, env) { |
|
|
Renderer.prototype.render = function (tokens, options, env) { |
|
|
var i, len, |
|
|
var i, len, |
|
|
result = '', |
|
|
result = '', |
|
|