astro-accelerator-utils 0.1.11 → 0.1.13

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 CHANGED
@@ -1,13 +1,14 @@
1
+ import { Cache } from "./lib/v1/cache.mjs";
1
2
  import { DateFormatter } from "./lib/v1/dates.mjs";
2
3
  import { Markdown } from "./lib/v1/markdown.mjs";
4
+ import { Navigation } from "./lib/v1/navigation.mjs";
3
5
  import { Posts } from "./lib/v1/posts.mjs";
4
6
  import { UrlFormatter } from "./lib/v1/urls.mjs";
5
- import { Cache } from "./lib/v1/cache.mjs";
6
7
  import * as PostQueries from "./lib/postQueries.mjs";
7
8
  import * as PostFiltering from "./lib/postFiltering.mjs";
8
9
  import * as PostOrdering from "./lib/postOrdering.mjs";
9
10
  import * as PostPaging from "./lib/postPaging.mjs";
10
11
  import * as FooterMenu from "./lib/footerMenu.mjs";
11
- import * as Navigation from "./lib/navigation.mjs";
12
+ import * as NavigationX from "./lib/navigation.mjs";
12
13
  import * as Taxonomy from "./lib/taxonomy.mjs";
13
- export { DateFormatter, Markdown, Posts, UrlFormatter, Cache, PostQueries, PostFiltering, PostOrdering, PostPaging, FooterMenu, Navigation, Taxonomy };
14
+ export { Cache, DateFormatter, Markdown, Navigation, Posts, UrlFormatter, PostQueries, PostFiltering, PostOrdering, PostPaging, FooterMenu, NavigationX, Taxonomy };
package/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Cache } from './lib/v1/cache.mjs';
2
2
  import { DateFormatter } from './lib/v1/dates.mjs';
3
3
  import { Markdown } from './lib/v1/markdown.mjs';
4
+ import { Navigation } from './lib/v1/navigation.mjs';
4
5
  import { Posts } from './lib/v1/posts.mjs';
5
6
  import { UrlFormatter } from './lib/v1/urls.mjs';
6
7
 
@@ -9,20 +10,21 @@ import * as PostFiltering from './lib/postFiltering.mjs';
9
10
  import * as PostOrdering from './lib/postOrdering.mjs';
10
11
  import * as PostPaging from './lib/postPaging.mjs';
11
12
  import * as FooterMenu from './lib/footerMenu.mjs';
12
- import * as Navigation from './lib/navigation.mjs';
13
+ import * as NavigationX from './lib/navigation.mjs';
13
14
  import * as Taxonomy from './lib/taxonomy.mjs';
14
15
 
15
16
  export {
17
+ Cache,
16
18
  DateFormatter,
17
19
  Markdown,
20
+ Navigation,
18
21
  Posts,
19
22
  UrlFormatter,
20
- Cache,
21
23
  PostQueries,
22
24
  PostFiltering,
23
25
  PostOrdering,
24
26
  PostPaging,
25
27
  FooterMenu,
26
- Navigation,
28
+ NavigationX,
27
29
  Taxonomy
28
30
  };
@@ -1,5 +1,5 @@
1
1
  /**
2
- *
2
+ * @deprecated Use Navigation.menu()
3
3
  * @param {URL} currentUrl
4
4
  * @param {Site} site
5
5
  * @param {NavPage | 'auto'} menu
@@ -7,13 +7,13 @@
7
7
  */
8
8
  export function getMenu(currentUrl: URL, site: Site, menu: NavPage | 'auto'): Promise<NavPage[]>;
9
9
  /**
10
- *
10
+ * @deprecated Use Navigation.isNavPage
11
11
  * @param {NavPage | 'auto' | 'tags' | 'toptags' | 'categories'} item
12
12
  * @returns {item is NavPage}
13
13
  */
14
14
  export function isNavPage(item: NavPage | 'auto' | 'tags' | 'toptags' | 'categories'): item is import("../types/NavPage").NavPage;
15
15
  /**
16
- *
16
+ * @deprecated Use Navigation.autoMenu()
17
17
  * @param {URL} currentUrl
18
18
  * @param {Site} site
19
19
  * @returns {Promise<NavPage[]}
@@ -11,7 +11,7 @@ import * as PostFiltering from './postFiltering.mjs';
11
11
  const cache = new Cache(200);
12
12
 
13
13
  /**
14
- *
14
+ * @deprecated Use Navigation.menu()
15
15
  * @param {URL} currentUrl
16
16
  * @param {Site} site
17
17
  * @param {NavPage | 'auto'} menu
@@ -47,7 +47,7 @@ export async function getMenu (currentUrl, site, menu) {
47
47
  }
48
48
 
49
49
  /**
50
- *
50
+ * @deprecated Use Navigation.isNavPage
51
51
  * @param {NavPage | 'auto' | 'tags' | 'toptags' | 'categories'} item
52
52
  * @returns {item is NavPage}
53
53
  */
@@ -61,7 +61,7 @@ export function isNavPage (item) {
61
61
 
62
62
 
63
63
  /**
64
- *
64
+ * @deprecated Use Navigation.autoMenu()
65
65
  * @param {URL} currentUrl
66
66
  * @param {Site} site
67
67
  * @returns {Promise<NavPage[]}
@@ -27,6 +27,7 @@ export function getAuthors(frontmatter: Frontmatter): Promise<AuthorList>;
27
27
  export function getAuthorInfo(slug: string): Promise<AuthorInfo>;
28
28
  /**
29
29
  * Returns a list of breadcrumbs
30
+ * @deprecated Use Navigation.breadcrumbs
30
31
  * @param {URL} currentUrl
31
32
  * @param {Site} site
32
33
  * @returns {Promise<NavPage[]>}
@@ -34,6 +35,7 @@ export function getAuthorInfo(slug: string): Promise<AuthorInfo>;
34
35
  export function getBreadcrumbs(currentUrl: URL, site: Site): Promise<NavPage[]>;
35
36
  /**
36
37
  * Converts a MarkdownInstance into a NavPage
38
+ * @deprecated Use Navigation.mapNavPage()
37
39
  * @param {MarkdownInstance} page
38
40
  * @param {Site}
39
41
  * @returns {NavPage}
@@ -41,12 +43,14 @@ export function getBreadcrumbs(currentUrl: URL, site: Site): Promise<NavPage[]>;
41
43
  export function mapNavPage(page: MarkdownInstance, site: any): NavPage;
42
44
  /**
43
45
  * Walks the tree to set current page
46
+ * @deprecated Use Navigation.setCurrentPage()
44
47
  * @param {NavPage[]} pages
45
48
  * @param {URL} currentUrl
46
49
  */
47
50
  export function setCurrentPage(pages: NavPage[], currentUrl: URL): void;
48
51
  /**
49
52
  * Pops matching page from array
53
+ * @deprecated Use Navigation.popMatchingPage()
50
54
  * @param {MarkdownInstance[]} allPages
51
55
  * @param {string} search
52
56
  * @returns
@@ -148,6 +148,7 @@ export async function getAuthorInfo (slug) {
148
148
 
149
149
  /**
150
150
  * Returns a list of breadcrumbs
151
+ * @deprecated Use Navigation.breadcrumbs
151
152
  * @param {URL} currentUrl
152
153
  * @param {Site} site
153
154
  * @returns {Promise<NavPage[]>}
@@ -181,6 +182,7 @@ export async function getBreadcrumbs (currentUrl, site) {
181
182
 
182
183
  /**
183
184
  * Converts a MarkdownInstance into a NavPage
185
+ * @deprecated Use Navigation.mapNavPage()
184
186
  * @param {MarkdownInstance} page
185
187
  * @param {Site}
186
188
  * @returns {NavPage}
@@ -222,6 +224,7 @@ export function mapNavPage (page, site) {
222
224
 
223
225
  /**
224
226
  * Walks the tree to set current page
227
+ * @deprecated Use Navigation.setCurrentPage()
225
228
  * @param {NavPage[]} pages
226
229
  * @param {URL} currentUrl
227
230
  */
@@ -235,6 +238,7 @@ export function setCurrentPage (pages, currentUrl) {
235
238
 
236
239
  /**
237
240
  * Pops matching page from array
241
+ * @deprecated Use Navigation.popMatchingPage()
238
242
  * @param {MarkdownInstance[]} allPages
239
243
  * @param {string} search
240
244
  * @returns
@@ -0,0 +1,62 @@
1
+ /**
2
+ * @typedef { import("../../types/Astro").MarkdownInstance } MarkdownInstance
3
+ * @typedef { import("../../types/NavPage").NavPage } NavPage
4
+ */
5
+ export class Navigation {
6
+ /**
7
+ * Constructor
8
+ * @param {Posts} posts
9
+ * @param {UrlFormatter} urlFormatter
10
+ */
11
+ constructor(posts: Posts, urlFormatter: UrlFormatter);
12
+ posts: Posts;
13
+ urlFormatter: UrlFormatter;
14
+ /**
15
+ * Returns a list of breadcrumbs
16
+ * @param {URL} currentUrl
17
+ * @param {string} subfolder
18
+ * @returns {NavPage[]}
19
+ */
20
+ breadcrumbs(currentUrl: URL, subfolder: string): NavPage[];
21
+ /**
22
+ *
23
+ * @param {URL} currentUrl
24
+ * @param {string} subfolder
25
+ * @param {(NavPage | 'auto')[]} menu
26
+ * @returns {NavPage[]}
27
+ */
28
+ menu(currentUrl: URL, subfolder: string, menu: (NavPage | 'auto')[]): NavPage[];
29
+ /**
30
+ *
31
+ * @param {string} subfolder
32
+ * @returns {NavPage[]}
33
+ */
34
+ autoMenu(subfolder: string): NavPage[];
35
+ /**
36
+ * Walks a NavPage tree to set current page
37
+ * @param {NavPage[]} pages
38
+ * @param {URL} currentUrl
39
+ */
40
+ setCurrentPage(pages: NavPage[], currentUrl: URL): void;
41
+ /**
42
+ * Converts a MarkdownInstance into a NavPage
43
+ * @param {MarkdownInstance} page
44
+ * @returns {NavPage}
45
+ */
46
+ mapNavPage(page: MarkdownInstance): NavPage;
47
+ /**
48
+ * Checks whether the item is a NavPage
49
+ * @param {NavPage | 'auto' | 'tags' | 'toptags' | 'categories'} item
50
+ * @returns {item is NavPage}
51
+ */
52
+ isNavPage(item: NavPage | 'auto' | 'tags' | 'toptags' | 'categories'): item is import("../../types/NavPage").NavPage;
53
+ /**
54
+ * Pops matching page from array
55
+ * @param {MarkdownInstance[]} allPages
56
+ * @param {string} search
57
+ * @returns
58
+ */
59
+ popMatchingPage(allPages: MarkdownInstance[], search: string): import("../../types/Astro").MarkdownInstance;
60
+ }
61
+ export type MarkdownInstance = import("../../types/Astro").MarkdownInstance;
62
+ export type NavPage = import("../../types/NavPage").NavPage;
@@ -0,0 +1,220 @@
1
+ import * as PostFiltering from '../postFiltering.mjs';
2
+
3
+ /**
4
+ * @typedef { import("../../types/Astro").MarkdownInstance } MarkdownInstance
5
+ * @typedef { import("../../types/NavPage").NavPage } NavPage
6
+ */
7
+
8
+ export class Navigation {
9
+ /**
10
+ * Constructor
11
+ * @param {Posts} posts
12
+ * @param {UrlFormatter} urlFormatter
13
+ */
14
+ constructor(posts, urlFormatter) {
15
+ /* istanbul ignore next */
16
+ this.posts = posts;
17
+ this.urlFormatter = urlFormatter;
18
+ }
19
+
20
+ /**
21
+ * Returns a list of breadcrumbs
22
+ * @param {URL} currentUrl
23
+ * @param {string} subfolder
24
+ * @returns {NavPage[]}
25
+ */
26
+ breadcrumbs (currentUrl, subfolder) {
27
+ const allPages = this.posts.all();
28
+
29
+ const pathParts = currentUrl.pathname.split('/');
30
+
31
+ if (subfolder.length > 0) {
32
+ // Running in a subfolder
33
+ pathParts.shift();
34
+ }
35
+
36
+ /** @type {NavPage[]} */
37
+ const navPages = [];
38
+ let path = '';
39
+
40
+ pathParts.forEach((part) => {
41
+ path += part.length > 0 ? '/' + part : '';
42
+ const match = this.popMatchingPage(allPages, path);
43
+
44
+ if (match) {
45
+ navPages.push(this.mapNavPage(match));
46
+ }
47
+ });
48
+
49
+ this.setCurrentPage(navPages, currentUrl);
50
+
51
+ return navPages;
52
+ }
53
+
54
+ /**
55
+ *
56
+ * @param {URL} currentUrl
57
+ * @param {string} subfolder
58
+ * @param {(NavPage | 'auto')[]} menu
59
+ * @returns {NavPage[]}
60
+ */
61
+ menu (currentUrl, subfolder, menu) {
62
+ const pages = [];
63
+ for (let i = 0; i < menu.length; i++) {
64
+ const item = menu[i];
65
+ if (this.isNavPage(item)) {
66
+ pages.push(item);
67
+ } else {
68
+ const p = this.autoMenu(subfolder);
69
+ for (let j = 0; j < p.length; j++) {
70
+ pages.push(p[j]);
71
+ }
72
+ }
73
+ }
74
+
75
+ this.setCurrentPage(pages, currentUrl);
76
+
77
+ return pages;
78
+ }
79
+
80
+ /**
81
+ *
82
+ * @param {string} subfolder
83
+ * @returns {NavPage[]}
84
+ */
85
+ autoMenu (subfolder) {
86
+ const allPages = this.posts
87
+ .all()
88
+ .filter(PostFiltering.showInMenu);
89
+
90
+ const topLevelPages = this.posts
91
+ .root(subfolder)
92
+ .filter(PostFiltering.showInMenu);
93
+
94
+ const pageHierarchy = topLevelPages
95
+ .map(p => this.mapNavPage(p))
96
+ .sort((a, b) => a.order - b.order);
97
+
98
+ /** @type {NavPage[]} */
99
+ const pageList = allPages.map(p => this.mapNavPage(p));
100
+
101
+ for (let i = 0; i < pageHierarchy.length; i++) {
102
+ const page = pageHierarchy[i];
103
+
104
+ if (i > 0) {
105
+ // Don't add children to first link (Home)
106
+ page.children = pageList
107
+ .filter((mp) =>
108
+ page.url != '/'
109
+ && mp.url != page.url
110
+ && mp.url.startsWith(page.url)
111
+ )
112
+ .sort((mp) => mp.order);
113
+ }
114
+
115
+ if (page.children.length > 0) {
116
+ const ownChild = structuredClone(page);
117
+ ownChild.order = -1;
118
+ ownChild.children = [];
119
+ page.children.push(ownChild);
120
+ }
121
+ }
122
+
123
+ return pageHierarchy;
124
+ }
125
+
126
+
127
+ /**
128
+ * Walks a NavPage tree to set current page
129
+ * @param {NavPage[]} pages
130
+ * @param {URL} currentUrl
131
+ */
132
+ setCurrentPage (pages, currentUrl) {
133
+ pages.forEach(p => {
134
+ p.isOpen = currentUrl.pathname.startsWith(p.url);
135
+ p.ariaCurrent = p.url == currentUrl.pathname
136
+ ? 'page'
137
+ : false;
138
+
139
+ if (p.children) {
140
+ this.setCurrentPage(p.children, currentUrl);
141
+ }
142
+ });
143
+ }
144
+
145
+ /**
146
+ * Converts a MarkdownInstance into a NavPage
147
+ * @param {MarkdownInstance} page
148
+ * @returns {NavPage}
149
+ */
150
+ mapNavPage (page) {
151
+ let url = page.url == null || (page.url ?? '').length == 0
152
+ ? '/'
153
+ : page.url;
154
+
155
+ // Send visitors straight to the first page
156
+ if (page.frontmatter.paged) {
157
+ url += '/1/';
158
+ }
159
+
160
+ url = this.urlFormatter.addSlashToAddress(url);
161
+
162
+ if (page.frontmatter.layout == 'src/layouts/Redirect.astro') {
163
+ // Skips past the redirect
164
+ url = page.frontmatter.redirect;
165
+ }
166
+
167
+ /** @type {NavPage} */
168
+ const entry = {
169
+ section: page.frontmatter.navSection ?? page.frontmatter.navTitle ?? page.frontmatter.title,
170
+ title: page.frontmatter.navTitle ?? page.frontmatter.title,
171
+ url: url,
172
+ order: page.frontmatter.navOrder,
173
+ children: [],
174
+ // These are later set to the correct value, but not now as we want to cache
175
+ isOpen: false,
176
+ ariaCurrent: false
177
+ }
178
+
179
+ return entry;
180
+ }
181
+
182
+
183
+ /**
184
+ * Checks whether the item is a NavPage
185
+ * @param {NavPage | 'auto' | 'tags' | 'toptags' | 'categories'} item
186
+ * @returns {item is NavPage}
187
+ */
188
+ isNavPage (item) {
189
+ if (typeof item === 'string' && ['auto', 'tags', 'toptags', 'categories'].includes(item)) {
190
+ return false;
191
+ }
192
+
193
+ return true;
194
+ }
195
+
196
+ /**
197
+ * Pops matching page from array
198
+ * @param {MarkdownInstance[]} allPages
199
+ * @param {string} search
200
+ * @returns
201
+ */
202
+ popMatchingPage (allPages, search) {
203
+ const numberToRemove = 1;
204
+ let indexToRemove = -1;
205
+ let match = null;
206
+
207
+ for (let i = 0; i < allPages.length; i++) {
208
+ if (allPages[i].url == search) {
209
+ indexToRemove = i;
210
+ match = allPages[i];
211
+ }
212
+ }
213
+
214
+ if (match) {
215
+ allPages.splice(indexToRemove, numberToRemove);
216
+ }
217
+
218
+ return match;
219
+ }
220
+ }
package/lib/v1/posts.mjs CHANGED
@@ -35,6 +35,7 @@ export class Posts {
35
35
  * @returns {MarkdownInstance[]}
36
36
  */
37
37
  root (subfolder) {
38
+ console.log('SUBFOLDER IS', subfolder)
38
39
  const isRoot = subfolder.length == 0;
39
40
  const expectedDepth = isRoot ? 1 : 2;
40
41
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-accelerator-utils",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "Astro utilities for Astro Accelerator.",
5
5
  "main": "index.mjs",
6
6
  "type": "module",