astro-accelerator-utils 0.1.17 → 0.1.18

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
@@ -3,6 +3,7 @@ import { Cache } from "./lib/v1/cache.mjs";
3
3
  import { DateFormatter } from "./lib/v1/dates.mjs";
4
4
  import { Markdown } from "./lib/v1/markdown.mjs";
5
5
  import { Navigation } from "./lib/v1/navigation.mjs";
6
+ import { Paging } from "./lib/v1/paging.mjs";
6
7
  import { Posts } from "./lib/v1/posts.mjs";
7
8
  import { UrlFormatter } from "./lib/v1/urls.mjs";
8
9
  import * as PostFiltering from "./lib/postFiltering.mjs";
@@ -10,4 +11,4 @@ import * as PostOrdering from "./lib/postOrdering.mjs";
10
11
  import * as PostPaging from "./lib/postPaging.mjs";
11
12
  import * as FooterMenu from "./lib/footerMenu.mjs";
12
13
  import * as Taxonomy from "./lib/taxonomy.mjs";
13
- export { Authors, Cache, DateFormatter, Markdown, Navigation, Posts, UrlFormatter, PostFiltering, PostOrdering, PostPaging, FooterMenu, Taxonomy };
14
+ export { Authors, Cache, DateFormatter, Markdown, Navigation, Paging, Posts, UrlFormatter, PostFiltering, PostOrdering, PostPaging, FooterMenu, Taxonomy };
package/index.mjs CHANGED
@@ -3,6 +3,7 @@ import { Cache } from './lib/v1/cache.mjs';
3
3
  import { DateFormatter } from './lib/v1/dates.mjs';
4
4
  import { Markdown } from './lib/v1/markdown.mjs';
5
5
  import { Navigation } from './lib/v1/navigation.mjs';
6
+ import { Paging } from './lib/v1/paging.mjs';
6
7
  import { Posts } from './lib/v1/posts.mjs';
7
8
  import { UrlFormatter } from './lib/v1/urls.mjs';
8
9
 
@@ -18,6 +19,7 @@ export {
18
19
  DateFormatter,
19
20
  Markdown,
20
21
  Navigation,
22
+ Paging,
21
23
  Posts,
22
24
  UrlFormatter,
23
25
  PostFiltering,
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @typedef { import("../types/Link").Link } Link
3
+ */
4
+ export class Paging {
5
+ /**
6
+ * Provides a list of paging links, 1 ... 3 4 5 ... 7
7
+ * @param {number} limit
8
+ * @param {number} numberOfPages
9
+ * @param {number} currentPage
10
+ * @param {string} url
11
+ * @returns {Link[]}
12
+ */
13
+ links(limit: number, numberOfPages: number, currentPage: number, url: string): Link[];
14
+ }
15
+ export type Link = any;
@@ -0,0 +1,78 @@
1
+ /**
2
+ * @typedef { import("../types/Link").Link } Link
3
+ */
4
+
5
+ export class Paging {
6
+ /**
7
+ * Provides a list of paging links, 1 ... 3 4 5 ... 7
8
+ * @param {number} limit
9
+ * @param {number} numberOfPages
10
+ * @param {number} currentPage
11
+ * @param {string} url
12
+ * @returns {Link[]}
13
+ */
14
+ links (limit, numberOfPages, currentPage, url) {
15
+ /** @type {Link[]} */
16
+ const pageLinks = [];
17
+
18
+ let start = 0;
19
+ let end = numberOfPages;
20
+
21
+ if (numberOfPages > limit) {
22
+ start = (currentPage - (limit - 1) / 2) - 1;
23
+ if (start < 0) {
24
+ start = 0;
25
+ }
26
+
27
+ end = (start + limit);
28
+ if (end > numberOfPages) {
29
+ end = numberOfPages;
30
+ start = numberOfPages - limit;
31
+ }
32
+ }
33
+
34
+ if (start == 1) {
35
+ pageLinks.push({
36
+ title: '1',
37
+ url: url.replace('/' + currentPage, '/' + 1),
38
+ ariaCurrent: false,
39
+ class: ''
40
+ });
41
+ } else if (start > 1) {
42
+ pageLinks.push({
43
+ title: '1',
44
+ url: url.replace('/' + currentPage, '/' + 1),
45
+ ariaCurrent: false,
46
+ class: 'paging-collapse-after'
47
+ });
48
+ }
49
+
50
+ for (let i = start; i < end; i++) {
51
+ const userPage = i + 1;
52
+ pageLinks.push({
53
+ title: userPage.toString(),
54
+ url: url.replace('/' + currentPage, '/' + userPage),
55
+ ariaCurrent: userPage == currentPage ? 'page' : false,
56
+ class: ''
57
+ });
58
+ }
59
+
60
+ if (end < (numberOfPages - 1)) {
61
+ pageLinks.push({
62
+ title: numberOfPages.toString(),
63
+ url: url.replace('/' + currentPage, '/' + numberOfPages),
64
+ ariaCurrent: false,
65
+ class: 'paging-collapse-before'
66
+ });
67
+ } else if (end < numberOfPages) {
68
+ pageLinks.push({
69
+ title: numberOfPages.toString(),
70
+ url: url.replace('/' + currentPage, '/' + numberOfPages),
71
+ ariaCurrent: false,
72
+ class: ''
73
+ });
74
+ }
75
+
76
+ return pageLinks;
77
+ }
78
+ }
package/lib/v1/posts.mjs CHANGED
@@ -10,8 +10,8 @@ export class Posts {
10
10
  * @param {() => MarkdownInstance[]} [fetchAll]
11
11
  */
12
12
  constructor(cache, fetchAll) {
13
- /* istanbul ignore next */
14
13
  this.cache = cache;
14
+ /* istanbul ignore next */
15
15
  this.fetchAll = fetchAll ?? function() {
16
16
  return import.meta.glob("/src/pages/**/*.md", { eager: true });
17
17
  }
@@ -35,12 +35,12 @@ export class Posts {
35
35
  * @returns {MarkdownInstance[]}
36
36
  */
37
37
  root (subfolder) {
38
- console.log('SUBFOLDER IS', subfolder)
39
38
  const isRoot = subfolder.length == 0;
40
39
  const expectedDepth = isRoot ? 1 : 2;
41
40
 
42
41
  return this.all().filter(p => {
43
- const depth = (p.url ?? '/').split('/').length - 1;
42
+ const url = p.url ?? '/';
43
+ const depth = (url).split('/').length - 1;
44
44
  return depth == expectedDepth
45
45
  || (depth == (expectedDepth - 1) && p.file.includes(subfolder.toLowerCase() + '.md'));
46
46
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-accelerator-utils",
3
- "version": "0.1.17",
3
+ "version": "0.1.18",
4
4
  "description": "Astro utilities for Astro Accelerator.",
5
5
  "main": "index.mjs",
6
6
  "type": "module",