UNPKG

auto-cms-server

Version:

Auto turn any webpage into editable CMS without coding.

44 lines (43 loc) 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyTemplates = applyTemplates; const fs_1 = require("fs"); const path_1 = require("path"); const i18n_1 = require("./i18n"); function applyTemplates(options) { let { site_dir, html, lang } = options; let dir = (0, path_1.dirname)(options.file); for (;;) { let found = false; let matches = html.match(/{\[(.*?)\]}/g) || []; for (let key of matches) { // e.g. '{[/header.html]}' // e.g. '{[header.html]}' // e.g. '{[_.KEY]:value, arr: []}' let pathname = key.slice(2, -2); if (pathname.includes(']')) { continue; } let file = pathname[0] == '/' ? (0, path_1.resolve)((0, path_1.join)(site_dir, pathname)) : (0, path_1.resolve)((0, path_1.join)(dir, pathname)); if (!file.startsWith(site_dir)) { html = html.replace(key, ''); continue; } let template = (0, fs_1.readFileSync)(file).toString(); if (lang) { template = (0, i18n_1.translateHTML)({ html: template, file: file + i18n_1.LangFileSuffix, lang: lang, }); } html = html.replace(key, template); found = true; } if (!found) break; } return html; }