astro-accelerator-utils 0.1.10 → 0.1.12

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,13 @@
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
12
  import * as Taxonomy from "./lib/taxonomy.mjs";
13
- export { DateFormatter, Markdown, Posts, UrlFormatter, Cache, PostQueries, PostFiltering, PostOrdering, PostPaging, FooterMenu, Navigation, Taxonomy };
13
+ export { Cache, DateFormatter, Markdown, Navigation, Posts, UrlFormatter, PostQueries, PostFiltering, PostOrdering, PostPaging, FooterMenu, 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
 
@@ -13,11 +14,12 @@ import * as Navigation 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,
@@ -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[]>}
@@ -156,6 +157,10 @@ export async function getBreadcrumbs (currentUrl, site) {
156
157
  const allPages = await getPages();
157
158
 
158
159
  const pathParts = currentUrl.pathname.split('/');
160
+
161
+ if (site.subfolder.length > 0) {
162
+ pathParts.shift();
163
+ }
159
164
 
160
165
  /** @type {NavPage[]} */
161
166
  const navPages = [];
@@ -177,6 +182,7 @@ export async function getBreadcrumbs (currentUrl, site) {
177
182
 
178
183
  /**
179
184
  * Converts a MarkdownInstance into a NavPage
185
+ * @deprecated Use Navigation.mapNavPage()
180
186
  * @param {MarkdownInstance} page
181
187
  * @param {Site}
182
188
  * @returns {NavPage}
@@ -218,6 +224,7 @@ export function mapNavPage (page, site) {
218
224
 
219
225
  /**
220
226
  * Walks the tree to set current page
227
+ * @deprecated Use Navigation.setCurrentPage()
221
228
  * @param {NavPage[]} pages
222
229
  * @param {URL} currentUrl
223
230
  */
@@ -231,6 +238,7 @@ export function setCurrentPage (pages, currentUrl) {
231
238
 
232
239
  /**
233
240
  * Pops matching page from array
241
+ * @deprecated Use Navigation.popMatchingPage()
234
242
  * @param {MarkdownInstance[]} allPages
235
243
  * @param {string} search
236
244
  * @returns
@@ -0,0 +1,42 @@
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
+ * Walks a NavPage tree to set current page
23
+ * @param {NavPage[]} pages
24
+ * @param {URL} currentUrl
25
+ */
26
+ setCurrentPage(pages: NavPage[], currentUrl: URL): void;
27
+ /**
28
+ * Converts a MarkdownInstance into a NavPage
29
+ * @param {MarkdownInstance} page
30
+ * @returns {NavPage}
31
+ */
32
+ mapNavPage(page: MarkdownInstance): NavPage;
33
+ /**
34
+ * Pops matching page from array
35
+ * @param {MarkdownInstance[]} allPages
36
+ * @param {string} search
37
+ * @returns
38
+ */
39
+ popMatchingPage(allPages: MarkdownInstance[], search: string): import("../../types/Astro").MarkdownInstance;
40
+ }
41
+ export type MarkdownInstance = import("../../types/Astro").MarkdownInstance;
42
+ export type NavPage = import("../../types/NavPage").NavPage;
@@ -0,0 +1,131 @@
1
+ /**
2
+ * @typedef { import("../../types/Astro").MarkdownInstance } MarkdownInstance
3
+ * @typedef { import("../../types/NavPage").NavPage } NavPage
4
+ */
5
+
6
+ export class Navigation {
7
+ /**
8
+ * Constructor
9
+ * @param {Posts} posts
10
+ * @param {UrlFormatter} urlFormatter
11
+ */
12
+ constructor(posts, urlFormatter) {
13
+ /* istanbul ignore next */
14
+ this.posts = posts;
15
+ this.urlFormatter = urlFormatter;
16
+ }
17
+
18
+ /**
19
+ * Returns a list of breadcrumbs
20
+ * @param {URL} currentUrl
21
+ * @param {string} subfolder
22
+ * @returns {NavPage[]}
23
+ */
24
+ breadcrumbs (currentUrl, subfolder) {
25
+ const allPages = this.posts.all();
26
+
27
+ const pathParts = currentUrl.pathname.split('/');
28
+
29
+ if (subfolder.length > 0) {
30
+ // Running in a subfolder
31
+ pathParts.shift();
32
+ }
33
+
34
+ /** @type {NavPage[]} */
35
+ const navPages = [];
36
+ let path = '';
37
+
38
+ pathParts.forEach((part) => {
39
+ path += part.length > 0 ? '/' + part : '';
40
+ const match = this.popMatchingPage(allPages, path);
41
+
42
+ if (match) {
43
+ navPages.push(this.mapNavPage(match));
44
+ }
45
+ });
46
+
47
+ this.setCurrentPage(navPages, currentUrl);
48
+
49
+ return navPages;
50
+ }
51
+
52
+ /**
53
+ * Walks a NavPage tree to set current page
54
+ * @param {NavPage[]} pages
55
+ * @param {URL} currentUrl
56
+ */
57
+ setCurrentPage (pages, currentUrl) {
58
+ pages.forEach(p => {
59
+ p.isOpen = currentUrl.pathname.startsWith(p.url);
60
+ p.ariaCurrent = p.url == currentUrl.pathname
61
+ ? 'page'
62
+ : false;
63
+
64
+ if (p.children) {
65
+ this.setCurrentPage(p.children, currentUrl);
66
+ }
67
+ });
68
+ }
69
+
70
+ /**
71
+ * Converts a MarkdownInstance into a NavPage
72
+ * @param {MarkdownInstance} page
73
+ * @returns {NavPage}
74
+ */
75
+ mapNavPage (page) {
76
+ let url = page.url == null || (page.url ?? '').length == 0
77
+ ? '/'
78
+ : page.url;
79
+
80
+ // Send visitors straight to the first page
81
+ if (page.frontmatter.paged) {
82
+ url += '/1/';
83
+ }
84
+
85
+ url = this.urlFormatter.addSlashToAddress(url);
86
+
87
+ if (page.frontmatter.layout == 'src/layouts/Redirect.astro') {
88
+ // Skips past the redirect
89
+ url = page.frontmatter.redirect;
90
+ }
91
+
92
+ /** @type {NavPage} */
93
+ const entry = {
94
+ section: page.frontmatter.navSection ?? page.frontmatter.navTitle ?? page.frontmatter.title,
95
+ title: page.frontmatter.navTitle ?? page.frontmatter.title,
96
+ url: url,
97
+ order: page.frontmatter.navOrder,
98
+ children: [],
99
+ // These are later set to the correct value, but not now as we want to cache
100
+ isOpen: false,
101
+ ariaCurrent: false
102
+ }
103
+
104
+ return entry;
105
+ }
106
+
107
+ /**
108
+ * Pops matching page from array
109
+ * @param {MarkdownInstance[]} allPages
110
+ * @param {string} search
111
+ * @returns
112
+ */
113
+ popMatchingPage (allPages, search) {
114
+ const numberToRemove = 1;
115
+ let indexToRemove = -1;
116
+ let match = null;
117
+
118
+ for (let i = 0; i < allPages.length; i++) {
119
+ if (allPages[i].url == search) {
120
+ indexToRemove = i;
121
+ match = allPages[i];
122
+ }
123
+ }
124
+
125
+ if (match) {
126
+ allPages.splice(indexToRemove, numberToRemove);
127
+ }
128
+
129
+ return match;
130
+ }
131
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-accelerator-utils",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
4
4
  "description": "Astro utilities for Astro Accelerator.",
5
5
  "main": "index.mjs",
6
6
  "type": "module",