astro-accelerator-utils 0.0.34 → 0.0.36

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
@@ -6,5 +6,6 @@ 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
8
  import * as Navigation from "./lib/navigation.mjs";
9
+ import * as Taxonomy from "./lib/taxonomy.mjs";
9
10
  import * as Urls from "./lib/urls.mjs";
10
- export { PostQueries, PostFiltering, PostOrdering, PostPaging, Cache, Dates, Markdown, Navigation, Urls };
11
+ export { PostQueries, PostFiltering, PostOrdering, PostPaging, Cache, Dates, Markdown, Navigation, Taxonomy, Urls };
package/index.mjs CHANGED
@@ -6,6 +6,7 @@ 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
8
  import * as Navigation from './lib/navigation.mjs';
9
+ import * as Taxonomy from './lib/taxonomy.mjs';
9
10
  import * as Urls from './lib/urls.mjs';
10
11
 
11
12
  export {
@@ -17,5 +18,6 @@ export {
17
18
  Dates,
18
19
  Markdown,
19
20
  Navigation,
21
+ Taxonomy,
20
22
  Urls
21
23
  };
@@ -9,6 +9,12 @@
9
9
  * @returns {Promise<NavPage[]}
10
10
  */
11
11
  export function getMenu(currentUrl: URL, site: Site): Promise<NavPage[]>;
12
+ /**
13
+ *
14
+ * @param {NavPage | 'auto' | 'tags' | 'toptags' | 'categories'} item
15
+ * @returns {item is NavPage}
16
+ */
17
+ export function isNavPage(item: NavPage | 'auto' | 'tags' | 'toptags' | 'categories'): item is import("../types/NavPage").NavPage;
12
18
  /**
13
19
  *
14
20
  * @param {URL} currentUrl
@@ -1,7 +1,6 @@
1
1
  import * as PostQueries from './postQueries.mjs';
2
2
  import * as PostFiltering from './postFiltering.mjs';
3
3
  import * as Cache from './cache.mjs';
4
- import { menu } from '/src/data/navigation';
5
4
 
6
5
  /**
7
6
  * @typedef { import("../types/Site").Site } Site
@@ -12,9 +11,10 @@ import { menu } from '/src/data/navigation';
12
11
  *
13
12
  * @param {URL} currentUrl
14
13
  * @param {Site} site
14
+ * @param {NavPage | 'auto'} menu
15
15
  * @returns {Promise<NavPage[]}
16
16
  */
17
- export async function getMenu (currentUrl, site) {
17
+ export async function getMenu (currentUrl, site, menu) {
18
18
  const key = 'Navigation__getMenu';
19
19
 
20
20
  /** @type {NavPage[]} */
@@ -43,6 +43,20 @@ export async function getMenu (currentUrl, site) {
43
43
  return pages;
44
44
  }
45
45
 
46
+ /**
47
+ *
48
+ * @param {NavPage | 'auto' | 'tags' | 'toptags' | 'categories'} item
49
+ * @returns {item is NavPage}
50
+ */
51
+ export function isNavPage (item) {
52
+ if (typeof item === 'string' && ['auto', 'tags', 'toptags', 'categories'].includes(item)) {
53
+ return false;
54
+ }
55
+
56
+ return true;
57
+ }
58
+
59
+
46
60
  /**
47
61
  *
48
62
  * @param {URL} currentUrl
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @typedef { import("../types/Taxonomy").Taxonomy } Taxonomy
3
+ * @typedef { import("../types/Taxonomy").TaxonomyEntry } TaxonomyEntry
4
+ * @typedef { import("../types/Taxonomy").TaxonomyLinks } TaxonomyLinks
5
+ * @typedef { import("../types/Astro").MarkdownInstance } MarkdownInstance
6
+ */
7
+ /**
8
+ *
9
+ * @param {TaxonomyEntry} a
10
+ * @param {TaxonomyEntry} b
11
+ * @returns
12
+ */
13
+ export function sortByTitle(a: TaxonomyEntry, b: TaxonomyEntry): 0 | 1 | -1;
14
+ /**
15
+ *
16
+ * @param {any} translations
17
+ * @param {(entry: any) => string} lang
18
+ * @param {Site} site
19
+ * @returns {TaxonomyLinks}
20
+ */
21
+ export function taxonomyLinks(translations: any, lang: (entry: any) => string, site: Site): TaxonomyLinks;
22
+ /**
23
+ *
24
+ * @returns {Promise<Taxonomy}
25
+ */
26
+ export function getTaxonomy(): Promise<Taxonomy>;
27
+ export type Taxonomy = import("../types/Taxonomy").Taxonomy;
28
+ export type TaxonomyEntry = import("../types/Taxonomy").TaxonomyEntry;
29
+ export type TaxonomyLinks = import("../types/Taxonomy").TaxonomyLinks;
30
+ export type MarkdownInstance = import("../types/Astro").MarkdownInstance;
@@ -0,0 +1,123 @@
1
+ import * as Urls from './urls.mjs';
2
+ import * as Cache from './cache.mjs';
3
+ import * as PostQueries from './postQueries.mjs';
4
+
5
+ /**
6
+ * @typedef { import("../types/Taxonomy").Taxonomy } Taxonomy
7
+ * @typedef { import("../types/Taxonomy").TaxonomyEntry } TaxonomyEntry
8
+ * @typedef { import("../types/Taxonomy").TaxonomyLinks } TaxonomyLinks
9
+ * @typedef { import("../types/Astro").MarkdownInstance } MarkdownInstance
10
+ */
11
+
12
+ /**
13
+ *
14
+ * @param {TaxonomyEntry} a
15
+ * @param {TaxonomyEntry} b
16
+ * @returns
17
+ */
18
+ export function sortByTitle (a, b) {
19
+ if ( a.title < b.title ){
20
+ return -1;
21
+ }
22
+
23
+ if ( a.title > b.title ){
24
+ return 1;
25
+ }
26
+
27
+ return 0;
28
+ }
29
+
30
+ /**
31
+ *
32
+ * @param {any} translations
33
+ * @param {(entry: any) => string} lang
34
+ * @param {Site} site
35
+ * @returns {TaxonomyLinks}
36
+ */
37
+ export function taxonomyLinks(translations, lang, site) {
38
+ const category = lang(translations.articles.category) ?? 'category';
39
+ const categoryLink = `${site.subfolder}/${category}/`;
40
+
41
+ const tag = lang(translations.articles.tag) ?? 'category';
42
+ const tagLink = `${site.subfolder}/${tag}/`;
43
+
44
+ return {
45
+ tag: tag,
46
+ category: category,
47
+ getCategoryLink: (category) => {
48
+ return Urls.addSlashToAddress(categoryLink + category.toLowerCase().replace(/ /g, '-') + '/1/', site);
49
+ },
50
+ getTagLink: (tag) => {
51
+ return Urls.addSlashToAddress(tagLink + tag.toLowerCase().replace(/ /g, '-') + '/1/', site);
52
+ }
53
+ };
54
+ }
55
+
56
+ /**
57
+ *
58
+ * @returns {Promise<Taxonomy}
59
+ */
60
+ export async function getTaxonomy () {
61
+ const cacheKey = 'Global__taxonomy';
62
+
63
+ /** @type {Taxonomy} */
64
+ let taxonomy = await Cache.getItem(cacheKey);
65
+
66
+ if (taxonomy == null) {
67
+ /** @type {MarkdownInstance[]} */
68
+ const allPages = await PostQueries.getPages();
69
+
70
+ /** @type {{ [key: string]: number }} */
71
+ const tags = {};
72
+
73
+ /** @type {{ [key: string]: number }} */
74
+ const cats = {};
75
+
76
+ // Get taxonomy and counts
77
+ allPages.forEach((p) => {
78
+ p.frontmatter.tags && (p.frontmatter.tags).forEach(t => {
79
+ tags[t] = (tags[t] ?? 0) + 1;
80
+ });
81
+
82
+ p.frontmatter.categories && (p.frontmatter.categories).forEach(c => {
83
+ cats[c] = (cats[c] ?? 0) + 1;
84
+ });
85
+ });
86
+
87
+ // Map into the taxonomy
88
+ taxonomy = {
89
+ tags: Object.keys(tags).sort().map(x => {
90
+ return {
91
+ title: x,
92
+ count: tags[x]
93
+ };
94
+ }),
95
+ topTags: [],
96
+ categories: Object.keys(cats).sort().map(x => {
97
+ return {
98
+ title: x,
99
+ count: cats[x]
100
+ };
101
+ })
102
+ };
103
+
104
+ // Get a list of "top tags" by usage count
105
+ const length = Math.min(taxonomy.categories.length, taxonomy.tags.length);
106
+ taxonomy.topTags = taxonomy.tags
107
+ .sort((a, b) => b.count - a.count)
108
+ .slice(0, length)
109
+ .sort((a, b) => {
110
+ if ( a.title < b.title ){
111
+ return -1;
112
+ }
113
+ if ( a.title > b.title ){
114
+ return 1;
115
+ }
116
+ return 0;
117
+ });
118
+
119
+ await Cache.setItem(cacheKey, taxonomy);
120
+ }
121
+
122
+ return taxonomy;
123
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-accelerator-utils",
3
- "version": "0.0.34",
3
+ "version": "0.0.36",
4
4
  "description": "Astro utilities for Astro Accelerator.",
5
5
  "main": "index.mjs",
6
6
  "files": [
@@ -0,0 +1,15 @@
1
+ export interface TaxonomyEntry {
2
+ title: string;
3
+ count: number;
4
+ }
5
+ export interface Taxonomy {
6
+ tags: TaxonomyEntry[];
7
+ topTags: TaxonomyEntry[];
8
+ categories: TaxonomyEntry[];
9
+ }
10
+ export interface TaxonomyLinks {
11
+ tag: string;
12
+ category: string;
13
+ getCategoryLink: (category: string) => string;
14
+ getTagLink: (tag: string) => string;
15
+ }