Browse Source

Renderer: token stream should stay immutable, close #260

pull/270/head
Vitaly Puzrin 8 years ago
parent
commit
b7c868b64b
  1. 27
      lib/renderer.js

27
lib/renderer.js

@ -32,11 +32,10 @@ default_rules.fence = function (tokens, idx, options, env, slf) {
var token = tokens[idx],
info = token.info ? unescapeAll(token.info).trim() : '',
langName = '',
highlighted;
highlighted, i, tmpAttrs, tmpToken;
if (info) {
langName = info.split(/\s+/g)[0];
token.attrJoin('class', options.langPrefix + langName);
}
if (options.highlight) {
@ -49,6 +48,30 @@ default_rules.fence = function (tokens, idx, options, env, slf) {
return highlighted + '\n';
}
// If language exists, inject class gently, without mudofying original token.
// May be, one day we will add .clone() for token and simplify this part, but
// now we prefer to keep things local.
if (info) {
i = token.attrIndex('class');
tmpAttrs = token.attrs ? token.attrs.slice() : [];
if (i < 0) {
tmpAttrs.push([ 'class', options.langPrefix + langName ]);
} else {
tmpAttrs[i] += ' ' + options.langPrefix + langName;
}
// Fake token just to render attributes
tmpToken = {
attrs: tmpAttrs
};
return '<pre><code' + slf.renderAttrs(tmpToken) + '>'
+ highlighted
+ '</code></pre>\n';
}
return '<pre><code' + slf.renderAttrs(token) + '>'
+ highlighted
+ '</code></pre>\n';

Loading…
Cancel
Save