conflutora
Version:
Provider to convert and publish Antora documentation on Confluence
75 lines • 2.78 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SiteStructure = void 0;
const Logger_1 = require("../utils/Logger");
const Page_1 = require("./Page");
class SiteStructure {
logger = new Logger_1.Logger(this.constructor.name);
pages;
hierarchy;
constructor() {
this.pages = new Map();
this.hierarchy = new Map();
}
getRootPage() {
for (const [key, moduleHierarchy] of this.hierarchy) {
if (key.split('/').length === 1) {
return this.pages.get(moduleHierarchy.index);
}
}
return undefined;
}
addPage(page, parentId) {
this.pages.set(page.id, page);
if (parentId) {
if (!this.hierarchy.has(parentId)) {
this.hierarchy.set(parentId, { children: [] });
}
const parent = this.hierarchy.get(parentId);
if (!parent) {
this.logger.warn('Parent not found');
return;
}
if (page.isIndex()) {
parent.index = page.id;
}
else {
parent.children.push(page.id);
}
}
}
getParentPage(page) {
if (page.isIndex()) {
const parent = page.parent.split('/').slice(-2, -1)[0];
return this.hierarchy.has(parent) ? this.pages.get(this.hierarchy.get(parent).index) : undefined;
}
for (const [, moduleHierarchy] of this.hierarchy.entries()) {
if (moduleHierarchy.children.includes(page.id)) {
return this.pages.get(moduleHierarchy.index);
}
}
return undefined;
}
getAllPages() {
const sortedPages = [];
// Loop over the hierarchy, sort by the shortest key (counting by number of "/") representing the tree level.
// Then foe each of them, add the index page first if index is defined, then all it's children.
const sortedHierarchy = Array.from(this.hierarchy.entries()).sort((a, b) => a[0].split('/').length - b[0].split('/').length);
sortedHierarchy.forEach(([parent, moduleHierarchy]) => {
if (moduleHierarchy.index) {
sortedPages.push(this.pages.get(moduleHierarchy.index));
}
else {
// Create a page for the parent directory
const parentPage = new Page_1.Page(null, '', parent, '');
sortedPages.push(parentPage);
}
moduleHierarchy.children.forEach(childId => {
sortedPages.push(this.pages.get(childId));
});
});
return sortedPages;
}
}
exports.SiteStructure = SiteStructure;
//# sourceMappingURL=SiteStructure.js.map