UNPKG

@ng-doc/builder

Version:

<!-- PROJECT LOGO --> <br /> <div align="center"> <a href="https://github.com/ng-doc/ng-doc"> <img src="https://ng-doc.com/assets/images/ng-doc.svg?raw=true" alt="Logo" height="150px"> </a>

61 lines 2.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.renderTemplate = renderTemplate; exports.renderTemplateString = renderTemplateString; const tslib_1 = require("tslib"); const core_1 = require("@ng-doc/core"); const fs = tslib_1.__importStar(require("fs")); const nunjucks_1 = require("nunjucks"); const path = tslib_1.__importStar(require("path")); const ts_morph_1 = require("ts-morph"); const variables_1 = require("../variables"); const extentions_1 = require("./extentions"); const filters = tslib_1.__importStar(require("./filters")); class NgDocRelativeLoader { constructor(path, dependenciesStore) { this.path = path; this.dependenciesStore = dependenciesStore; } getSource(name) { const fullPath = path.join(this.path, name); this.dependenciesStore?.add(fullPath); return { src: fs.readFileSync(fullPath, 'utf-8'), path: fullPath, noCache: true, }; } } /** * * @param scope * @param dependencies */ function getEnvironment(scope, dependencies) { let environment = new nunjucks_1.Environment(new NgDocRelativeLoader(scope, dependencies), { autoescape: false, }); (0, core_1.objectKeys)(filters).forEach((filter) => (environment = environment.addFilter(filter, filters[filter]))); environment.addGlobal('Node', ts_morph_1.Node); environment.addExtension('NgDocIndexExtension', new extentions_1.NgDocIndexExtension()); return environment; } /** * Renders a template with the given options via Nunjucks. * @param template - The template path to render. * @param options - The options to render the template with. */ function renderTemplate(template, options) { const environment = getEnvironment(options?.scope ?? variables_1.TEMPLATES_PATH, options?.dependencies); return environment.render(template, options?.context); } /** * Renders a template string with the given options via Nunjucks. * @param template - The template string to render. * @param options - The options to render the template with. */ function renderTemplateString(template, options) { const environment = getEnvironment(options?.scope ?? variables_1.TEMPLATES_PATH, options?.dependencies); return environment.renderString(template, options?.context ?? {}); } //# sourceMappingURL=render-template.js.map