astro-accelerator-utils 0.0.33 → 0.0.34
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/index.d.mts +2 -1
- package/index.mjs +2 -0
- package/lib/navigation.d.mts +20 -0
- package/lib/navigation.mjs +97 -0
- package/package.json +1 -1
package/index.d.mts
CHANGED
|
@@ -5,5 +5,6 @@ import * as PostPaging from "./lib/postPaging.mjs";
|
|
|
5
5
|
import * as Cache from "./lib/cache.mjs";
|
|
6
6
|
import * as Dates from "./lib/dates.mjs";
|
|
7
7
|
import * as Markdown from "./lib/markdown.mjs";
|
|
8
|
+
import * as Navigation from "./lib/navigation.mjs";
|
|
8
9
|
import * as Urls from "./lib/urls.mjs";
|
|
9
|
-
export { PostQueries, PostFiltering, PostOrdering, PostPaging, Cache, Dates, Markdown, Urls };
|
|
10
|
+
export { PostQueries, PostFiltering, PostOrdering, PostPaging, Cache, Dates, Markdown, Navigation, Urls };
|
package/index.mjs
CHANGED
|
@@ -5,6 +5,7 @@ import * as PostPaging from './lib/postPaging.mjs';
|
|
|
5
5
|
import * as Cache from './lib/cache.mjs';
|
|
6
6
|
import * as Dates from './lib/dates.mjs';
|
|
7
7
|
import * as Markdown from './lib/markdown.mjs';
|
|
8
|
+
import * as Navigation from './lib/navigation.mjs';
|
|
8
9
|
import * as Urls from './lib/urls.mjs';
|
|
9
10
|
|
|
10
11
|
export {
|
|
@@ -15,5 +16,6 @@ export {
|
|
|
15
16
|
Cache,
|
|
16
17
|
Dates,
|
|
17
18
|
Markdown,
|
|
19
|
+
Navigation,
|
|
18
20
|
Urls
|
|
19
21
|
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef { import("../types/Site").Site } Site
|
|
3
|
+
* @typedef { import("../types/NavPage").NavPage } NavPage
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @param {URL} currentUrl
|
|
8
|
+
* @param {Site} site
|
|
9
|
+
* @returns {Promise<NavPage[]}
|
|
10
|
+
*/
|
|
11
|
+
export function getMenu(currentUrl: URL, site: Site): Promise<NavPage[]>;
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @param {URL} currentUrl
|
|
15
|
+
* @param {Site} site
|
|
16
|
+
* @returns {Promise<NavPage[]}
|
|
17
|
+
*/
|
|
18
|
+
export function getNavigation(currentUrl: URL, site: Site): Promise<NavPage[]>;
|
|
19
|
+
export type Site = import("../types/Site").Site;
|
|
20
|
+
export type NavPage = import("../types/NavPage").NavPage;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import * as PostQueries from './postQueries.mjs';
|
|
2
|
+
import * as PostFiltering from './postFiltering.mjs';
|
|
3
|
+
import * as Cache from './cache.mjs';
|
|
4
|
+
import { menu } from '/src/data/navigation';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef { import("../types/Site").Site } Site
|
|
8
|
+
* @typedef { import("../types/NavPage").NavPage } NavPage
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
* @param {URL} currentUrl
|
|
14
|
+
* @param {Site} site
|
|
15
|
+
* @returns {Promise<NavPage[]}
|
|
16
|
+
*/
|
|
17
|
+
export async function getMenu (currentUrl, site) {
|
|
18
|
+
const key = 'Navigation__getMenu';
|
|
19
|
+
|
|
20
|
+
/** @type {NavPage[]} */
|
|
21
|
+
let pages = await Cache.getItem(key);
|
|
22
|
+
|
|
23
|
+
if (pages == null) {
|
|
24
|
+
pages = [];
|
|
25
|
+
for (let i = 0; i < menu.length; i++) {
|
|
26
|
+
const item = menu[i];
|
|
27
|
+
if (isNavPage(item)) {
|
|
28
|
+
pages.push(item);
|
|
29
|
+
} else {
|
|
30
|
+
const p = await getNavigation(currentUrl, site);
|
|
31
|
+
for (let j = 0; j < p.length; j++) {
|
|
32
|
+
pages.push(p[j]);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Cache the result
|
|
38
|
+
await Cache.setItem(key, pages);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
PostQueries.setCurrentPage(pages, currentUrl);
|
|
42
|
+
|
|
43
|
+
return pages;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
*
|
|
48
|
+
* @param {URL} currentUrl
|
|
49
|
+
* @param {Site} site
|
|
50
|
+
* @returns {Promise<NavPage[]}
|
|
51
|
+
*/
|
|
52
|
+
export async function getNavigation (currentUrl, site) {
|
|
53
|
+
|
|
54
|
+
const key = 'Navigation__getNavigation';
|
|
55
|
+
|
|
56
|
+
/** @type {NavPage[]} */
|
|
57
|
+
let pageHierarchy = await Cache.getItem(key);
|
|
58
|
+
|
|
59
|
+
if (pageHierarchy == null) {
|
|
60
|
+
const topLevelPages = await PostQueries.getTopLevelPages(site, PostFiltering.showInMenu);
|
|
61
|
+
const allPages = await PostQueries.getPages(PostFiltering.showInMenu);
|
|
62
|
+
|
|
63
|
+
pageHierarchy = topLevelPages
|
|
64
|
+
.map(p => PostQueries.mapNavPage(p, site))
|
|
65
|
+
.sort((a, b) => a.order - b.order);
|
|
66
|
+
|
|
67
|
+
/** @type {NavPage[]} */
|
|
68
|
+
const pageList = allPages.map(p => PostQueries.mapNavPage(p, site));
|
|
69
|
+
|
|
70
|
+
for (let i = 0; i < pageHierarchy.length; i++) {
|
|
71
|
+
const page = pageHierarchy[i];
|
|
72
|
+
|
|
73
|
+
if (i > 0) {
|
|
74
|
+
// Don't add children to first link (Home)
|
|
75
|
+
page.children = pageList
|
|
76
|
+
.filter((mp) =>
|
|
77
|
+
page.url != '/'
|
|
78
|
+
&& mp.url != page.url
|
|
79
|
+
&& mp.url.startsWith(page.url)
|
|
80
|
+
)
|
|
81
|
+
.sort((mp) => mp.order);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (page.children.length > 0) {
|
|
85
|
+
const ownChild = structuredClone(page);
|
|
86
|
+
ownChild.order = -1;
|
|
87
|
+
ownChild.children = [];
|
|
88
|
+
page.children.push(ownChild);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Cache the result
|
|
93
|
+
await Cache.setItem(key, pageHierarchy);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return pageHierarchy;
|
|
97
|
+
}
|