astro-accelerator-utils 0.1.0 → 0.1.2
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 +3 -1
- package/index.mjs +3 -0
- package/lib/cache.d.mts +30 -0
- package/lib/cache.mjs +76 -0
- package/lib/footerMenu.d.mts +2 -2
- package/lib/footerMenu.mjs +1 -1
- package/lib/postQueries.mjs +4 -2
- package/lib/taxonomy.mjs +4 -3
- package/lib/v1/urls.d.mts +2 -2
- package/lib/v1/urls.mjs +1 -1
- package/package.json +1 -1
- package/lib/urls.d.mts +0 -17
- package/lib/urls.mjs +0 -34
package/index.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { DateFormatter } from "./lib/v1/dates.mjs";
|
|
2
2
|
import { UrlFormatter } from "./lib/v1/urls.mjs";
|
|
3
|
+
import { Posts } from "./lib/v1/posts.mjs";
|
|
4
|
+
import * as Cache from "./lib/cache.mjs";
|
|
3
5
|
import * as PostQueries from "./lib/postQueries.mjs";
|
|
4
6
|
import * as PostFiltering from "./lib/postFiltering.mjs";
|
|
5
7
|
import * as PostOrdering from "./lib/postOrdering.mjs";
|
|
@@ -8,4 +10,4 @@ import * as FooterMenu from "./lib/footerMenu.mjs";
|
|
|
8
10
|
import * as Markdown from "./lib/markdown.mjs";
|
|
9
11
|
import * as Navigation from "./lib/navigation.mjs";
|
|
10
12
|
import * as Taxonomy from "./lib/taxonomy.mjs";
|
|
11
|
-
export { DateFormatter, UrlFormatter, PostQueries, PostFiltering, PostOrdering, PostPaging, FooterMenu, Markdown, Navigation, Taxonomy };
|
|
13
|
+
export { DateFormatter, UrlFormatter, Posts, Cache, PostQueries, PostFiltering, PostOrdering, PostPaging, FooterMenu, Markdown, Navigation, Taxonomy };
|
package/index.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import { DateFormatter } from './lib/v1/dates.mjs';
|
|
|
2
2
|
import { UrlFormatter } from './lib/v1/urls.mjs';
|
|
3
3
|
import { Posts } from './lib/v1/posts.mjs';
|
|
4
4
|
|
|
5
|
+
import * as Cache from './lib/cache.mjs';
|
|
5
6
|
import * as PostQueries from './lib/postQueries.mjs';
|
|
6
7
|
import * as PostFiltering from './lib/postFiltering.mjs';
|
|
7
8
|
import * as PostOrdering from './lib/postOrdering.mjs';
|
|
@@ -14,6 +15,8 @@ import * as Taxonomy from './lib/taxonomy.mjs';
|
|
|
14
15
|
export {
|
|
15
16
|
DateFormatter,
|
|
16
17
|
UrlFormatter,
|
|
18
|
+
Posts,
|
|
19
|
+
Cache,
|
|
17
20
|
PostQueries,
|
|
18
21
|
PostFiltering,
|
|
19
22
|
PostOrdering,
|
package/lib/cache.d.mts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get's the path of the cache files
|
|
3
|
+
* @returns {Promise<string>}
|
|
4
|
+
*/
|
|
5
|
+
export function getCachePath(): Promise<string>;
|
|
6
|
+
/**
|
|
7
|
+
* Gets the file path for a cache item
|
|
8
|
+
* @param {string} key
|
|
9
|
+
* @returns {Promise<string>}
|
|
10
|
+
*/
|
|
11
|
+
export function getItemPath(key: string): Promise<string>;
|
|
12
|
+
/**
|
|
13
|
+
* Gets an item from the cache
|
|
14
|
+
* @param {string} key
|
|
15
|
+
* @param {number} [maxAgeInSeconds]
|
|
16
|
+
* @returns {Promise<any>}
|
|
17
|
+
*/
|
|
18
|
+
export function getItem(key: string, maxAgeInSeconds?: number): Promise<any>;
|
|
19
|
+
/**
|
|
20
|
+
* Adds an item to the cache
|
|
21
|
+
* @param {string} key
|
|
22
|
+
* @param {any} value
|
|
23
|
+
* @returns {Promise<void>}
|
|
24
|
+
*/
|
|
25
|
+
export function setItem(key: string, value: any): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Clears the cache
|
|
28
|
+
* @returns {Promise<void>}
|
|
29
|
+
*/
|
|
30
|
+
export function clear(): Promise<void>;
|
package/lib/cache.mjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import process from 'process';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Get's the path of the cache files
|
|
7
|
+
* @returns {Promise<string>}
|
|
8
|
+
*/
|
|
9
|
+
export async function getCachePath () {
|
|
10
|
+
const cachePath = path.join(process.cwd(), '.cache/');
|
|
11
|
+
await fs.promises.mkdir(cachePath, { recursive: true })
|
|
12
|
+
return cachePath;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Gets the file path for a cache item
|
|
17
|
+
* @param {string} key
|
|
18
|
+
* @returns {Promise<string>}
|
|
19
|
+
*/
|
|
20
|
+
export async function getItemPath (key) {
|
|
21
|
+
const cachePath = await getCachePath();
|
|
22
|
+
return path.join(cachePath, key + '.json');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Gets an item from the cache
|
|
27
|
+
* @param {string} key
|
|
28
|
+
* @param {number} [maxAgeInSeconds]
|
|
29
|
+
* @returns {Promise<any>}
|
|
30
|
+
*/
|
|
31
|
+
export async function getItem (key, maxAgeInSeconds) {
|
|
32
|
+
if (maxAgeInSeconds == null) {
|
|
33
|
+
maxAgeInSeconds = 200;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const itemPath = await getItemPath(key);
|
|
37
|
+
try {
|
|
38
|
+
|
|
39
|
+
const { mtime } = await fs.promises.stat(itemPath);
|
|
40
|
+
|
|
41
|
+
var date_time = new Date();
|
|
42
|
+
let timeDifference = Math.abs((date_time.getTime() - mtime.getTime()) / 1000);
|
|
43
|
+
if (timeDifference < maxAgeInSeconds) {
|
|
44
|
+
console.log('Cache hit', key);
|
|
45
|
+
const content = fs.readFileSync(itemPath).toString();
|
|
46
|
+
return JSON.parse(content);
|
|
47
|
+
}
|
|
48
|
+
} catch{}
|
|
49
|
+
|
|
50
|
+
console.log('Cache miss', key);
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Adds an item to the cache
|
|
56
|
+
* @param {string} key
|
|
57
|
+
* @param {any} value
|
|
58
|
+
* @returns {Promise<void>}
|
|
59
|
+
*/
|
|
60
|
+
export async function setItem (key, value) {
|
|
61
|
+
const itemPath = await getItemPath(key);
|
|
62
|
+
await fs.promises.writeFile(itemPath, JSON.stringify(value));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Clears the cache
|
|
67
|
+
* @returns {Promise<void>}
|
|
68
|
+
*/
|
|
69
|
+
export async function clear() {
|
|
70
|
+
const folder = await getCachePath();
|
|
71
|
+
const files = fs.readdirSync(folder);
|
|
72
|
+
|
|
73
|
+
for(const file of files) {
|
|
74
|
+
fs.unlinkSync(path.join(folder, file));
|
|
75
|
+
}
|
|
76
|
+
}
|
package/lib/footerMenu.d.mts
CHANGED
|
@@ -12,9 +12,9 @@
|
|
|
12
12
|
* @param {any} translations
|
|
13
13
|
* @param {Site} site
|
|
14
14
|
* @param {(NavPage | 'categories' | 'tags' | 'toptags')[]} menu
|
|
15
|
-
* @returns {NavPage[]}
|
|
15
|
+
* @returns {Promise<NavPage[]>}
|
|
16
16
|
*/
|
|
17
|
-
export function getMenu(currentUrl: URL, _: TranslationProvider, translations: any, site: any, menu: (NavPage | 'categories' | 'tags' | 'toptags')[]): NavPage[]
|
|
17
|
+
export function getMenu(currentUrl: URL, _: TranslationProvider, translations: any, site: any, menu: (NavPage | 'categories' | 'tags' | 'toptags')[]): Promise<NavPage[]>;
|
|
18
18
|
/**
|
|
19
19
|
*
|
|
20
20
|
* @param {TaxonomyLinks} links
|
package/lib/footerMenu.mjs
CHANGED
|
@@ -17,7 +17,7 @@ import * as PostQueries from './postQueries.mjs';
|
|
|
17
17
|
* @param {any} translations
|
|
18
18
|
* @param {Site} site
|
|
19
19
|
* @param {(NavPage | 'categories' | 'tags' | 'toptags')[]} menu
|
|
20
|
-
* @returns {NavPage[]}
|
|
20
|
+
* @returns {Promise<NavPage[]>}
|
|
21
21
|
*/
|
|
22
22
|
export async function getMenu (currentUrl, _, translations, site, menu) {
|
|
23
23
|
const links = Taxonomy.taxonomyLinks(translations, _, site);
|
package/lib/postQueries.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getItem, setItem } from './cache.mjs';
|
|
2
2
|
import * as PostFiltering from './postFiltering.mjs';
|
|
3
|
-
import
|
|
3
|
+
import { UrlFormatter } from './v1/urls.mjs';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @typedef { import("../types/PagePredicate").PagePredicate } PagePredicate
|
|
@@ -189,6 +189,8 @@ export async function getBreadcrumbs (currentUrl, site) {
|
|
|
189
189
|
*/
|
|
190
190
|
export function mapNavPage (page, site) {
|
|
191
191
|
|
|
192
|
+
const urlFormatter = new UrlFormatter(site.url);
|
|
193
|
+
|
|
192
194
|
let url = page.url == null || (page.url ?? '').length == 0
|
|
193
195
|
? '/'
|
|
194
196
|
: page.url;
|
|
@@ -198,7 +200,7 @@ export function mapNavPage (page, site) {
|
|
|
198
200
|
url += '/1/';
|
|
199
201
|
}
|
|
200
202
|
|
|
201
|
-
url =
|
|
203
|
+
url = urlFormatter.addSlashToAddress(url);
|
|
202
204
|
|
|
203
205
|
if (page.frontmatter.layout == 'src/layouts/Redirect.astro') {
|
|
204
206
|
// Skips past the redirect
|
package/lib/taxonomy.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { UrlFormatter } from './v1/urls.mjs';
|
|
2
2
|
import * as Cache from './cache.mjs';
|
|
3
3
|
import * as PostQueries from './postQueries.mjs';
|
|
4
4
|
import * as PostFiltering from './postFiltering.mjs';
|
|
@@ -37,6 +37,7 @@ export function sortByTitle (a, b) {
|
|
|
37
37
|
* @returns {TaxonomyLinks}
|
|
38
38
|
*/
|
|
39
39
|
export function taxonomyLinks(translations, lang, site) {
|
|
40
|
+
const urlFormatter = new UrlFormatter(site.url);
|
|
40
41
|
const category = lang(translations.articles.category) ?? 'category';
|
|
41
42
|
const categoryLink = `${site.subfolder}/${category}/`;
|
|
42
43
|
|
|
@@ -47,10 +48,10 @@ export function taxonomyLinks(translations, lang, site) {
|
|
|
47
48
|
tag: tag,
|
|
48
49
|
category: category,
|
|
49
50
|
getCategoryLink: (category) => {
|
|
50
|
-
return
|
|
51
|
+
return urlFormatter.addSlashToAddress(categoryLink + category.toLowerCase().replace(/ /g, '-') + '/1/');
|
|
51
52
|
},
|
|
52
53
|
getTagLink: (tag) => {
|
|
53
|
-
return
|
|
54
|
+
return urlFormatter.addSlashToAddress(tagLink + tag.toLowerCase().replace(/ /g, '-') + '/1/');
|
|
54
55
|
}
|
|
55
56
|
};
|
|
56
57
|
}
|
package/lib/v1/urls.d.mts
CHANGED
|
@@ -13,8 +13,8 @@ export class UrlFormatter {
|
|
|
13
13
|
addSlashToUrl(url: URL): URL;
|
|
14
14
|
/**
|
|
15
15
|
* Ensures trailing slash is used
|
|
16
|
-
* @param {string |
|
|
16
|
+
* @param {string | undefined} address
|
|
17
17
|
* @returns {string}
|
|
18
18
|
*/
|
|
19
|
-
addSlashToAddress(address: string |
|
|
19
|
+
addSlashToAddress(address: string | undefined): string;
|
|
20
20
|
}
|
package/lib/v1/urls.mjs
CHANGED
package/package.json
CHANGED
package/lib/urls.d.mts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef { import("../types/Site") } Site
|
|
3
|
-
*/
|
|
4
|
-
/**
|
|
5
|
-
* Ensures trailing slash is used
|
|
6
|
-
* @param {URL} url
|
|
7
|
-
* @returns {URL}
|
|
8
|
-
*/
|
|
9
|
-
export function addSlashToUrl(url: URL): URL;
|
|
10
|
-
/**
|
|
11
|
-
* Ensures trailing slash is used
|
|
12
|
-
* @param {string | null} address
|
|
13
|
-
* @param {Site} site
|
|
14
|
-
* @returns {string}
|
|
15
|
-
*/
|
|
16
|
-
export function addSlashToAddress(address: string | null, site: Site): string;
|
|
17
|
-
export type Site = typeof import("../types/Site");
|
package/lib/urls.mjs
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef { import("../types/Site") } Site
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Ensures trailing slash is used
|
|
7
|
-
* @param {URL} url
|
|
8
|
-
* @returns {URL}
|
|
9
|
-
*/
|
|
10
|
-
export function addSlashToUrl(url) {
|
|
11
|
-
url.pathname += url.pathname.endsWith('/') ? '' : '/';
|
|
12
|
-
return url;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Ensures trailing slash is used
|
|
17
|
-
* @param {string | null} address
|
|
18
|
-
* @param {Site} site
|
|
19
|
-
* @returns {string}
|
|
20
|
-
*/
|
|
21
|
-
export function addSlashToAddress(address, site) {
|
|
22
|
-
if (!address) {
|
|
23
|
-
// Handle null or empty addresses
|
|
24
|
-
address = '/';
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (address.indexOf('//') > -1) {
|
|
28
|
-
// Don't mess with absolute addresses
|
|
29
|
-
return address;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const url = addSlashToUrl(new URL(address, site.url));
|
|
33
|
-
return url.pathname + url.search;
|
|
34
|
-
}
|