summaryrefslogtreecommitdiffstats
path: root/lib/templating/replaceBlocks.js
blob: 4b1c37f925784f0f90fd8317509c0ff7b247d0fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var Immutable = require('immutable');
var TemplateBlock = require('../models/templateBlock');

/**
    Replace position markers of blocks by body after processing
    This is done to avoid that markdown/asciidoc processer parse the block content

    @param {String} content
    @return {Object} {blocks: Set, content: String}
*/
function replaceBlocks(content) {
    var blockTypes = new Immutable.Set();
    var newContent = content.replace(/\{\{\-\%([\s\S]+?)\%\-\}\}/g, function(match, key) {
        var replacedWith = match;

        var block = TemplateBlock.getBlockResultByKey(key);
        if (block) {
            var result = replaceBlocks(block.body);

            blockTypes = blockTypes.add(block.name);
            blockTypes = blockTypes.concat(result.blocks);
            replacedWith = result.content;
        }

        return replacedWith;
    });

    return {
        content: newContent,
        blocks: blockTypes
    };
}

module.exports = replaceBlocks;