UNPKG

@mdfriday/foundry

Version:

The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.

204 lines 7 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigLoader = void 0; const provider_1 = require("./provider"); const path = __importStar(require("path")); const NO_CONFIG_FILE_ERR_INFO = "Unable to locate config file or config directory."; /** * ConfigLoader loads and assembles configuration */ class ConfigLoader { constructor(sourceDescriptor, baseDirs) { this.cfg = (0, provider_1.newDefaultProvider)(); this.sourceDescriptor = sourceDescriptor; this.baseDirs = baseDirs; } /** * Load configuration by default */ async loadConfigByDefault() { const filename = this.sourceDescriptor.filename(); await this.loadProvider(filename); this.applyDefaultConfig(); this.cfg.setDefaultMergeStrategy(); if (!this.cfg.isSet('languages')) { // We need at least one const lang = this.cfg.getString('defaultContentLanguage'); this.cfg.set('languages', { [lang]: {} }); } return this.cfg; } /** * Delete merge strategies */ deleteMergeStrategies() { this.cfg.walkParams((params) => { // In TypeScript, we don't need complex merge strategy deletion // Keep this method for interface compatibility return false; }); } /** * Load provider from config file */ async loadProvider(configName) { const baseDir = this.baseDirs.workingDir; let baseFilename; if (path.isAbsolute(configName)) { baseFilename = configName; } else { baseFilename = path.join(baseDir, configName); } let filename = ''; if (path.extname(configName) !== '') { const exists = await this.fileExists(baseFilename); if (exists) { filename = baseFilename; } } if (filename === '') { throw new Error(NO_CONFIG_FILE_ERR_INFO); } const configData = await this.loadConfigFromFile(filename); // Set overwrites keys of the same name, recursively this.cfg.set('', configData); } /** * Apply default configuration settings */ applyDefaultConfig() { const defaultSettings = { baseURL: '', cleanDestinationDir: false, watch: false, contentDir: 'content', resourceDir: 'resources', publishDir: 'public', publishDirOrig: 'public', themesDir: 'themes', assetDir: 'assets', layoutDir: 'layouts', i18nDir: 'i18n', dataDir: 'data', archetypeDir: 'archetypes', configDir: 'config', staticDir: 'static', buildDrafts: false, buildFuture: false, buildExpired: false, params: {}, environment: 'production', uglyURLs: false, verbose: false, ignoreCache: false, canonifyURLs: false, relativeURLs: false, removePathAccents: false, titleCaseStyle: 'AP', taxonomies: { tag: 'tags', category: 'categories' }, permalinks: {}, sitemap: { priority: -1, filename: 'sitemap.xml' }, menus: {}, disableLiveReload: false, pluralizeListTitles: true, capitalizeListTitles: true, forceSyncStatic: false, footnoteAnchorPrefix: '', footnoteReturnLinkContents: '', newContentEditor: '', paginate: 10, paginatePath: 'page', summaryLength: 70, rssLimit: -1, sectionPagesMenu: '', disablePathToLower: false, hasCJKLanguage: false, enableEmoji: false, defaultContentLanguage: 'en', defaultContentLanguageInSubdir: false, enableMissingTranslationPlaceholders: false, enableGitInfo: false, ignoreFiles: [], disableAliases: false, debug: false, disableFastRender: false, timeout: '30s', timeZone: '', enableInlineShortcodes: false }; this.cfg.setDefaults(defaultSettings); } /** * Check if file exists */ async fileExists(filename) { try { const fs = this.sourceDescriptor.fs(); const stat = await fs.stat(filename); return !stat.isDir(); // If it's not a directory, assume it's a file } catch { return false; } } /** * Load configuration from file */ async loadConfigFromFile(filename) { const fs = this.sourceDescriptor.fs(); const file = await fs.open(filename); try { // Read file content using the File interface const buffer = new Uint8Array(1024 * 1024); // 1MB buffer const { bytesRead } = await file.read(buffer); const content = new TextDecoder().decode(buffer.slice(0, bytesRead)); // Simple JSON parsing - in real implementation, you'd use a proper TOML parser try { return JSON.parse(content); } catch { // If JSON parsing fails, return empty object return {}; } } finally { await file.close(); } } } exports.ConfigLoader = ConfigLoader; //# sourceMappingURL=loader.js.map