astro-accelerator-utils 0.0.5 → 0.0.7

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/dist/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
- export type { Site } from './Site';
2
- export type { Frontmatter } from './Frontmatter';
1
+ export type { Frontmatter } from './types/Frontmatter';
2
+ export type { PagePredicate } from './types/PagePredicate';
3
+ export type { Site } from './types/Site';
4
+ export { getCachePath, getItem, getItemPath, setItem } from './lib/Cache';
3
5
  export { formatDate, formatModifiedDate } from './lib/dates';
4
6
  export { isAuthor, isListable, isSearch, showInMenu, showInSearch, showInSitemap } from './lib/postFiltering';
5
7
  export { sortByModDate, sortByPubDate, sortByPubDateDesc } from './lib/postOrdering';
8
+ export { getPages } from './lib/postQueries';
6
9
  export { addSlashToAddress, addSlashToUrl } from './lib/urls';
package/dist/index.js CHANGED
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.addSlashToUrl = exports.addSlashToAddress = exports.sortByPubDateDesc = exports.sortByPubDate = exports.sortByModDate = exports.showInSitemap = exports.showInSearch = exports.showInMenu = exports.isSearch = exports.isListable = exports.isAuthor = exports.formatModifiedDate = exports.formatDate = void 0;
3
+ exports.addSlashToUrl = exports.addSlashToAddress = exports.getPages = exports.sortByPubDateDesc = exports.sortByPubDate = exports.sortByModDate = exports.showInSitemap = exports.showInSearch = exports.showInMenu = exports.isSearch = exports.isListable = exports.isAuthor = exports.formatModifiedDate = exports.formatDate = exports.setItem = exports.getItemPath = exports.getItem = exports.getCachePath = void 0;
4
+ var Cache_1 = require("./lib/Cache");
5
+ Object.defineProperty(exports, "getCachePath", { enumerable: true, get: function () { return Cache_1.getCachePath; } });
6
+ Object.defineProperty(exports, "getItem", { enumerable: true, get: function () { return Cache_1.getItem; } });
7
+ Object.defineProperty(exports, "getItemPath", { enumerable: true, get: function () { return Cache_1.getItemPath; } });
8
+ Object.defineProperty(exports, "setItem", { enumerable: true, get: function () { return Cache_1.setItem; } });
4
9
  var dates_1 = require("./lib/dates");
5
10
  Object.defineProperty(exports, "formatDate", { enumerable: true, get: function () { return dates_1.formatDate; } });
6
11
  Object.defineProperty(exports, "formatModifiedDate", { enumerable: true, get: function () { return dates_1.formatModifiedDate; } });
@@ -15,6 +20,8 @@ var postOrdering_1 = require("./lib/postOrdering");
15
20
  Object.defineProperty(exports, "sortByModDate", { enumerable: true, get: function () { return postOrdering_1.sortByModDate; } });
16
21
  Object.defineProperty(exports, "sortByPubDate", { enumerable: true, get: function () { return postOrdering_1.sortByPubDate; } });
17
22
  Object.defineProperty(exports, "sortByPubDateDesc", { enumerable: true, get: function () { return postOrdering_1.sortByPubDateDesc; } });
23
+ var postQueries_1 = require("./lib/postQueries");
24
+ Object.defineProperty(exports, "getPages", { enumerable: true, get: function () { return postQueries_1.getPages; } });
18
25
  var urls_1 = require("./lib/urls");
19
26
  Object.defineProperty(exports, "addSlashToAddress", { enumerable: true, get: function () { return urls_1.addSlashToAddress; } });
20
27
  Object.defineProperty(exports, "addSlashToUrl", { enumerable: true, get: function () { return urls_1.addSlashToUrl; } });
@@ -0,0 +1,5 @@
1
+ export declare const maxAge = 200;
2
+ export declare function getCachePath(): Promise<string>;
3
+ export declare function getItemPath(key: string): Promise<string>;
4
+ export declare function getItem(key: string): Promise<any>;
5
+ export declare function setItem(key: string, value: object): Promise<void>;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.setItem = exports.getItem = exports.getItemPath = exports.getCachePath = exports.maxAge = void 0;
16
+ const fs_1 = __importDefault(require("fs"));
17
+ const path_1 = __importDefault(require("path"));
18
+ const process_1 = __importDefault(require("process"));
19
+ exports.maxAge = 200; //seconds
20
+ function getCachePath() {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ const cachePath = path_1.default.join(process_1.default.cwd(), '.cache/');
23
+ yield fs_1.default.promises.mkdir(cachePath, { recursive: true });
24
+ return cachePath;
25
+ });
26
+ }
27
+ exports.getCachePath = getCachePath;
28
+ function getItemPath(key) {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ const cachePath = yield getCachePath();
31
+ return path_1.default.join(cachePath, key + '.cache');
32
+ });
33
+ }
34
+ exports.getItemPath = getItemPath;
35
+ function getItem(key) {
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ const itemPath = yield getItemPath(key);
38
+ try {
39
+ const { mtime } = yield fs_1.default.promises.stat(itemPath);
40
+ var date_time = new Date();
41
+ let timeDifference = Math.abs((date_time.getTime() - mtime.getTime()) / 1000);
42
+ if (timeDifference < exports.maxAge) {
43
+ console.log('Cache hit', key);
44
+ const content = fs_1.default.readFileSync(itemPath).toString();
45
+ return JSON.parse(content);
46
+ }
47
+ }
48
+ catch (_a) { }
49
+ console.warn('Cache miss', key);
50
+ return null;
51
+ });
52
+ }
53
+ exports.getItem = getItem;
54
+ function setItem(key, value) {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ const itemPath = yield getItemPath(key);
57
+ yield fs_1.default.promises.writeFile(itemPath, JSON.stringify(value));
58
+ });
59
+ }
60
+ exports.setItem = setItem;
@@ -1,4 +1,4 @@
1
- import type { Site } from '../Site';
2
- import type { Frontmatter } from '../Frontmatter';
1
+ import type { Site } from '../types/Site';
2
+ import type { Frontmatter } from '../types/Frontmatter';
3
3
  export declare function formatDate(frontmatter: Frontmatter, lang: string, site: Site): string;
4
4
  export declare function formatModifiedDate(frontmatter: Frontmatter, lang: string, site: Site): string;
@@ -1,6 +1,7 @@
1
- export declare function showInSitemap(p: any): boolean;
2
- export declare function showInSearch(p: any): boolean;
3
- export declare function showInMenu(p: any): boolean;
4
- export declare function isAuthor(p: any): boolean;
5
- export declare function isSearch(p: any): boolean;
6
- export declare function isListable(p: any): boolean;
1
+ import { Markdown } from '../types/Markdown';
2
+ export declare function showInSitemap(p: Markdown<Record<string, any>>): boolean;
3
+ export declare function showInSearch(p: Markdown<Record<string, any>>): boolean;
4
+ export declare function showInMenu(p: Markdown<Record<string, any>>): boolean;
5
+ export declare function isAuthor(p: Markdown<Record<string, any>>): boolean;
6
+ export declare function isSearch(p: Markdown<Record<string, any>>): boolean;
7
+ export declare function isListable(p: Markdown<Record<string, any>>): boolean;
@@ -1,3 +1,4 @@
1
- export declare function sortByPubDate(a: any, b: any): any;
2
- export declare function sortByPubDateDesc(a: any, b: any): any;
3
- export declare function sortByModDate(a: any, b: any): any;
1
+ import { Markdown } from "../types/Markdown";
2
+ export declare function sortByPubDate(a: Markdown<Record<string, any>>, b: Markdown<Record<string, any>>): any;
3
+ export declare function sortByPubDateDesc(a: Markdown<Record<string, any>>, b: Markdown<Record<string, any>>): any;
4
+ export declare function sortByModDate(a: Markdown<Record<string, any>>, b: Markdown<Record<string, any>>): any;
@@ -0,0 +1,3 @@
1
+ import { Markdown } from "../types/Markdown";
2
+ import { PagePredicate } from "../types/PagePredicate";
3
+ export declare function getPages(fetchPages: () => Record<string, any>, filter?: PagePredicate | null): Promise<Markdown<Record<string, any>>[]>;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.getPages = void 0;
13
+ const Cache_1 = require("./Cache");
14
+ function getPages(fetchPages, filter) {
15
+ return __awaiter(this, void 0, void 0, function* () {
16
+ const key = 'PageQueries__getPages';
17
+ let allPages = yield (0, Cache_1.getItem)(key);
18
+ if (allPages == null) {
19
+ const pageImportResult = fetchPages();
20
+ allPages = Object.values(pageImportResult);
21
+ yield (0, Cache_1.setItem)(key, allPages);
22
+ }
23
+ if (filter == null) {
24
+ return allPages;
25
+ }
26
+ return allPages.filter(filter);
27
+ });
28
+ }
29
+ exports.getPages = getPages;
@@ -1,3 +1,3 @@
1
- import type { Site } from '../Site';
1
+ import type { Site } from '../types/Site';
2
2
  export declare function addSlashToUrl(url: URL): URL;
3
3
  export declare function addSlashToAddress(address: string | undefined, site: Site): string;
@@ -0,0 +1 @@
1
+ export type Frontmatter = Record<string, any>;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ export interface Markdown<T extends Record<string, any>> {
2
+ url: string;
3
+ frontmatter: T;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ import { Markdown } from "./Markdown";
2
+ export type PagePredicate = (value: Markdown<Record<string, any>>, index: number, array: Markdown<Record<string, any>>[]) => boolean;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,34 @@
1
+ export type Site = {
2
+ url: string;
3
+ dateOptions: Intl.DateTimeFormatOptions;
4
+ subfolder: string;
5
+ feedUrl: string;
6
+ title: string;
7
+ description: string;
8
+ defaultLanguage: string;
9
+ themeColor: string;
10
+ owner: string;
11
+ default: {
12
+ lang: string;
13
+ locale: string;
14
+ dir: string;
15
+ };
16
+ search: {
17
+ fallbackUrl: 'https://www.google.com/search' | string;
18
+ fallbackSite: 'q' | string;
19
+ fallbackQuery: 'q' | string;
20
+ };
21
+ pageSize: number;
22
+ pageLinks: number;
23
+ rssLimit: number;
24
+ featureFlags: {
25
+ codeBlocks: 'copy'[];
26
+ figures: 'enlarge'[];
27
+ youTubeLinks: 'embed'[];
28
+ };
29
+ images: {
30
+ contentSize: string;
31
+ listerSize: string;
32
+ authorSize: string;
33
+ };
34
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-accelerator-utils",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "Astro utilities for Astro Accelerator.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -25,6 +25,7 @@
25
25
  },
26
26
  "homepage": "https://astro.stevefenton.co.uk/",
27
27
  "devDependencies": {
28
- "typescript": "^4.9.3"
28
+ "typescript": "^4.9.3",
29
+ "@types/node": "^18.11.9"
29
30
  }
30
31
  }