2025-05-12 05:38:44 +09:00

47 lines
1.4 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const promise_1 = __importDefault(require("../utils/promise"));
/**
* 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, blocks) {
const newContent = content.replace(/\{\{\-\%([\s\S]+?)\%\-\}\}/g, (match, key) => {
let replacedWith = match;
const block = blocks.get(key);
if (block) {
replacedWith = replaceBlocks(block.get("body"), blocks);
}
return replacedWith;
});
return newContent;
}
/**
* Post render a template:
* - Execute "post" for blocks
* - Replace block content
*
* @param {TemplateEngine} engine
* @param {TemplateOutput} content
* @return {Promise<String>}
*/
function postRender(engine, output) {
const content = output.getContent();
const blocks = output.getBlocks();
const result = replaceBlocks(content, blocks);
return promise_1.default.forEach(blocks, (block) => {
const post = block.get("post");
if (!post) {
return;
}
return post();
}).thenResolve(result);
}
exports.default = postRender;