Yuki Takei пре 2 година
родитељ
комит
16f69a39de

+ 43 - 59
packages/remark-lsx/src/utils/page-node.spec.ts

@@ -18,7 +18,7 @@ function omitPageData(pageNode: PageNode): Omit<PageNode, 'page'> {
 
 describe('generatePageNodeTree()', () => {
 
-  it("returns with { rootPagePath : '/' }", () => {
+  it('returns with non-empty pages', () => {
     // setup
     const pages: IPageHasId[] = [
       '/',
@@ -28,9 +28,6 @@ describe('generatePageNodeTree()', () => {
       '/Sandbox/child2/child2-1',
       '/Sandbox/child2/child2-2',
       '/Sandbox/child2/child2-3',
-      '/user/foo',
-      '/user/bar',
-      '/user/bar/memo/2023/06/01',
     ].map(path => mock<IPageHasId>({ path }));
 
     // when
@@ -40,54 +37,83 @@ describe('generatePageNodeTree()', () => {
     // then
     expect(resultWithoutPageData).toStrictEqual([
       {
-        pagePath: '/Sandbox/',
+        pagePath: '/Sandbox',
         children: [
           {
-            pagePath: '/Sandbox/child1/',
+            pagePath: '/Sandbox/child1',
             children: [],
           },
           {
-            pagePath: '/Sandbox/child2/',
+            pagePath: '/Sandbox/child2',
             children: [
               {
-                pagePath: '/Sandbox/child2/child2-1/',
+                pagePath: '/Sandbox/child2/child2-1',
                 children: [],
               },
               {
-                pagePath: '/Sandbox/child2/child2-2/',
+                pagePath: '/Sandbox/child2/child2-2',
                 children: [],
               },
               {
-                pagePath: '/Sandbox/child2/child2-3/',
+                pagePath: '/Sandbox/child2/child2-3',
                 children: [],
               },
             ],
           },
         ],
       },
+    ]);
+  });
+
+  it('returns with empty pages', () => {
+    // setup
+    const pages: IPageHasId[] = [
+      '/',
+      '/user/foo',
+      '/user/bar',
+      '/user/bar/memo/2023/06/01',
+      '/user/bar/memo/2023/06/02/memo-test',
+    ].map(path => mock<IPageHasId>({ path }));
+
+    // when
+    const result = generatePageNodeTree('/', pages);
+    const resultWithoutPageData = result.map(pageNode => omitPageData(pageNode));
+
+    // then
+    expect(resultWithoutPageData).toStrictEqual([
+
       {
-        pagePath: '/user/',
+        pagePath: '/user',
         children: [
           {
-            pagePath: '/user/foo/',
+            pagePath: '/user/foo',
             children: [],
           },
           {
-            pagePath: '/user/bar/',
+            pagePath: '/user/bar',
             children: [
               {
-                pagePath: '/user/bar/memo/',
+                pagePath: '/user/bar/memo',
                 children: [
                   {
-                    pagePath: '/user/bar/memo/2023/',
+                    pagePath: '/user/bar/memo/2023',
                     children: [
                       {
-                        pagePath: '/user/bar/memo/2023/06/',
+                        pagePath: '/user/bar/memo/2023/06',
                         children: [
                           {
-                            pagePath: '/user/bar/memo/2023/06/01/',
+                            pagePath: '/user/bar/memo/2023/06/01',
                             children: [],
                           },
+                          {
+                            pagePath: '/user/bar/memo/2023/06/02',
+                            children: [
+                              {
+                                pagePath: '/user/bar/memo/2023/06/02/memo-test',
+                                children: [],
+                              },
+                            ],
+                          },
                         ],
                       },
                     ],
@@ -101,46 +127,4 @@ describe('generatePageNodeTree()', () => {
     ]);
   });
 
-  it("returns with { rootPagePath : '/Sandbox' }", () => {
-    // setup
-    const pages: IPageHasId[] = [
-      '/',
-      '/Sandbox',
-      '/Sandbox/child1',
-      '/Sandbox/child2',
-      '/Sandbox/child2/child2-1',
-      '/Sandbox/child2/child2-2',
-      '/Sandbox/child2/child2-3',
-    ].map(path => mock<IPageHasId>({ path }));
-
-    // when
-    const result = generatePageNodeTree('/Sandbox', pages);
-    const resultWithoutPageData = result.map(pageNode => omitPageData(pageNode));
-
-    // then
-    expect(resultWithoutPageData).toStrictEqual([
-      {
-        pagePath: '/Sandbox/child1/',
-        children: [],
-      },
-      {
-        pagePath: '/Sandbox/child2/',
-        children: [
-          {
-            pagePath: '/Sandbox/child2/child2-1/',
-            children: [],
-          },
-          {
-            pagePath: '/Sandbox/child2/child2-2/',
-            children: [],
-          },
-          {
-            pagePath: '/Sandbox/child2/child2-3/',
-            children: [],
-          },
-        ],
-      },
-    ]);
-  });
-
 });

+ 2 - 6
packages/remark-lsx/src/utils/page-node.ts

@@ -9,7 +9,7 @@ function isEquals(path1: string, path2: string) {
 }
 
 function getParentPath(path: string) {
-  return pathUtils.addTrailingSlash(decodeURIComponent(url.resolve(path, '../')));
+  return pathUtils.removeTrailingSlash(decodeURIComponent(url.resolve(path, './')));
 }
 
 /**
@@ -54,11 +54,7 @@ export function generatePageNodeTree(rootPagePath: string, pages: IPageHasId[]):
   const pathToNodeMap: Record<string, PageNode> = {};
 
   pages.forEach((page) => {
-    // add slash ensure not to forward match to another page
-    // e.g. '/Java/' not to match to '/JavaScript'
-    const pagePath = pathUtils.addTrailingSlash(page.path);
-
-    const node = generatePageNode(pathToNodeMap, rootPagePath, pagePath); // this will not be null
+    const node = generatePageNode(pathToNodeMap, rootPagePath, page.path); // this will not be null
 
     // exclude rootPagePath itself
     if (node == null) {