astro-accelerator-utils 0.3.7 → 0.3.8

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.
Files changed (42) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +19 -19
  3. package/index.d.mts +4 -4
  4. package/index.mjs +9 -9
  5. package/lib/postFiltering.d.mts +48 -48
  6. package/lib/postFiltering.mjs +116 -116
  7. package/lib/postOrdering.d.mts +32 -32
  8. package/lib/postOrdering.mjs +51 -51
  9. package/lib/v1/accelerator.d.mts +36 -36
  10. package/lib/v1/accelerator.mjs +70 -70
  11. package/lib/v1/authors.d.mts +28 -28
  12. package/lib/v1/authors.mjs +71 -71
  13. package/lib/v1/cache.d.mts +43 -43
  14. package/lib/v1/cache.mjs +99 -99
  15. package/lib/v1/dates.d.mts +19 -19
  16. package/lib/v1/dates.mjs +26 -26
  17. package/lib/v1/markdown.d.mts +20 -20
  18. package/lib/v1/markdown.mjs +45 -45
  19. package/lib/v1/navigation.d.mts +111 -111
  20. package/lib/v1/navigation.mjs +446 -427
  21. package/lib/v1/paging.d.mts +15 -15
  22. package/lib/v1/paging.mjs +77 -77
  23. package/lib/v1/posts.d.mts +26 -26
  24. package/lib/v1/posts.mjs +47 -47
  25. package/lib/v1/statistics-stub.d.mts +5 -5
  26. package/lib/v1/statistics-stub.mjs +9 -9
  27. package/lib/v1/statistics.d.mts +39 -39
  28. package/lib/v1/statistics.mjs +78 -78
  29. package/lib/v1/taxonomy.d.mts +62 -62
  30. package/lib/v1/taxonomy.mjs +144 -144
  31. package/lib/v1/urls.d.mts +40 -40
  32. package/lib/v1/urls.mjs +104 -104
  33. package/package.json +3 -3
  34. package/types/Astro.d.ts +19 -19
  35. package/types/AuthorList.d.ts +8 -8
  36. package/types/BannerImage.d.ts +4 -4
  37. package/types/Frontmatter.d.ts +30 -30
  38. package/types/Link.d.ts +6 -6
  39. package/types/NavPage.d.ts +22 -22
  40. package/types/PagePredicate.d.ts +2 -2
  41. package/types/Site.d.ts +43 -43
  42. package/types/Taxonomy.d.ts +15 -15
@@ -1,72 +1,72 @@
1
- import * as PostFiltering from '../postFiltering.mjs';
2
-
3
- /**
4
- * @typedef { import("../../types/AuthorList").AuthorList } AuthorList
5
- * @typedef { import("../../types/Astro").MarkdownInstance } MarkdownInstance
6
- * @typedef { import("../../types/Frontmatter").Frontmatter } Frontmatter
7
- */
8
-
9
- export class Authors {
10
- /**
11
- * Constructor
12
- * @param {Posts} posts
13
- */
14
- constructor(posts) {
15
- this.posts = posts;
16
- }
17
-
18
- /**
19
- * Gets a list of authors, and exposes main author and contributors
20
- * @param {Frontmatter} frontmatter
21
- * @returns {AuthorList}
22
- */
23
- forPost (frontmatter) {
24
- const authors = this.posts.all()
25
- .filter(PostFiltering.isAuthor);
26
-
27
- /** @type {AuthorList} */
28
- const result = {
29
- image: null,
30
- writers: [],
31
- mainAuthor: null,
32
- contributors: []
33
- };
34
-
35
- (frontmatter.authors ?? []).forEach((a) => {
36
- const matches = authors.filter((x) => x.frontmatter.id == a);
37
-
38
- if (matches.length == 0) {
39
- console.warn("Unknown author", a);
40
- }
41
-
42
- if (matches.length > 1) {
43
- console.warn("Multiple authors with id", a);
44
- }
45
-
46
- if (matches.length == 1) {
47
- result.writers.push(matches[0]);
48
- if (result.image == null) {
49
- result.image = matches[0].frontmatter.bannerImage;
50
- }
51
- }
52
- });
53
-
54
- result.mainAuthor = result.writers.slice(0, 1)[0];
55
- result.contributors = result.writers.slice(1);
56
-
57
- return result;
58
- }
59
-
60
- /**
61
- * Get a single author by id/slug
62
- * @param {string} slug
63
- * @returns {MarkdownInstance}
64
- */
65
- info (slug) {
66
- const author = this.posts.all()
67
- .filter(PostFiltering.isAuthor)
68
- .filter(x => x.url?.split('/')[2] == slug)[0];
69
-
70
- return author;
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
+ * @typedef { import("../../types/Frontmatter").Frontmatter } Frontmatter
7
+ */
8
+
9
+ export class Authors {
10
+ /**
11
+ * Constructor
12
+ * @param {Posts} posts
13
+ */
14
+ constructor(posts) {
15
+ this.posts = posts;
16
+ }
17
+
18
+ /**
19
+ * Gets a list of authors, and exposes main author and contributors
20
+ * @param {Frontmatter} frontmatter
21
+ * @returns {AuthorList}
22
+ */
23
+ forPost (frontmatter) {
24
+ const authors = this.posts.all()
25
+ .filter(PostFiltering.isAuthor);
26
+
27
+ /** @type {AuthorList} */
28
+ const result = {
29
+ image: null,
30
+ writers: [],
31
+ mainAuthor: null,
32
+ contributors: []
33
+ };
34
+
35
+ (frontmatter.authors ?? []).forEach((a) => {
36
+ const matches = authors.filter((x) => x.frontmatter.id == a);
37
+
38
+ if (matches.length == 0) {
39
+ console.warn("Unknown author", a);
40
+ }
41
+
42
+ if (matches.length > 1) {
43
+ console.warn("Multiple authors with id", a);
44
+ }
45
+
46
+ if (matches.length == 1) {
47
+ result.writers.push(matches[0]);
48
+ if (result.image == null) {
49
+ result.image = matches[0].frontmatter.bannerImage;
50
+ }
51
+ }
52
+ });
53
+
54
+ result.mainAuthor = result.writers.slice(0, 1)[0];
55
+ result.contributors = result.writers.slice(1);
56
+
57
+ return result;
58
+ }
59
+
60
+ /**
61
+ * Get a single author by id/slug
62
+ * @param {string} slug
63
+ * @returns {MarkdownInstance}
64
+ */
65
+ info (slug) {
66
+ const author = this.posts.all()
67
+ .filter(PostFiltering.isAuthor)
68
+ .filter(x => x.url?.split('/')[2] == slug)[0];
69
+
70
+ return author;
71
+ }
72
72
  }
@@ -1,43 +1,43 @@
1
- export class Cache {
2
- /**
3
- * Constructor
4
- * @param {number} maxAge
5
- */
6
- constructor(maxAge: number);
7
- maxAge: number;
8
- /**
9
- * Gets an item from the cache, falls back to supplied function
10
- * @param {string} key
11
- * @param {() => any} func
12
- */
13
- get(key: string, func: () => any): any;
14
- /**
15
- * Gets an item from the cache
16
- * @param {string} key
17
- * @returns {Promise<any>}
18
- */
19
- getItem(key: string): Promise<any>;
20
- /**
21
- * Adds an item to the cache
22
- * @param {string} key
23
- * @param {any} value
24
- * @returns {Promise<void>}
25
- */
26
- setItem(key: string, value: any): Promise<void>;
27
- /**
28
- * Clears the cache
29
- * @returns {Promise<void>}
30
- */
31
- clear(): Promise<void>;
32
- /**
33
- * Get's the path of the cache files
34
- * @returns {string}
35
- */
36
- getCachePath(): string;
37
- /**
38
- * Gets the file path for a cache item
39
- * @param {string} key
40
- * @returns {string}
41
- */
42
- getItemPath(key: string): string;
43
- }
1
+ export class Cache {
2
+ /**
3
+ * Constructor
4
+ * @param {number} maxAge
5
+ */
6
+ constructor(maxAge: number);
7
+ maxAge: number;
8
+ /**
9
+ * Gets an item from the cache, falls back to supplied function
10
+ * @param {string} key
11
+ * @param {() => any} func
12
+ */
13
+ get(key: string, func: () => any): any;
14
+ /**
15
+ * Gets an item from the cache
16
+ * @param {string} key
17
+ * @returns {Promise<any>}
18
+ */
19
+ getItem(key: string): Promise<any>;
20
+ /**
21
+ * Adds an item to the cache
22
+ * @param {string} key
23
+ * @param {any} value
24
+ * @returns {Promise<void>}
25
+ */
26
+ setItem(key: string, value: any): Promise<void>;
27
+ /**
28
+ * Clears the cache
29
+ * @returns {Promise<void>}
30
+ */
31
+ clear(): Promise<void>;
32
+ /**
33
+ * Get's the path of the cache files
34
+ * @returns {string}
35
+ */
36
+ getCachePath(): string;
37
+ /**
38
+ * Gets the file path for a cache item
39
+ * @param {string} key
40
+ * @returns {string}
41
+ */
42
+ getItemPath(key: string): string;
43
+ }
package/lib/v1/cache.mjs CHANGED
@@ -1,100 +1,100 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import process from 'process';
4
-
5
- export class Cache {
6
- /**
7
- * Constructor
8
- * @param {number} maxAge
9
- */
10
- constructor(maxAge) {
11
- this.maxAge = maxAge;
12
- }
13
-
14
- /**
15
- * Gets an item from the cache, falls back to supplied function
16
- * @param {string} key
17
- * @param {() => any} func
18
- */
19
- get(key, func) {
20
- const cached = this.getItem(key);
21
-
22
- if (cached) {
23
- return cached;
24
- }
25
-
26
- const fetched = func();
27
-
28
- this.setItem(key, fetched);
29
-
30
- return fetched;
31
- }
32
-
33
- /**
34
- * Gets an item from the cache
35
- * @param {string} key
36
- * @returns {Promise<any>}
37
- */
38
- getItem (key) {
39
- const itemPath = this.getItemPath(key);
40
- try {
41
-
42
- const { mtime } = fs.statSync(itemPath);
43
-
44
- var date_time = new Date();
45
- let timeDifference = Math.abs((date_time.getTime() - mtime.getTime()) / 1000);
46
- if (timeDifference < this.maxAge) {
47
- console.log('Cache hit', key);
48
- const content = fs.readFileSync(itemPath).toString();
49
- return JSON.parse(content);
50
- }
51
- } catch{}
52
-
53
- console.log('Cache miss', key);
54
- return null;
55
- }
56
-
57
- /**
58
- * Adds an item to the cache
59
- * @param {string} key
60
- * @param {any} value
61
- * @returns {Promise<void>}
62
- */
63
- setItem (key, value) {
64
- const itemPath = this.getItemPath(key);
65
- fs.writeFileSync(itemPath, JSON.stringify(value));
66
- }
67
-
68
- /**
69
- * Clears the cache
70
- * @returns {Promise<void>}
71
- */
72
- clear() {
73
- const folder = this.getCachePath();
74
- const files = fs.readdirSync(folder);
75
-
76
- for(const file of files) {
77
- fs.unlinkSync(path.join(folder, file));
78
- }
79
- }
80
-
81
- /**
82
- * Get's the path of the cache files
83
- * @returns {string}
84
- */
85
- getCachePath () {
86
- const cachePath = path.join(process.cwd(), '.cache/');
87
- fs.mkdirSync(cachePath, { recursive: true })
88
- return cachePath;
89
- }
90
-
91
- /**
92
- * Gets the file path for a cache item
93
- * @param {string} key
94
- * @returns {string}
95
- */
96
- getItemPath (key) {
97
- const cachePath = this.getCachePath();
98
- return path.join(cachePath, key + '.json');
99
- }
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import process from 'process';
4
+
5
+ export class Cache {
6
+ /**
7
+ * Constructor
8
+ * @param {number} maxAge
9
+ */
10
+ constructor(maxAge) {
11
+ this.maxAge = maxAge;
12
+ }
13
+
14
+ /**
15
+ * Gets an item from the cache, falls back to supplied function
16
+ * @param {string} key
17
+ * @param {() => any} func
18
+ */
19
+ get(key, func) {
20
+ const cached = this.getItem(key);
21
+
22
+ if (cached) {
23
+ return cached;
24
+ }
25
+
26
+ const fetched = func();
27
+
28
+ this.setItem(key, fetched);
29
+
30
+ return fetched;
31
+ }
32
+
33
+ /**
34
+ * Gets an item from the cache
35
+ * @param {string} key
36
+ * @returns {Promise<any>}
37
+ */
38
+ getItem (key) {
39
+ const itemPath = this.getItemPath(key);
40
+ try {
41
+
42
+ const { mtime } = fs.statSync(itemPath);
43
+
44
+ var date_time = new Date();
45
+ let timeDifference = Math.abs((date_time.getTime() - mtime.getTime()) / 1000);
46
+ if (timeDifference < this.maxAge) {
47
+ console.log('Cache hit', key);
48
+ const content = fs.readFileSync(itemPath).toString();
49
+ return JSON.parse(content);
50
+ }
51
+ } catch{}
52
+
53
+ console.log('Cache miss', key);
54
+ return null;
55
+ }
56
+
57
+ /**
58
+ * Adds an item to the cache
59
+ * @param {string} key
60
+ * @param {any} value
61
+ * @returns {Promise<void>}
62
+ */
63
+ setItem (key, value) {
64
+ const itemPath = this.getItemPath(key);
65
+ fs.writeFileSync(itemPath, JSON.stringify(value));
66
+ }
67
+
68
+ /**
69
+ * Clears the cache
70
+ * @returns {Promise<void>}
71
+ */
72
+ clear() {
73
+ const folder = this.getCachePath();
74
+ const files = fs.readdirSync(folder);
75
+
76
+ for(const file of files) {
77
+ fs.unlinkSync(path.join(folder, file));
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Get's the path of the cache files
83
+ * @returns {string}
84
+ */
85
+ getCachePath () {
86
+ const cachePath = path.join(process.cwd(), '.cache/');
87
+ fs.mkdirSync(cachePath, { recursive: true })
88
+ return cachePath;
89
+ }
90
+
91
+ /**
92
+ * Gets the file path for a cache item
93
+ * @param {string} key
94
+ * @returns {string}
95
+ */
96
+ getItemPath (key) {
97
+ const cachePath = this.getCachePath();
98
+ return path.join(cachePath, key + '.json');
99
+ }
100
100
  }
@@ -1,19 +1,19 @@
1
- /**
2
- * @typedef { import("../types/Site") } Site
3
- */
4
- export class DateFormatter {
5
- /**
6
- * Constructor
7
- * @param {Intl.DateTimeFormatOptions} dateOptions
8
- */
9
- constructor(dateOptions: Intl.DateTimeFormatOptions);
10
- dateOptions: Intl.DateTimeFormatOptions;
11
- /**
12
- * Returns the formatted pubDate
13
- * @param {string | Date} date
14
- * @param {string} lang
15
- * @returns {string}
16
- */
17
- formatDate(date: string | Date, lang: string): string;
18
- }
19
- export type Site = any;
1
+ /**
2
+ * @typedef { import("../types/Site") } Site
3
+ */
4
+ export class DateFormatter {
5
+ /**
6
+ * Constructor
7
+ * @param {Intl.DateTimeFormatOptions} dateOptions
8
+ */
9
+ constructor(dateOptions: Intl.DateTimeFormatOptions);
10
+ dateOptions: Intl.DateTimeFormatOptions;
11
+ /**
12
+ * Returns the formatted pubDate
13
+ * @param {string | Date} date
14
+ * @param {string} lang
15
+ * @returns {string}
16
+ */
17
+ formatDate(date: string | Date, lang: string): string;
18
+ }
19
+ export type Site = any;
package/lib/v1/dates.mjs CHANGED
@@ -1,27 +1,27 @@
1
- /**
2
- * @typedef { import("../types/Site") } Site
3
- */
4
-
5
- export class DateFormatter {
6
- /**
7
- * Constructor
8
- * @param {Intl.DateTimeFormatOptions} dateOptions
9
- */
10
- constructor(dateOptions) {
11
- this.dateOptions = dateOptions;
12
- }
13
-
14
- /**
15
- * Returns the formatted pubDate
16
- * @param {string | Date} date
17
- * @param {string} lang
18
- * @returns {string}
19
- */
20
- formatDate(date, lang) {
21
- if (date) {
22
- return new Date(date).toLocaleDateString(lang, this.dateOptions);
23
- }
24
-
25
- return '';
26
- }
1
+ /**
2
+ * @typedef { import("../types/Site") } Site
3
+ */
4
+
5
+ export class DateFormatter {
6
+ /**
7
+ * Constructor
8
+ * @param {Intl.DateTimeFormatOptions} dateOptions
9
+ */
10
+ constructor(dateOptions) {
11
+ this.dateOptions = dateOptions;
12
+ }
13
+
14
+ /**
15
+ * Returns the formatted pubDate
16
+ * @param {string | Date} date
17
+ * @param {string} lang
18
+ * @returns {string}
19
+ */
20
+ formatDate(date, lang) {
21
+ if (date) {
22
+ return new Date(date).toLocaleDateString(lang, this.dateOptions);
23
+ }
24
+
25
+ return '';
26
+ }
27
27
  }
@@ -1,20 +1,20 @@
1
- export class Markdown {
2
- /**
3
- * Converts markdown to HTML without wrapping in a <p>
4
- * @param {string} markdown
5
- * @returns {Promise<string>}
6
- */
7
- getInlineHtmlFrom(markdown: string): Promise<string>;
8
- /**
9
- * Converts markdown to HTML
10
- * @param {string} markdown
11
- * @returns {Promise<string>}
12
- */
13
- getHtmlFrom(markdown: string): Promise<string>;
14
- /**
15
- * Converts markdown to plain text
16
- * @param {string} markdown
17
- * @returns {Promise<string>}
18
- */
19
- getTextFrom(markdown: string): Promise<string>;
20
- }
1
+ export class Markdown {
2
+ /**
3
+ * Converts markdown to HTML without wrapping in a <p>
4
+ * @param {string} markdown
5
+ * @returns {Promise<string>}
6
+ */
7
+ getInlineHtmlFrom(markdown: string): Promise<string>;
8
+ /**
9
+ * Converts markdown to HTML
10
+ * @param {string} markdown
11
+ * @returns {Promise<string>}
12
+ */
13
+ getHtmlFrom(markdown: string): Promise<string>;
14
+ /**
15
+ * Converts markdown to plain text
16
+ * @param {string} markdown
17
+ * @returns {Promise<string>}
18
+ */
19
+ getTextFrom(markdown: string): Promise<string>;
20
+ }
@@ -1,46 +1,46 @@
1
- import { unified } from 'unified';
2
- import remarkParse from 'remark-parse';
3
- import remarkRehype from 'remark-rehype'
4
- import rehypeStringify from 'rehype-stringify';
5
-
6
- export class Markdown {
7
- /**
8
- * Converts markdown to HTML without wrapping in a <p>
9
- * @param {string} markdown
10
- * @returns {Promise<string>}
11
- */
12
- async getInlineHtmlFrom(markdown) {
13
- let html = await this.getHtmlFrom(markdown);
14
-
15
- // There may be a better way to unwrap this... maybe a visitor
16
- if (html.substring(0, 3) == '<p>' && html.substring(html.length - 4) == '</p>') {
17
- html = html.substring(3, html.length - 4);
18
- }
19
-
20
- return html;
21
- }
22
-
23
- /**
24
- * Converts markdown to HTML
25
- * @param {string} markdown
26
- * @returns {Promise<string>}
27
- */
28
- async getHtmlFrom(markdown) {
29
- const vfile = await unified()
30
- .use(remarkParse)
31
- .use(remarkRehype)
32
- .use(rehypeStringify)
33
- .process(markdown)
34
-
35
- return String(vfile);
36
- }
37
-
38
- /**
39
- * Converts markdown to plain text
40
- * @param {string} markdown
41
- * @returns {Promise<string>}
42
- */
43
- async getTextFrom(markdown) {
44
- return markdown.replace(/[\\*>~]/g, '').trim();
45
- }
1
+ import { unified } from 'unified';
2
+ import remarkParse from 'remark-parse';
3
+ import remarkRehype from 'remark-rehype'
4
+ import rehypeStringify from 'rehype-stringify';
5
+
6
+ export class Markdown {
7
+ /**
8
+ * Converts markdown to HTML without wrapping in a <p>
9
+ * @param {string} markdown
10
+ * @returns {Promise<string>}
11
+ */
12
+ async getInlineHtmlFrom(markdown) {
13
+ let html = await this.getHtmlFrom(markdown);
14
+
15
+ // There may be a better way to unwrap this... maybe a visitor
16
+ if (html.substring(0, 3) == '<p>' && html.substring(html.length - 4) == '</p>') {
17
+ html = html.substring(3, html.length - 4);
18
+ }
19
+
20
+ return html;
21
+ }
22
+
23
+ /**
24
+ * Converts markdown to HTML
25
+ * @param {string} markdown
26
+ * @returns {Promise<string>}
27
+ */
28
+ async getHtmlFrom(markdown) {
29
+ const vfile = await unified()
30
+ .use(remarkParse)
31
+ .use(remarkRehype)
32
+ .use(rehypeStringify)
33
+ .process(markdown)
34
+
35
+ return String(vfile);
36
+ }
37
+
38
+ /**
39
+ * Converts markdown to plain text
40
+ * @param {string} markdown
41
+ * @returns {Promise<string>}
42
+ */
43
+ async getTextFrom(markdown) {
44
+ return markdown.replace(/[\\*>~]/g, '').trim();
45
+ }
46
46
  }