diff --git a/lib/renderer.js b/lib/renderer.js index 0da9ad8..34628ed 100644 --- a/lib/renderer.js +++ b/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 '
'
+          + highlighted
+          + '
\n'; + } + + return '
'
         + highlighted
         + '
\n';