Explorar o código

WIP: add depthRange option

Yuki Takei %!s(int64=2) %!d(string=hai) anos
pai
achega
625dd05f20

+ 3 - 1
packages/remark-lsx/src/components/Lsx.tsx

@@ -83,7 +83,9 @@ const LsxSubstance = React.memo(({
       return <></>;
     }
 
-    const nodeTree = generatePageNodeTree(prefix, data.flatMap(d => d.pages));
+    const depthRange = lsxContext.getOptDepth();
+
+    const nodeTree = generatePageNodeTree(prefix, data.flatMap(d => d.pages), depthRange);
     const basisViewersCount = data.at(-1)?.toppageViewersCount;
 
     return <LsxListView nodeTree={nodeTree} lsxContext={lsxContext} basisViewersCount={basisViewersCount} />;

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

@@ -1,4 +1,4 @@
-import { IPageHasId } from '@growi/core';
+import { IPageHasId, OptionParser } from '@growi/core';
 import { mock } from 'vitest-mock-extended';
 
 import { PageNode } from '../interfaces/page-node';
@@ -127,4 +127,46 @@ describe('generatePageNodeTree()', () => {
     ]);
   });
 
+  it("returns with 'depth=1:2'", () => {
+    // setup
+    const pages: IPageHasId[] = [
+      '/Sandbox/child1',
+      '/Sandbox/child2',
+      '/Sandbox/child2/child2-1',
+      '/Sandbox/child2/child2-2',
+      '/Sandbox/child2/child2-2/child2-2-1',
+    ].map(path => mock<IPageHasId>({ path }));
+
+    // when
+    const depthRange = OptionParser.parseRange('1:2');
+    const result = generatePageNodeTree('/Sandbox', pages, depthRange);
+    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-2/child2-2-1',
+                children: [],
+              },
+            ],
+          },
+        ],
+      },
+    ]);
+  });
+
 });

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

@@ -1,6 +1,6 @@
 import * as url from 'url';
 
-import { IPageHasId, pathUtils } from '@growi/core';
+import { IPageHasId, ParseRangeResult, pathUtils } from '@growi/core';
 
 import type { PageNode } from '../interfaces/page-node';
 
@@ -50,7 +50,7 @@ function generatePageNode(pathToNodeMap: Record<string, PageNode>, rootPagePath:
   return node;
 }
 
-export function generatePageNodeTree(rootPagePath: string, pages: IPageHasId[]): PageNode[] {
+export function generatePageNodeTree(rootPagePath: string, pages: IPageHasId[], depthRange?: ParseRangeResult | null): PageNode[] {
   const pathToNodeMap: Record<string, PageNode> = {};
 
   pages.forEach((page) => {