Browse Source

Add shallow cloning capabilities to Tokens to preserve the AST after rendering

pull/259/head
Christopher Breeden 9 years ago
parent
commit
0ccc687367
  1. 5
      lib/renderer.js
  2. 24
      lib/token.js

5
lib/renderer.js

@ -29,7 +29,10 @@ default_rules.code_block = function (tokens, idx /*, options, env */) {
default_rules.fence = function (tokens, idx, options, env, slf) {
var token = tokens[idx],
// We will make modifications of the attributes object of this
// token while processing, so we obtain a shallow clone as to
// prevent modifying the token in the token list.
var token = tokens[idx].clone(),
info = token.info ? unescapeAll(token.info).trim() : '',
langName = '',
highlighted;

24
lib/token.js

@ -194,4 +194,28 @@ Token.prototype.attrJoin = function attrJoin(name, value) {
};
/**
* Token.clone()
*
* Obtain a shallow clone of the token. You can use this while rendering to
* prevent modifying the token list while rendering.
*/
Token.prototype.clone = function clone() {
var token = new Token(this.type, this.tag, this.nesting);
token.attrs = this.attrs;
token.level = this.level;
token.children = this.children;
token.content = this.content;
token.map = this.map;
token.markup = this.markup;
token.info = this.info;
token.meta = this.meta;
token.block = this.block;
token.hidden = this.hidden;
return token;
};
module.exports = Token;

Loading…
Cancel
Save