UNPKG

@mdfriday/foundry

Version:

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

192 lines 4.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Lookup = void 0; exports.newLookup = newLookup; const baseof_1 = require("../vo/baseof"); /** * Lookup entity for finding templates and managing dependencies * TypeScript version of Go's Lookup struct */ class Lookup { constructor(baseOf, funcMap) { this.baseOf = baseOf || new baseof_1.BaseOf(); this.funcsv = new Map(); if (funcMap) { for (const [key, value] of funcMap) { this.funcsv.set(key, value); } } } /** * Find template with no dependencies (legacy method) */ findNoDependence(name, ns) { try { const tmpl = ns.lookup(name); if (tmpl) { return [tmpl, true, null]; } return [null, false, null]; } catch (error) { return [null, false, error]; } } /** * Find regular template with baseof dependency support */ findTemplate(name, templateNS) { try { // First try simple lookup const tmpl = templateNS.lookup(name); if (tmpl) { return [tmpl, true, null]; } // Try to find template with dependencies const templateState = templateNS.findTemplateWithDependencies(name); if (templateState && templateState.baseInfo) { // This template needs baseof processing - return the template // The actual baseof merging should be handled by the parser return [templateState.template, true, null]; } return [null, false, null]; } catch (error) { return [null, false, error]; } } /** * Find partial template (simple lookup, no dependencies) */ findPartial(name, partialNS) { try { const tmpl = partialNS.lookup(name); if (tmpl) { return [tmpl, true, null]; } return [null, false, null]; } catch (error) { return [null, false, error]; } } /** * Find shortcode template (simplest lookup) */ findShortcode(name, shortcodeNS) { try { const tmpl = shortcodeNS.getShortcode(name); if (tmpl) { return [tmpl, true, null]; } return [null, false, null]; } catch (error) { return [null, false, error]; } } /** * Find dependent template information */ findDependentInfo(name) { const overlay = this.baseOf.getNeedsBaseOf(name); if (!overlay) { return [null, null, false]; } let base = null; let found = false; for (const searchKey of this.baseOf.getTemplateSearchOrder(name)) { base = this.baseOf.getBaseOf(searchKey); if (base) { found = true; break; } } return [overlay, base, found]; } /** * Get function by name */ getFunc(name) { return this.funcsv.get(name); } /** * Set function */ setFunc(name, fn) { this.funcsv.set(name, fn); } /** * Set function map */ setFuncMap(funcMap) { this.funcsv.clear(); for (const [key, value] of funcMap) { this.funcsv.set(key, value); } } /** * Get function map */ getFuncMap() { return new Map(this.funcsv); } /** * Create template lookup function for namespace */ newTemplateLookup(ns) { return (name) => { return ns.findTemplate(name); }; } /** * Get base template manager */ getBaseOf() { return this.baseOf; } /** * Set base template manager */ setBaseOf(baseOf) { this.baseOf = baseOf; } /** * Check if template has dependencies */ hasDependencies(name) { const [overlay, base, found] = this.findDependentInfo(name); return found && (overlay !== null || base !== null); } /** * Get all available functions */ getAllFunctions() { return Array.from(this.funcsv.keys()); } /** * Clear all functions */ clearFunctions() { this.funcsv.clear(); } /** * Get lookup statistics */ getStats() { const baseOfStats = this.baseOf.getStats(); return { functionCount: this.funcsv.size, baseTemplateCount: baseOfStats.baseOfCount, dependentTemplateCount: baseOfStats.needsBaseOfCount, }; } } exports.Lookup = Lookup; /** * Create a new Lookup instance */ function newLookup(baseOf, funcMap) { return new Lookup(baseOf, funcMap); } //# sourceMappingURL=lookup.js.map