mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-23 02:53:29 +00:00
Remove used blocks from templates
This commit is contained in:
+16
-6
@@ -80,18 +80,22 @@ var TemplateEngine = function(book) {
|
||||
// Process a block in a context
|
||||
TemplateEngine.prototype.processBlock = function(blk) {
|
||||
if (_.isString(blk)) blk = { body: blk };
|
||||
|
||||
blk = _.defaults(blk, {
|
||||
parse: false
|
||||
});
|
||||
blk.id = _.uniqueId("blk");
|
||||
|
||||
//Parsable block, just return it
|
||||
if (blk.parse) {
|
||||
return blk.body;
|
||||
}
|
||||
|
||||
// Add to global map
|
||||
this.blocks[blk.id] = blk;
|
||||
|
||||
// If don't parse id, return it as a macro
|
||||
if (!blk.parse) return "%+%"+blk.id+"%+%";
|
||||
|
||||
return blk.body;
|
||||
// Return it as a macro
|
||||
return "%+%"+blk.id+"%+%";
|
||||
};
|
||||
|
||||
// Replace blocks by body after processing
|
||||
@@ -101,8 +105,14 @@ TemplateEngine.prototype.replaceBlocks = function(content) {
|
||||
|
||||
return content.replace(/\%\+\%([\s\S]+?)\%\+\%/g, function(match, key) {
|
||||
var blk = that.blocks[key];
|
||||
if (!blk || blk.parse) return match;
|
||||
return blk.body;
|
||||
if (!blk) return match;
|
||||
|
||||
var body = blk.body;
|
||||
|
||||
// Remove it from map
|
||||
delete that.blocks[key];
|
||||
|
||||
return body;
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user