From 4772e435ab2de5bb866ea1c412aadd6ed6af8748 Mon Sep 17 00:00:00 2001 From: Andreas Haller Date: Thu, 19 Sep 2024 16:28:19 +0200 Subject: [PATCH] improve developers content toc --- .../developers-content/developers-content.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/blocks/developers-content/developers-content.js b/blocks/developers-content/developers-content.js index 564758bd..6624e081 100644 --- a/blocks/developers-content/developers-content.js +++ b/blocks/developers-content/developers-content.js @@ -1,6 +1,6 @@ import ComponentBase from '../../scripts/component-base.js'; -const sitePathPrefix = 'developers'; +const sitePathPrefix = window.location.hostname === 'docs.raqn.io' ? '/developers' : ''; export default class DeveloperToc extends ComponentBase { static loaderConfig = { @@ -35,11 +35,11 @@ export default class DeveloperToc extends ComponentBase { if(window.location.host.startsWith('localhost') || window.location.host.search(/\.aem\.(page|live)/) > 0) { return path; } - return `/${sitePathPrefix}${path}`; + return `${sitePathPrefix}${path}`; } async loadPageHierarchy() { - const response = await fetch(`/${sitePathPrefix}/query-index.json`); + const response = await fetch(`${sitePathPrefix}/query-index.json`); if(!response.ok) return []; const json = await response.json(); @@ -57,7 +57,7 @@ export default class DeveloperToc extends ComponentBase { node = { nodePath, segment, - active: window.location.pathname.startsWith(`/${sitePathPrefix}${nodePath}`), + active: window.location.pathname.startsWith(`${sitePathPrefix}${nodePath}`), children: [], }; if(nodePath === page.path) { @@ -130,6 +130,13 @@ export default class DeveloperToc extends ComponentBase { return li.outerHTML; } + findRepositoryRoot(node){ + if(node.children.length === 1 && !node.children[0].page) { + return this.findRepositoryRoot(node.children[0]); + } + return node; + } + async generateTablesOfContent() { const [pageHierarchy, currentNode] = await this.loadPageHierarchy(); const currentOrg = pageHierarchy.find((org) => org.active); @@ -141,8 +148,9 @@ export default class DeveloperToc extends ComponentBase { if(currentRepository && currentNode) { const h2 = document.createElement('h2'); h2.innerText = `${currentOrg.segment} - ${currentProject.segment} - ${currentRepository.segment}`; + const root = this.findRepositoryRoot(currentRepository); tocs += `
${h2.outerHTML} -
`; + `; } this.innerHTML = tocs;