Browse Source

provided support for ReactJS components

Using map and reduce
pull/152/head
Jason McCarrell 10 years ago
parent
commit
ecb4348aad
  1. 12
      lib/index.js
  2. 43
      lib/renderer.js

12
lib/index.js

@ -57,7 +57,7 @@ function normalizeLink(url) {
if (!parsed.protocol || RECODE_HOSTNAME_FOR.indexOf(parsed.protocol) >= 0) {
try {
parsed.hostname = punycode.toASCII(parsed.hostname);
} catch (er) { /**/ }
} catch(er) {}
}
}
@ -77,7 +77,7 @@ function normalizeLinkText(url) {
if (!parsed.protocol || RECODE_HOSTNAME_FOR.indexOf(parsed.protocol) >= 0) {
try {
parsed.hostname = punycode.toUnicode(parsed.hostname);
} catch (er) { /**/ }
} catch(er) {}
}
}
@ -483,7 +483,7 @@ MarkdownIt.prototype.use = function (plugin /*, params, ... */) {
* AST).
*
* `env` is used to pass data between "distributed" rules and return additional
* metadata like reference info, needed for the renderer. It also can be used to
* metadata like reference info, needed for for renderer. It also can be used to
* inject data in specific cases. Usually, you will be ok to pass `{}`,
* and then pass updated object to renderer.
**/
@ -513,6 +513,12 @@ MarkdownIt.prototype.render = function (src, env) {
return this.renderer.render(this.parse(src, env), this.options, env);
};
MarkdownIt.prototype.renderTokens = function (src, env) {
env = env || {};
return this.renderer.renderTokens(this.parse(src, env), this.options, env);
};
/** internal
* MarkdownIt.parseInline(src, env) -> Array

43
lib/renderer.js

@ -28,7 +28,7 @@ default_rules.code_block = function (tokens, idx /*, options, env */) {
};
default_rules.fence = function (tokens, idx, options, env, slf) {
default_rules.fence = function (tokens, idx, options, env, self) {
var token = tokens[idx],
info = token.info ? unescapeAll(token.info).trim() : '',
langName = '',
@ -45,13 +45,13 @@ default_rules.fence = function (tokens, idx, options, env, slf) {
highlighted = escapeHtml(token.content);
}
return '<pre><code' + slf.renderAttrs(token) + '>'
return '<pre><code' + self.renderAttrs(token) + '>'
+ highlighted
+ '</code></pre>\n';
};
default_rules.image = function (tokens, idx, options, env, slf) {
default_rules.image = function (tokens, idx, options, env, self) {
var token = tokens[idx];
// "alt" attr MUST be set, even if empty. Because it's mandatory and
@ -60,9 +60,9 @@ default_rules.image = function (tokens, idx, options, env, slf) {
// Replace content with actual value
token.attrs[token.attrIndex('alt')][1] =
slf.renderInlineAsText(token.children, options, env);
self.renderInlineAsText(token.children, options, env);
return slf.renderToken(tokens, idx, options);
return self.renderToken(tokens, idx, options);
};
@ -228,6 +228,16 @@ Renderer.prototype.renderInline = function (tokens, options, env) {
var type,
result = '',
rules = this.rules;
return tokens.map(function(token, i) {
type = token.type;
if (typeof rules[type] !== 'undefined') {
return rules[type](tokens, i, options, env, this);
} else {
return this.renderToken(tokens, i, options);
}
}, this);
for (var i = 0, len = tokens.length; i < len; i++) {
type = tokens[i].type;
@ -279,23 +289,30 @@ Renderer.prototype.renderInlineAsText = function (tokens, options, env) {
* this method directly.
**/
Renderer.prototype.render = function (tokens, options, env) {
return this.renderTokens(tokens, options, env).reduce(function(collector, token) {
return collector + token;
}, '');
};
Renderer.prototype.renderTokens = function (tokens, options, env) {
var i, len, type,
result = '',
rules = this.rules;
for (i = 0, len = tokens.length; i < len; i++) {
type = tokens[i].type;
return tokens.reduce(function(collector, token, i) {
type = token.type;
if (type === 'inline') {
result += this.renderInline(tokens[i].children, options, env);
console.log('inline: ', this.renderInline(token.children, options, env));
return collector.concat(this.renderInline(token.children, options, env));
} else if (typeof rules[type] !== 'undefined') {
result += rules[tokens[i].type](tokens, i, options, env, this);
collector.push(rules[type](tokens, i, options, env, this));
return collector;
} else {
result += this.renderToken(tokens, i, options, env);
collector.push(this.renderToken(tokens, i, options, env));
return collector;
}
}
return result;
}.bind(this), []);
};
module.exports = Renderer;

Loading…
Cancel
Save