@uniweb/core 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/website.js +61 -3
package/package.json
CHANGED
package/src/website.js
CHANGED
|
@@ -49,6 +49,9 @@ export default class Website {
|
|
|
49
49
|
new Page(page, index, this, this.headerPage, this.footerPage, this.leftPage, this.rightPage)
|
|
50
50
|
)
|
|
51
51
|
|
|
52
|
+
// Build parent-child relationships based on route structure
|
|
53
|
+
this.buildPageHierarchy()
|
|
54
|
+
|
|
52
55
|
this.activePage =
|
|
53
56
|
this.pages.find((page) => page.route === '/' || page.route === '/index') ||
|
|
54
57
|
this.pages[0]
|
|
@@ -98,6 +101,53 @@ export default class Website {
|
|
|
98
101
|
}))
|
|
99
102
|
}
|
|
100
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Build parent-child relationships between pages based on route structure
|
|
106
|
+
* E.g., /getting-started/installation is a child of /getting-started
|
|
107
|
+
* @private
|
|
108
|
+
*/
|
|
109
|
+
buildPageHierarchy() {
|
|
110
|
+
// Sort pages by route depth (parents before children)
|
|
111
|
+
const sortedPages = [...this.pages].sort((a, b) => {
|
|
112
|
+
const depthA = (a.route.match(/\//g) || []).length
|
|
113
|
+
const depthB = (b.route.match(/\//g) || []).length
|
|
114
|
+
return depthA - depthB
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
// Build a map of route to page for quick lookup
|
|
118
|
+
const pageMap = new Map()
|
|
119
|
+
for (const page of sortedPages) {
|
|
120
|
+
pageMap.set(page.route, page)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// For each page, find its parent and add it as a child
|
|
124
|
+
for (const page of sortedPages) {
|
|
125
|
+
const route = page.route
|
|
126
|
+
if (route === '/' || route === '') continue
|
|
127
|
+
|
|
128
|
+
// Find parent route by removing the last segment
|
|
129
|
+
// /getting-started/installation -> /getting-started
|
|
130
|
+
const segments = route.split('/').filter(Boolean)
|
|
131
|
+
if (segments.length <= 1) continue // Root-level pages have no parent
|
|
132
|
+
|
|
133
|
+
// Build parent route
|
|
134
|
+
const parentRoute = '/' + segments.slice(0, -1).join('/')
|
|
135
|
+
const parent = pageMap.get(parentRoute)
|
|
136
|
+
|
|
137
|
+
if (parent) {
|
|
138
|
+
parent.children.push(page)
|
|
139
|
+
page.parent = parent
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Sort children by order
|
|
144
|
+
for (const page of this.pages) {
|
|
145
|
+
if (page.children.length > 0) {
|
|
146
|
+
page.children.sort((a, b) => (a.order || 0) - (b.order || 0))
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
101
151
|
/**
|
|
102
152
|
* Get page by route
|
|
103
153
|
* @param {string} route
|
|
@@ -391,7 +441,7 @@ export default class Website {
|
|
|
391
441
|
} = options
|
|
392
442
|
|
|
393
443
|
// Filter pages based on navigation type and visibility
|
|
394
|
-
|
|
444
|
+
const isPageVisible = (page) => {
|
|
395
445
|
// Always exclude special pages (header/footer are already separated)
|
|
396
446
|
if (page.route.startsWith('/@')) return false
|
|
397
447
|
|
|
@@ -406,7 +456,15 @@ export default class Website {
|
|
|
406
456
|
if (customFilter && !customFilter(page)) return false
|
|
407
457
|
|
|
408
458
|
return true
|
|
409
|
-
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
let filteredPages = this.pages.filter(isPageVisible)
|
|
462
|
+
|
|
463
|
+
// When nested, only include root-level pages at top level
|
|
464
|
+
// (children will be nested inside their parents)
|
|
465
|
+
if (nested) {
|
|
466
|
+
filteredPages = filteredPages.filter(page => !page.parent)
|
|
467
|
+
}
|
|
410
468
|
|
|
411
469
|
// Apply custom sort or default to order
|
|
412
470
|
if (customSort) {
|
|
@@ -424,7 +482,7 @@ export default class Website {
|
|
|
424
482
|
order: page.order,
|
|
425
483
|
hasContent: page.getBodyBlocks().length > 0,
|
|
426
484
|
children: nested && page.hasChildren()
|
|
427
|
-
? page.children.map(buildPageInfo)
|
|
485
|
+
? page.children.filter(isPageVisible).map(buildPageInfo)
|
|
428
486
|
: []
|
|
429
487
|
})
|
|
430
488
|
|