-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathrenderer.js
63 lines (53 loc) · 1.35 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import * as utils from './common/utils';
import rules from './rules';
/**
* Renderer class. Renders HTML and exposes `rules` to allow
* local modifications.
*/
export default function Renderer() {
this.rules = utils.assign({}, rules);
// exported helper, for custom rules only
this.getBreak = rules.getBreak;
}
/**
* Render a string of inline HTML with the given `tokens` and
* `options`.
*
* @param {Array} `tokens`
* @param {Object} `options`
* @param {Object} `env`
* @return {String}
* @api public
*/
Renderer.prototype.renderInline = function (tokens, options, env) {
var _rules = this.rules;
var len = tokens.length, i = 0;
var result = '';
while (len--) {
result += _rules[tokens[i].type](tokens, i++, options, env, this);
}
return result;
};
/**
* Render a string of HTML with the given `tokens` and
* `options`.
*
* @param {Array} `tokens`
* @param {Object} `options`
* @param {Object} `env`
* @return {String}
* @api public
*/
Renderer.prototype.render = function (tokens, options, env) {
var _rules = this.rules;
var len = tokens.length, i = -1;
var result = '';
while (++i < len) {
if (tokens[i].type === 'inline') {
result += this.renderInline(tokens[i].children, options, env);
} else {
result += _rules[tokens[i].type](tokens, i, options, env, this);
}
}
return result;
};