astro-accelerator-utils 0.0.29 → 0.0.31

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.
@@ -4,34 +4,34 @@
4
4
  */
5
5
  /**
6
6
  * Predicate for whether a page should appear in the sitemap
7
- * @param {MarkdownInstance<Record<string, any>>} p
7
+ * @param {MarkdownInstance} p
8
8
  * @returns {boolean}
9
9
  */
10
- export function showInSitemap(p: any): boolean;
10
+ export function showInSitemap(p: MarkdownInstance): boolean;
11
11
  /**
12
12
  * Predicate for whether a page should appear in the site search
13
- * @param {MarkdownInstance<Record<string, any>>} p
13
+ * @param {MarkdownInstance} p
14
14
  * @returns {boolean}
15
15
  */
16
- export function showInSearch(p: any): boolean;
16
+ export function showInSearch(p: MarkdownInstance): boolean;
17
17
  /**
18
18
  * Predicate for whether a page should appear in the navigation menu
19
- * @param {MarkdownInstance<Record<string, any>>} p
19
+ * @param {MarkdownInstance} p
20
20
  * @returns {boolean}
21
21
  */
22
- export function showInMenu(p: any): boolean;
22
+ export function showInMenu(p: MarkdownInstance): boolean;
23
23
  /**
24
24
  * Predicate for whether a page is an author page
25
- * @param {MarkdownInstance<Record<string, any>>} p
25
+ * @param {MarkdownInstance} p
26
26
  * @returns {boolean}
27
27
  */
28
- export function isAuthor(p: any): boolean;
28
+ export function isAuthor(p: MarkdownInstance): boolean;
29
29
  /**
30
30
  * Predicate for whether a page is a search page
31
- * @param {MarkdownInstance<Record<string, any>>} p
31
+ * @param {MarkdownInstance} p
32
32
  * @returns {boolean}
33
33
  */
34
- export function isSearch(p: any): boolean;
34
+ export function isSearch(p: MarkdownInstance): boolean;
35
35
  /**
36
36
  * Predicate for whether a page should be listed
37
37
  * @param {MarkdownInstance<Record<string, any>>} p
@@ -39,4 +39,4 @@ export function isSearch(p: any): boolean;
39
39
  */
40
40
  export function isListable(p: any): boolean;
41
41
  export type PagePredicate = import("../types/PagePredicate").PagePredicate;
42
- export type MarkdownInstance = any;
42
+ export type MarkdownInstance = import("../types/Astro").MarkdownInstance;
@@ -5,7 +5,7 @@
5
5
 
6
6
  /**
7
7
  * Predicate for whether a page should appear in the sitemap
8
- * @param {MarkdownInstance<Record<string, any>>} p
8
+ * @param {MarkdownInstance} p
9
9
  * @returns {boolean}
10
10
  */
11
11
  export function showInSitemap (p) {
@@ -20,7 +20,7 @@ export function showInSitemap (p) {
20
20
 
21
21
  /**
22
22
  * Predicate for whether a page should appear in the site search
23
- * @param {MarkdownInstance<Record<string, any>>} p
23
+ * @param {MarkdownInstance} p
24
24
  * @returns {boolean}
25
25
  */
26
26
  export function showInSearch (p) {
@@ -35,7 +35,7 @@ export function showInSearch (p) {
35
35
 
36
36
  /**
37
37
  * Predicate for whether a page should appear in the navigation menu
38
- * @param {MarkdownInstance<Record<string, any>>} p
38
+ * @param {MarkdownInstance} p
39
39
  * @returns {boolean}
40
40
  */
41
41
  export function showInMenu (p) {
@@ -49,7 +49,7 @@ export function showInMenu (p) {
49
49
 
50
50
  /**
51
51
  * Predicate for whether a page is an author page
52
- * @param {MarkdownInstance<Record<string, any>>} p
52
+ * @param {MarkdownInstance} p
53
53
  * @returns {boolean}
54
54
  */
55
55
  export function isAuthor (p) {
@@ -62,7 +62,7 @@ export function isAuthor (p) {
62
62
 
63
63
  /**
64
64
  * Predicate for whether a page is a search page
65
- * @param {MarkdownInstance<Record<string, any>>} p
65
+ * @param {MarkdownInstance} p
66
66
  * @returns {boolean}
67
67
  */
68
68
  export function isSearch (p) {
@@ -3,23 +3,23 @@
3
3
  */
4
4
  /**
5
5
  * Sorts by the pubDate field
6
- * @param {MarkdownInstance<Record<string, any>>} a
7
- * @param {MarkdownInstance<Record<string, any>>} b
6
+ * @param {MarkdownInstance} a
7
+ * @param {MarkdownInstance} b
8
8
  * @returns {any}
9
9
  */
10
- export function sortByPubDate(a: any, b: any): any;
10
+ export function sortByPubDate(a: MarkdownInstance, b: MarkdownInstance): any;
11
11
  /**
12
12
  * Sorts by the pubDate field in descending order
13
- * @param {MarkdownInstance<Record<string, any>>} a
14
- * @param {MarkdownInstance<Record<string, any>>} b
13
+ * @param {MarkdownInstance} a
14
+ * @param {MarkdownInstance} b
15
15
  * @returns {any}
16
16
  */
17
- export function sortByPubDateDesc(a: any, b: any): any;
17
+ export function sortByPubDateDesc(a: MarkdownInstance, b: MarkdownInstance): any;
18
18
  /**
19
19
  * Sorts by the modDate field
20
- * @param {MarkdownInstance<Record<string, any>>} a
21
- * @param {MarkdownInstance<Record<string, any>>} b
20
+ * @param {MarkdownInstance} a
21
+ * @param {MarkdownInstance} b
22
22
  * @returns {any}
23
23
  */
24
- export function sortByModDate(a: any, b: any): any;
25
- export type MarkdownInstance = any;
24
+ export function sortByModDate(a: MarkdownInstance, b: MarkdownInstance): any;
25
+ export type MarkdownInstance = import("../types/Astro").MarkdownInstance;
@@ -4,8 +4,8 @@
4
4
 
5
5
  /**
6
6
  * Sorts by the pubDate field
7
- * @param {MarkdownInstance<Record<string, any>>} a
8
- * @param {MarkdownInstance<Record<string, any>>} b
7
+ * @param {MarkdownInstance} a
8
+ * @param {MarkdownInstance} b
9
9
  * @returns {any}
10
10
  */
11
11
  export function sortByPubDate (a, b) {
@@ -14,8 +14,8 @@ export function sortByPubDate (a, b) {
14
14
 
15
15
  /**
16
16
  * Sorts by the pubDate field in descending order
17
- * @param {MarkdownInstance<Record<string, any>>} a
18
- * @param {MarkdownInstance<Record<string, any>>} b
17
+ * @param {MarkdownInstance} a
18
+ * @param {MarkdownInstance} b
19
19
  * @returns {any}
20
20
  */
21
21
  export function sortByPubDateDesc (a, b) {
@@ -24,8 +24,8 @@ export function sortByPubDateDesc (a, b) {
24
24
 
25
25
  /**
26
26
  * Sorts by the modDate field
27
- * @param {MarkdownInstance<Record<string, any>>} a
28
- * @param {MarkdownInstance<Record<string, any>>} b
27
+ * @param {MarkdownInstance} a
28
+ * @param {MarkdownInstance} b
29
29
  * @returns {any}
30
30
  */
31
31
  export function sortByModDate (a, b) {
@@ -1,27 +1,42 @@
1
1
  /**
2
2
  * Fetches pages
3
3
  * @param {PagePredicate} [filter]
4
- * @returns {Promise<MarkdownInstance<Record<string, any>>[]>}
4
+ * @returns {Promise<MarkdownInstance}
5
5
  */
6
- export function getPages(filter?: PagePredicate): Promise<MarkdownInstance<Record<string, any>>[]>;
6
+ export function getPages(filter?: PagePredicate): Promise<MarkdownInstance>;
7
+ /**
8
+ *
9
+ * @param {Site} site
10
+ * @param {PagePredicate} [filter]
11
+ * @returns {Promise<MarkdownInstance[]>}
12
+ */
13
+ export function getTopLevelPages(site: Site, filter?: PagePredicate): Promise<MarkdownInstance[]>;
7
14
  /**
8
15
  * Gets a list of authors, and exposes main author and contributors
9
16
  * @param {Frontmatter} frontmatter
10
17
  * @returns {Promise<AuthorList>}
11
18
  */
12
19
  export function getAuthors(frontmatter: Frontmatter): Promise<AuthorList>;
20
+ /**
21
+ *
22
+ * @param {string} slug
23
+ * @returns {Promise<AuthorInfo>}
24
+ */
25
+ export function getAuthorInfo(slug: string): Promise<AuthorInfo>;
13
26
  /**
14
27
  * Returns a list of breadcrumbs
15
28
  * @param {URL} currentUrl
29
+ * @param {Site} site
16
30
  * @returns {NavPage[]}
17
31
  */
18
- export function getBreadcrumbs(currentUrl: URL): NavPage[];
32
+ export function getBreadcrumbs(currentUrl: URL, site: Site): NavPage[];
19
33
  /**
20
34
  * Converts a MarkdownInstance into a NavPage
21
- * @param {MarkdownInstance<Record<string, any>>} page
35
+ * @param {MarkdownInstance} page
36
+ * @param {Site}
22
37
  * @returns {NavPage}
23
38
  */
24
- export function mapNavPage(page: any): NavPage;
39
+ export function mapNavPage(page: MarkdownInstance, site: any): NavPage;
25
40
  /**
26
41
  * Walks the tree to set current page
27
42
  * @param {NavPage[]} pages
@@ -30,13 +45,15 @@ export function mapNavPage(page: any): NavPage;
30
45
  export function setCurrentPage(pages: NavPage[], currentUrl: URL): void;
31
46
  /**
32
47
  * Pops matching page from array
33
- * @param {MarkdownInstance<Record<string, any>>[]} allPages
48
+ * @param {MarkdownInstance[]} allPages
34
49
  * @param {string} search
35
50
  * @returns
36
51
  */
37
- export function popMatchingPage(allPages: MarkdownInstance<Record<string, any>>[], search: string): any;
52
+ export function popMatchingPage(allPages: MarkdownInstance[], search: string): import("../types/Astro").MarkdownInstance;
38
53
  export type PagePredicate = import("../types/PagePredicate").PagePredicate;
39
54
  export type Frontmatter = import("../types/Frontmatter").Frontmatter;
40
- export type MarkdownInstance = any;
55
+ export type MarkdownInstance = import("../types/Astro").MarkdownInstance;
41
56
  export type AuthorList = import("../types/AuthorList").AuthorList;
57
+ export type AuthorInfo = import("../types/AuthorInfo").AuthorInfo;
42
58
  export type NavPage = import("../types/NavPage").NavPage;
59
+ export type Site = import("../types/Site").Site;
@@ -7,7 +7,9 @@ import * as Urls from './urls.mjs';
7
7
  * @typedef { import("../types/Frontmatter").Frontmatter } Frontmatter
8
8
  * @typedef { import("../types/Astro").MarkdownInstance } MarkdownInstance
9
9
  * @typedef { import("../types/AuthorList").AuthorList } AuthorList
10
+ * @typedef { import("../types/AuthorInfo").AuthorInfo } AuthorInfo
10
11
  * @typedef { import("../types/NavPage").NavPage } NavPage
12
+ * @typedef { import("../types/Site").Site } Site
11
13
  */
12
14
 
13
15
  function fetchAll () {
@@ -17,7 +19,7 @@ function fetchAll () {
17
19
  /**
18
20
  * Fetches pages
19
21
  * @param {PagePredicate} [filter]
20
- * @returns {Promise<MarkdownInstance<Record<string, any>>[]>}
22
+ * @returns {Promise<MarkdownInstance}
21
23
  */
22
24
  export async function getPages (filter) {
23
25
  const key = 'PageQueries__getPages';
@@ -36,6 +38,39 @@ export async function getPages (filter) {
36
38
  return allPages.filter(filter);
37
39
  }
38
40
 
41
+ /**
42
+ *
43
+ * @param {Site} site
44
+ * @param {PagePredicate} [filter]
45
+ * @returns {Promise<MarkdownInstance[]>}
46
+ */
47
+ export async function getTopLevelPages (site, filter) {
48
+ const key = 'PageQueries__getTopLevelPages';
49
+
50
+ /** @type {MarkdownInstance[]} */
51
+ let allPages = await getItem(key);
52
+
53
+ if (allPages == null) {
54
+ allPages = await getPages();
55
+
56
+ const isRoot = site.subfolder.length == 0;
57
+ const expectedDepth = isRoot ? 1 : 2;
58
+ allPages = allPages.filter(p => {
59
+ const depth = (p.url ?? '/').split('/').length - 1;
60
+ return depth == expectedDepth
61
+ || (depth == (expectedDepth - 1) && p.file.includes(site.subfolder.toLowerCase() + '.md'));
62
+ });
63
+
64
+ await setItem(key, allPages);
65
+ }
66
+
67
+ if (filter == null) {
68
+ return allPages;
69
+ }
70
+
71
+ return allPages.filter(filter);
72
+ }
73
+
39
74
  /**
40
75
  * Gets a list of authors, and exposes main author and contributors
41
76
  * @param {Frontmatter} frontmatter
@@ -77,13 +112,42 @@ export async function getAuthors (frontmatter) {
77
112
  return result;
78
113
  }
79
114
 
115
+ /**
116
+ *
117
+ * @param {string} slug
118
+ * @returns {Promise<AuthorInfo>}
119
+ */
120
+ export async function getAuthorInfo (slug) {
121
+ const cacheKey = 'Global__author_info';
122
+
123
+ /** @type {AuthorInfo} */
124
+ let authorInfo = await getItem(cacheKey);
125
+
126
+ if (authorInfo == null) {
127
+ const allPages = await getPages();
128
+
129
+ const author = allPages
130
+ .filter(PostFiltering.isAuthor)
131
+ .filter(x => x.url?.split('/')[2] == slug)[0];
132
+
133
+ authorInfo = {
134
+ frontmatter: author.frontmatter
135
+ };
136
+
137
+ await setItem(cacheKey, authorInfo);
138
+ }
139
+
140
+ return authorInfo;
141
+ }
142
+
80
143
  /**
81
144
  * Returns a list of breadcrumbs
82
145
  * @param {URL} currentUrl
146
+ * @param {Site} site
83
147
  * @returns {NavPage[]}
84
148
  */
85
- export async function getBreadcrumbs (currentUrl) {
86
- const allPages = await PostQueries.getPages();
149
+ export async function getBreadcrumbs (currentUrl, site) {
150
+ const allPages = await getPages();
87
151
 
88
152
  const pathParts = currentUrl.pathname.split('/');
89
153
 
@@ -96,7 +160,7 @@ export async function getBreadcrumbs (currentUrl) {
96
160
  const match = popMatchingPage(allPages, path);
97
161
 
98
162
  if (match) {
99
- navPages.push(mapNavPage(match));
163
+ navPages.push(mapNavPage(match, site));
100
164
  }
101
165
  });
102
166
 
@@ -107,10 +171,11 @@ export async function getBreadcrumbs (currentUrl) {
107
171
 
108
172
  /**
109
173
  * Converts a MarkdownInstance into a NavPage
110
- * @param {MarkdownInstance<Record<string, any>>} page
174
+ * @param {MarkdownInstance} page
175
+ * @param {Site}
111
176
  * @returns {NavPage}
112
177
  */
113
- export function mapNavPage (page) {
178
+ export function mapNavPage (page, site) {
114
179
 
115
180
  let url = page.url == null || (page.url ?? '').length == 0
116
181
  ? '/'
@@ -121,7 +186,7 @@ export function mapNavPage (page) {
121
186
  url += '/1/';
122
187
  }
123
188
 
124
- url = Urls.addSlashToAddress(url, SITE);
189
+ url = Urls.addSlashToAddress(url, site);
125
190
 
126
191
  if (page.frontmatter.layout == 'src/layouts/Redirect.astro') {
127
192
  // Skips past the redirect
@@ -158,7 +223,7 @@ export function setCurrentPage (pages, currentUrl) {
158
223
 
159
224
  /**
160
225
  * Pops matching page from array
161
- * @param {MarkdownInstance<Record<string, any>>[]} allPages
226
+ * @param {MarkdownInstance[]} allPages
162
227
  * @param {string} search
163
228
  * @returns
164
229
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-accelerator-utils",
3
- "version": "0.0.29",
3
+ "version": "0.0.31",
4
4
  "description": "Astro utilities for Astro Accelerator.",
5
5
  "main": "index.mjs",
6
6
  "files": [
package/types/Astro.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- export type MarkdownInstance<T extends Record<string, any>> = {
2
- frontmatter: T;
1
+ import { Frontmatter } from "./Frontmatter";
2
+ export type MarkdownInstance = {
3
+ frontmatter: Frontmatter;
3
4
  /** Absolute file path (e.g. `/home/user/projects/.../file.md`) */
4
5
  file: string;
5
6
  /** Browser URL for files under `/src/pages` (e.g. `/en/guides/markdown-content`) */
@@ -0,0 +1,4 @@
1
+ import { Frontmatter } from "./Frontmatter";
2
+ export interface AuthorInfo {
3
+ frontmatter: Frontmatter;
4
+ }
@@ -2,7 +2,7 @@ import type { MarkdownInstance } from "./Astro";
2
2
  import type { BannerImage } from "./BannerImage";
3
3
  export interface AuthorList {
4
4
  image: BannerImage | null;
5
- writers: MarkdownInstance<Record<string, any>>[];
6
- mainAuthor: MarkdownInstance<Record<string, any>> | null;
7
- contributors: MarkdownInstance<Record<string, any>>[];
5
+ writers: MarkdownInstance[];
6
+ mainAuthor: MarkdownInstance | null;
7
+ contributors: MarkdownInstance[];
8
8
  }
@@ -1,2 +1,2 @@
1
1
  import { MarkdownInstance } from "./Astro";
2
- export type PagePredicate = (value: MarkdownInstance<Record<string, any>>, index: number, array: MarkdownInstance<Record<string, any>>[]) => boolean;
2
+ export type PagePredicate = (value: MarkdownInstance, index: number, array: MarkdownInstance[]) => boolean;