astro-accelerator-utils 0.2.19 → 0.2.21

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.
@@ -10,6 +10,7 @@ export class Accelerator {
10
10
  dateOptions: Intl.DateTimeFormatOptions;
11
11
  siteUrl: string;
12
12
  subfolder: string;
13
+ useTrailingUrlSlash: boolean;
13
14
  get authors(): Authors;
14
15
  get cache(): Cache;
15
16
  get dateFormatter(): DateFormatter;
@@ -21,6 +21,7 @@ export class Accelerator {
21
21
  this.dateOptions = site.dateOptions;
22
22
  this.siteUrl = site.url;
23
23
  this.subfolder = site.subfolder;
24
+ this.useTrailingUrlSlash = site.useTrailingUrlSlash;
24
25
  }
25
26
 
26
27
  get authors() {
@@ -56,6 +57,6 @@ export class Accelerator {
56
57
  }
57
58
 
58
59
  get urlFormatter() {
59
- return new UrlFormatter(this.siteUrl, this.subfolder)
60
+ return new UrlFormatter(this.siteUrl, this.subfolder, this.useTrailingUrlSlash)
60
61
  }
61
62
  }
package/lib/v1/urls.d.mts CHANGED
@@ -3,10 +3,23 @@ export class UrlFormatter {
3
3
  * Constructor
4
4
  * @param {string} siteUrl
5
5
  * @param {string} subfolder
6
+ * @param {boolean} useTrailingUrlSlash
6
7
  */
7
- constructor(siteUrl: string, subfolder: string);
8
+ constructor(siteUrl: string, subfolder: string, useTrailingUrlSlash: boolean);
8
9
  siteUrl: string;
9
10
  subfolder: string;
11
+ useTrailingUrlSlash: boolean;
12
+ /** Uses config to decide whether to add or remove trailing slashes
13
+ * @param {URL} url
14
+ * @returns {URL}
15
+ */
16
+ formatUrl(url: URL): URL;
17
+ /**
18
+ * Ensures trailing slash is used
19
+ * @param {string | undefined} address
20
+ * @returns {string}
21
+ */
22
+ formatAddress(address: string | undefined): string;
10
23
  /**
11
24
  * Ensures trailing slash is used
12
25
  * @param {URL} url
package/lib/v1/urls.mjs CHANGED
@@ -3,10 +3,56 @@ export class UrlFormatter {
3
3
  * Constructor
4
4
  * @param {string} siteUrl
5
5
  * @param {string} subfolder
6
+ * @param {boolean} useTrailingUrlSlash
6
7
  */
7
- constructor(siteUrl, subfolder) {
8
+ constructor(siteUrl, subfolder, useTrailingUrlSlash) {
8
9
  this.siteUrl = siteUrl;
9
10
  this.subfolder = subfolder;
11
+ this.useTrailingUrlSlash = useTrailingUrlSlash;
12
+ }
13
+
14
+ /** Uses config to decide whether to add or remove trailing slashes
15
+ * @param {URL} url
16
+ * @returns {URL}
17
+ */
18
+ formatUrl(url) {
19
+ if (!url) {
20
+ return new URL(this.siteUrl);
21
+ }
22
+
23
+ if (this.useTrailingUrlSlash) {
24
+ // Add slash
25
+ url.pathname += url.pathname.endsWith('/')
26
+ ? ''
27
+ : '/';
28
+ } else {
29
+ // Remove slash
30
+ if (url.pathname.endsWith('/')) {
31
+ url.pathname = url.pathname.substring(0, url.pathname.length - 1);
32
+ }
33
+ }
34
+
35
+ return url;
36
+ }
37
+
38
+ /**
39
+ * Ensures trailing slash is used
40
+ * @param {string | undefined} address
41
+ * @returns {string}
42
+ */
43
+ formatAddress(address) {
44
+ if (!address) {
45
+ // Handle null or empty addresses
46
+ address = '/';
47
+ }
48
+
49
+ if (address.indexOf('//') > -1) {
50
+ // Don't mess with absolute addresses
51
+ return address;
52
+ }
53
+
54
+ const url = this.formatUrl(new URL(address, this.siteUrl));
55
+ return url.pathname + url.search;
10
56
  }
11
57
 
12
58
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-accelerator-utils",
3
- "version": "0.2.19",
3
+ "version": "0.2.21",
4
4
  "description": "Astro utilities for Astro Accelerator.",
5
5
  "main": "index.mjs",
6
6
  "type": "module",
package/types/Site.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export type Site = {
2
2
  url: string;
3
+ useTrailingUrlSlash?: boolean;
3
4
  dateOptions: Intl.DateTimeFormatOptions;
4
5
  subfolder: string;
5
6
  feedUrl: string;
@@ -30,6 +31,13 @@ export type Site = {
30
31
  figures: 'enlarge'[];
31
32
  youTubeLinks: 'embed'[];
32
33
  };
34
+ layouts: {
35
+ default: string;
36
+ category?: string;
37
+ tag?: string;
38
+ author?: string;
39
+ page?: string;
40
+ };
33
41
  images: {
34
42
  contentSize: string;
35
43
  listerSize: string;