UNPKG

@mdfriday/foundry

Version:

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

120 lines 4.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Section = exports.PAGE_HOME_BASE = void 0; const pagetrees_1 = require("./pagetrees"); const fileinfo_1 = require("../vo/fileinfo"); const pagesource_1 = require("./pagesource"); const kind_1 = require("../vo/kind"); const doctree_1 = require("../../../../pkg/doctree"); const log_1 = require("../../../../pkg/log"); // Create a domain-specific logger for content operations const log = (0, log_1.getDomainLogger)('content', { component: 'section' }); /** * Constants - exact replica of Go's constants */ exports.PAGE_HOME_BASE = "/"; /** * Section class - exact replica of Go's Section struct */ class Section { constructor(fsSvc) { this.home = null; this.seen = {}; this.fsSvc = fsSvc; } /** * IsHome - exact replica of Go's isHome method */ isHome(key) { return key === ""; } /** * IsSectionExist - exact replica of Go's isSectionExist method */ isSectionExist(section) { if (section === "" || this.seen[section]) { return true; } this.seen[section] = true; return false; } /** * Assemble - exact replica of Go's Assemble method */ async assemble(pages, pb, langIdx) { this.seen = {}; const lockType = doctree_1.LockType.LockTypeWrite; const walker = new doctree_1.NodeShiftTreeWalker({ tree: pages, lockType: lockType, handle: async (k, n, match) => { if (!n) { throw new Error("n is null"); } const [ps, found] = n.getPage(); if (!found) { return [false, null]; } if (this.isHome(k)) { this.home = ps; return [false, null]; } const kind = ps.kind(); if (kind !== (0, kind_1.getKindMain)("page") && kind !== (0, kind_1.getKindMain)("section")) { // Skip taxonomy nodes etc. return [false, null]; } const p = ps.paths(); const sections = p.sections(); for (const section of sections) { if (this.isSectionExist(section)) { continue; } // Try to preserve the original casing if possible. try { const fmi = this.fsSvc.newFileMetaInfo("/" + section + "/_index.md"); const f = (0, fileinfo_1.newFileInfo)(fmi); const sectionSource = (0, pagesource_1.newPageSource)(f); const sectionBase = sectionSource.paths().base(); const nn = walker.tree.get(sectionBase); if (!nn) { const sectionPage = await pb.withSource(sectionSource).withLangIdx(langIdx).kindBuild(); walker.tree.insertIntoValuesDimension(sectionBase, (0, pagetrees_1.newPageTreesNode)(sectionPage)); } } catch (error) { return [false, error]; } } return [false, null]; }, }); // Execute the walker try { await walker.walk(); } catch (error) { log.error('Error walking pages:', error); throw error; } if (!this.home) { await this.createHome(pb); if (this.home) { const homePage = this.home; pages.insertWithLock(homePage.paths().base(), (0, pagetrees_1.newPageTreesNode)(homePage)); } } } /** * CreateHome - exact replica of Go's CreateHome method */ async createHome(pb) { const fmi = this.fsSvc.newFileMetaInfo("/_index.md"); const f = (0, fileinfo_1.newFileInfo)(fmi); const homeSource = (0, pagesource_1.newPageSource)(f); const homePage = await pb.withSource(homeSource).kindBuild(); this.home = homePage; } } exports.Section = Section; //# sourceMappingURL=section.js.map