42 lines
1.6 KiB
JavaScript
42 lines
1.6 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"));
|
|
const timing_1 = __importDefault(require("../utils/timing"));
|
|
const templateOutput_1 = __importDefault(require("../models/templateOutput"));
|
|
const replaceShortcuts_1 = __importDefault(require("./replaceShortcuts"));
|
|
/**
|
|
* Render a template
|
|
*
|
|
* @param {TemplateEngine} engine
|
|
* @param {string} filePath: absolute path for the loader
|
|
* @param {string} content
|
|
* @param {Object} context (optional)
|
|
* @return {Promise<TemplateOutput>}
|
|
*/
|
|
function renderTemplate(engine, filePath, content, context) {
|
|
context = context || {};
|
|
// Mutable objects to contains all blocks requiring post-processing
|
|
const blocks = {};
|
|
// Create nunjucks environment
|
|
const env = engine.toNunjucks(blocks);
|
|
// Replace shortcuts from plugin's blocks
|
|
content = (0, replaceShortcuts_1.default)(engine.getBlocks(), filePath, content);
|
|
return timing_1.default.measure("template.render", promise_1.default.nfcall(env.renderString.bind(env), content, context, {
|
|
path: filePath
|
|
})
|
|
.then((content) => {
|
|
return templateOutput_1.default.create(content, blocks);
|
|
})
|
|
.catch((error) => {
|
|
console.log("env:", env);
|
|
console.log("context:", context);
|
|
console.log("content:", content);
|
|
console.error("rendering error:", error);
|
|
return promise_1.default.reject(error);
|
|
}));
|
|
}
|
|
exports.default = renderTemplate;
|