astro-accelerator-utils 0.1.12 → 0.1.15

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