UNPKG

@mdfriday/foundry

Version:

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

246 lines 6.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PathFactoryUtils = exports.PathBuilder = exports.SimplePathPool = exports.DefaultPathFactory = exports.PathFactoryImpl = void 0; const pathparser_1 = require("../vo/pathparser"); const pathcomponents_1 = require("../vo/pathcomponents"); const path_1 = require("../entity/path"); /** * PathFactoryImpl implements the PathFactory interface * Provides methods for creating Path instances */ class PathFactoryImpl { constructor(config, pool) { const normalizer = config?.normalizer ? { normalize: config.normalizer } : new pathparser_1.BasicPathNormalizer(config?.normalize !== false, config?.replaceSpaces !== false); const extChecker = new pathparser_1.DefaultFileExtensionChecker(); this.processor = new pathparser_1.PathProcessorImpl(normalizer, extChecker); if (pool) { this.pool = pool; } } /** * Create a Path from component and path string */ create(component, path, config) { if (this.pool) { const pooledPath = this.pool.get(); // Reset and configure the pooled path // This would require path reset functionality } // For now, create new instances return this.processor.parse(component, path); } /** * Create a Path from PathComponents */ createFromComponents(components) { return new path_1.Path(components); } /** * Create multiple paths from an array of path strings */ createMany(component, paths, config) { return paths.map(path => this.create(component, path, config)); } /** * Create a path with custom configuration */ createWithConfig(component, path, normalizeConfig) { const config = {}; if (normalizeConfig?.toLowerCase !== undefined) { config.normalize = normalizeConfig.toLowerCase; } if (normalizeConfig?.replaceSpaces !== undefined) { config.replaceSpaces = normalizeConfig.replaceSpaces; } if (normalizeConfig?.customNormalizer !== undefined) { config.normalizer = normalizeConfig.customNormalizer; } return this.create(component, path, config); } } exports.PathFactoryImpl = PathFactoryImpl; /** * DefaultPathFactory provides a default configured path factory */ class DefaultPathFactory extends PathFactoryImpl { constructor() { super({ normalize: true, replaceSpaces: true }); } } exports.DefaultPathFactory = DefaultPathFactory; /** * SimplePathPool implements PathPool interface for basic object pooling */ class SimplePathPool { constructor(maxSize = 100) { this.pool = []; this.maxSize = maxSize; } get() { if (this.pool.length > 0) { return this.pool.pop(); } // Create new path with empty components const components = pathcomponents_1.PathComponentsFactory.createEmpty(); return new path_1.Path(components); } put(path) { if (this.pool.length < this.maxSize) { this.pool.push(path); } } /** * Clear all paths from the pool */ clear() { this.pool = []; } /** * Get current pool size */ size() { return this.pool.length; } } exports.SimplePathPool = SimplePathPool; /** * PathBuilder provides a fluent interface for building paths */ class PathBuilder { constructor(factory) { this.component = ''; this.path = ''; this.config = {}; this.factory = factory || new DefaultPathFactory(); } /** * Set the component */ withComponent(component) { this.component = component; return this; } /** * Set the path */ withPath(path) { this.path = path; return this; } /** * Enable/disable normalization */ withNormalization(normalize) { this.config.normalize = normalize; return this; } /** * Enable/disable space replacement */ withSpaceReplacement(replaceSpaces) { this.config.replaceSpaces = replaceSpaces; return this; } /** * Set custom normalizer */ withNormalizer(normalizer) { this.config.normalizer = normalizer; return this; } /** * Build the path */ build() { if (!this.component || !this.path) { throw new Error('Component and path must be set'); } return this.factory.create(this.component, this.path, this.config); } /** * Reset builder to initial state */ reset() { this.component = ''; this.path = ''; this.config = {}; return this; } } exports.PathBuilder = PathBuilder; /** * Static factory methods for common path creation scenarios */ class PathFactoryUtils { /** * Create a content path */ static createContentPath(path) { return PathFactoryUtils.defaultFactory.create('content', path); } /** * Create a static resource path */ static createStaticPath(path) { return PathFactoryUtils.defaultFactory.create('static', path); } /** * Create a layout path */ static createLayoutPath(path) { return PathFactoryUtils.defaultFactory.create('layouts', path); } /** * Create an archetype path */ static createArchetypePath(path) { return PathFactoryUtils.defaultFactory.create('archetypes', path); } /** * Create a data path */ static createDataPath(path) { return PathFactoryUtils.defaultFactory.create('data', path); } /** * Create a theme path */ static createThemePath(path) { return PathFactoryUtils.defaultFactory.create('themes', path); } /** * Create paths from a configuration object */ static createFromConfig(config) { const factoryConfig = {}; if (config.normalize !== undefined) { factoryConfig.normalize = config.normalize; } if (config.replaceSpaces !== undefined) { factoryConfig.replaceSpaces = config.replaceSpaces; } const factory = new PathFactoryImpl(factoryConfig); return config.paths.map(path => factory.create(config.component, path)); } /** * Get a path builder instance */ static builder() { return new PathBuilder(); } /** * Create a path with pooling */ static createWithPool(component, path, pool) { const factory = new PathFactoryImpl(undefined, pool); return factory.create(component, path); } } exports.PathFactoryUtils = PathFactoryUtils; PathFactoryUtils.defaultFactory = new DefaultPathFactory(); //# sourceMappingURL=pathfactory.js.map