UNPKG

honkit

Version:

HonKit is building beautiful books using Markdown.

135 lines (134 loc) 5.55 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); const nunjucks_1 = __importDefault(require("nunjucks")); const nunjucks_do_1 = __importDefault(require("nunjucks-do")); const DoExtension = (0, nunjucks_do_1.default)(nunjucks_1.default); const memoize_one_1 = __importDefault(require("memoize-one")); const api_1 = __importDefault(require("../../api")); const deprecate_1 = __importDefault(require("../../api/deprecate")); const json_1 = __importDefault(require("../../json")); const location_1 = __importDefault(require("../../utils/location")); const fs_1 = __importDefault(require("../../utils/fs")); const path_2 = __importDefault(require("../../utils/path")); const templateEngine_1 = __importDefault(require("../../models/templateEngine")); const templatesFolder_1 = __importDefault(require("../../constants/templatesFolder")); const defaultFilters_1 = __importDefault(require("../../constants/defaultFilters")); const templating_1 = __importDefault(require("../../templating")); const listSearchPaths_1 = __importDefault(require("./listSearchPaths")); const fileToURL_1 = __importDefault(require("../helper/fileToURL")); const resolveFileToURL_1 = __importDefault(require("../helper/resolveFileToURL")); /** * Directory for a theme with the templates */ function templateFolder(dir) { return path_1.default.join(dir, templatesFolder_1.default); } /** * Create templating engine to render themes * * @param {Output} output * @param {string} currentFile * @return {TemplateEngine} */ function createTemplateEngine(output, currentFile) { const book = output.getBook(); const state = output.getState(); const i18n = state.getI18n(); const config = book.getConfig(); const summary = book.getSummary(); const outputFolder = output.getRoot(); // Search paths for templates const searchPaths = (0, listSearchPaths_1.default)(output); const tplSearchPaths = searchPaths.map(templateFolder); // Create loader // @ts-expect-error: Expected 0 arguments, but got 1. const loader = new templating_1.default.ThemesLoader(tplSearchPaths); // Get languages const language = config.getValue("language"); // Create API context const context = api_1.default.encodeGlobal(output); /** * Check if a file exists * @param {string} fileName * @return {boolean} */ function fileExists(fileName) { if (!fileName) { return false; } // @ts-expect-error ts-migrate(2554) FIXME: Expected 1 arguments, but got 2. const filePath = path_2.default.resolveInRoot(outputFolder, fileName); return fs_1.default.existsSync(filePath); } /** * Return an article by its path * @param {string} filePath * @return {Object|undefined} */ function getArticleByPath(filePath) { const article = summary.getByPath(filePath); if (!article) return undefined; return json_1.default.encodeSummaryArticle(article); } /** * Return a page by its path * @param {string} filePath * @return {Object|undefined} */ function getPageByPath(filePath) { const page = output.getPage(filePath); if (!page) return undefined; return json_1.default.encodePage(page, summary); } return templateEngine_1.default.create({ loader: loader, context: context, globals: { getArticleByPath: (0, memoize_one_1.default)(getArticleByPath), getPageByPath: (0, memoize_one_1.default)(getPageByPath), fileExists: fileExists }, filters: defaultFilters_1.default.merge({ /** * Translate a sentence */ t: function t(s) { return i18n.t(language, s); }, /** * Resolve an absolute file path into a * relative path. * it also resolve pages */ resolveFile: function (filePath) { filePath = (0, resolveFileToURL_1.default)(output, filePath); return location_1.default.relativeForFile(currentFile, filePath); }, resolveAsset: function (filePath) { filePath = location_1.default.toAbsolute(filePath, "", ""); filePath = path_1.default.join("gitbook", filePath); filePath = location_1.default.relativeForFile(currentFile, filePath); // Use assets from parent if language book if (book.isLanguageBook()) { filePath = path_1.default.join("../", filePath); } return location_1.default.normalize(filePath); }, fileExists: deprecate_1.default.method(book, "fileExists", fileExists, 'Filter "fileExists" is deprecated, use "fileExists(filename)" '), getArticleByPath: deprecate_1.default.method(book, "getArticleByPath", fileExists, 'Filter "getArticleByPath" is deprecated, use "getArticleByPath(filename)" '), contentURL: function (filePath) { return (0, fileToURL_1.default)(output, filePath); } }), extensions: { DoExtension: new DoExtension() } }); } exports.default = createTemplateEngine;