astro-accelerator-utils 0.3.96 → 0.3.98

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.
@@ -103,6 +103,18 @@ export class Navigation {
103
103
  * @returns {item is NavPage}
104
104
  */
105
105
  isNavPage(item: NavPage | "auto" | "tags" | "toptags" | "categories"): item is NavPage;
106
+ /**
107
+ * Counts non-empty path segments so trailing slashes do not affect depth
108
+ * @param {string | null | undefined} path
109
+ * @returns {number}
110
+ */
111
+ pathDepth(path: string | null | undefined): number;
112
+ /**
113
+ * Normalizes a path for comparison by joining non-empty segments
114
+ * @param {string | null | undefined} path
115
+ * @returns {string}
116
+ */
117
+ normalizePath(path: string | null | undefined): string;
106
118
  /**
107
119
  * Pops matching page from array
108
120
  * @param {MarkdownInstance[]} allPages
@@ -120,7 +120,7 @@ export class Navigation {
120
120
  page.url != '/'
121
121
  && mp.url != page.url
122
122
  && mp.url.startsWith(page.url)
123
- && mp.url.split('/').length == (page.url.split('/').length + 1)
123
+ && this.pathDepth(mp.url) == (this.pathDepth(page.url) + 1)
124
124
  )
125
125
  .sort((mp) => mp.order);
126
126
 
@@ -458,6 +458,30 @@ export class Navigation {
458
458
  return true;
459
459
  }
460
460
 
461
+ /**
462
+ * Counts non-empty path segments so trailing slashes do not affect depth
463
+ * @param {string | null | undefined} path
464
+ * @returns {number}
465
+ */
466
+ pathDepth(path) {
467
+ const url = path ?? '/';
468
+ return url.split('/').filter((segment) => segment.length > 0).length;
469
+ }
470
+
471
+ /**
472
+ * Normalizes a path for comparison by joining non-empty segments
473
+ * @param {string | null | undefined} path
474
+ * @returns {string}
475
+ */
476
+ normalizePath(path) {
477
+ const depth = this.pathDepth(path);
478
+ if (depth === 0) {
479
+ return '/';
480
+ }
481
+
482
+ return '/' + (path ?? '/').split('/').filter((segment) => segment.length > 0).join('/');
483
+ }
484
+
461
485
  /**
462
486
  * Pops matching page from array
463
487
  * @param {MarkdownInstance[]} allPages
@@ -468,9 +492,10 @@ export class Navigation {
468
492
  const numberToRemove = 1;
469
493
  let indexToRemove = -1;
470
494
  let match = null;
495
+ const normalizedSearch = this.normalizePath(search);
471
496
 
472
497
  for (let i = 0; i < allPages.length; i++) {
473
- if (allPages[i].url == search) {
498
+ if (this.normalizePath(allPages[i].url) === normalizedSearch) {
474
499
  indexToRemove = i;
475
500
  match = allPages[i];
476
501
  }
package/lib/v1/posts.mjs CHANGED
@@ -40,7 +40,7 @@ export class Posts {
40
40
 
41
41
  return this.all().filter(p => {
42
42
  const url = p.url ?? '/';
43
- const depth = (url).split('/').length - 1;
43
+ const depth = url.split('/').filter((segment) => segment.length > 0).length;
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.3.96",
3
+ "version": "0.3.98",
4
4
  "description": "Astro utilities for Astro Accelerator.",
5
5
  "main": "index.mjs",
6
6
  "type": "module",