Yuki Takei 2 лет назад
Родитель
Сommit
7ca1ad8c16

+ 32 - 18
packages/remark-lsx/src/utils/page-node.spec.ts

@@ -18,11 +18,30 @@ function omitPageData(pageNode: PageNode): Omit<PageNode, 'page'> {
 
 describe('generatePageNodeTree()', () => {
 
-  it('returns when the pages are not empty', () => {
+  it("returns when the rootPagePath is '/'", () => {
     // setup
     const pages: IPageHasId[] = [
       '/',
       '/Sandbox',
+    ].map(path => mock<IPageHasId>({ path }));
+
+    // when
+    const result = generatePageNodeTree('/', pages);
+    const resultWithoutPageData = result.map(pageNode => omitPageData(pageNode));
+
+    // then
+    expect(resultWithoutPageData).toStrictEqual([
+      {
+        pagePath: '/Sandbox',
+        children: [],
+      },
+    ]);
+  });
+
+  it('returns when the pages are not empty', () => {
+    // setup
+    const pages: IPageHasId[] = [
+      '/Sandbox',
       '/Sandbox/level2',
       '/Sandbox/level2/level3-1',
       '/Sandbox/level2/level3-2',
@@ -30,30 +49,25 @@ describe('generatePageNodeTree()', () => {
     ].map(path => mock<IPageHasId>({ path }));
 
     // when
-    const result = generatePageNodeTree('/', pages);
+    const result = generatePageNodeTree('/Sandbox', pages);
     const resultWithoutPageData = result.map(pageNode => omitPageData(pageNode));
 
     // then
     expect(resultWithoutPageData).toStrictEqual([
       {
-        pagePath: '/Sandbox',
+        pagePath: '/Sandbox/level2',
         children: [
           {
-            pagePath: '/Sandbox/level2',
-            children: [
-              {
-                pagePath: '/Sandbox/level2/level3-1',
-                children: [],
-              },
-              {
-                pagePath: '/Sandbox/level2/level3-2',
-                children: [],
-              },
-              {
-                pagePath: '/Sandbox/level2/level3-3',
-                children: [],
-              },
-            ],
+            pagePath: '/Sandbox/level2/level3-1',
+            children: [],
+          },
+          {
+            pagePath: '/Sandbox/level2/level3-2',
+            children: [],
+          },
+          {
+            pagePath: '/Sandbox/level2/level3-3',
+            children: [],
           },
         ],
       },

+ 5 - 0
packages/remark-lsx/src/utils/page-node.ts

@@ -29,6 +29,11 @@ function generatePageNode(
     pathToNodeMap: Record<string, PageNode>, rootPagePath: string, pagePath: string, depthRange?: ParseRangeResult | null,
 ): PageNode | null {
 
+  // exclude rootPagePath itself
+  if (pagePath === rootPagePath) {
+    return null;
+  }
+
   const depthStartToProcess = getDepthOfPath(rootPagePath) + (depthRange?.start ?? 0); // at least 1
   const currentPageDepth = getDepthOfPath(pagePath);