astro-accelerator-utils 0.0.34 → 0.0.35

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