From 525e0e193341d3a0fd8d902d8c6027dddc6f3e0e Mon Sep 17 00:00:00 2001 From: Tu Nguyen Date: Fri, 21 Feb 2025 16:01:43 -0800 Subject: [PATCH] Remove extraneous headers and nav item from versioned docs (#2692) * remove extraneous headers and nav item from versioned docs * add tests --- .../loaders/__tests__/remote-content.test.ts | 130 ++++++++++++++++++ src/views/docs-view/loaders/remote-content.ts | 19 +++ 2 files changed, 149 insertions(+) diff --git a/src/views/docs-view/loaders/__tests__/remote-content.test.ts b/src/views/docs-view/loaders/__tests__/remote-content.test.ts index cf894c4352..82de1ce995 100644 --- a/src/views/docs-view/loaders/__tests__/remote-content.test.ts +++ b/src/views/docs-view/loaders/__tests__/remote-content.test.ts @@ -279,6 +279,136 @@ describe('RemoteContentLoader', () => { // assert that `serialize` is called with the result of the hook expect(serializeSpy).toHaveBeenCalledWith('Mock impl', expect.any(Object)) }) + + test('should not modify nav data for latest version', async () => { + // Create test data with a heading and an empty path item + const testNavData = { + meta: { + status_code: 200, + status_text: 'OK', + }, + result: { + navData: [ + { + heading: 'Test Heading', + }, + { + title: 'Empty Path Item', + path: '', + }, + { + title: 'Valid Item', + path: 'some-path', + }, + ], + }, + } + + scope + .get('/api/content/waypoint/version-metadata') + .query({ partial: 'true' }) + .reply(200, versionMetadata_200) + scope + .get('/api/content/waypoint/doc/v0.5.x/commands') + .reply(200, document_200) + scope + .get('/api/content/waypoint/nav-data/v0.5.x/commands') + .reply(200, testNavData) + + const versionedDocsLoader = new RemoteContentLoader({ + ...loader.opts, + enabledVersionedDocs: true, + latestVersionRef: 'v0.5.x', + }) + + const props = await versionedDocsLoader.loadStaticProps({ + params: { + page: ['v0.5.x'], + }, + }) + + // Verify the heading is preserved + expect(props.navData[0]).toHaveProperty('heading', 'Test Heading') + + // Verify the empty path item is preserved + const emptyPathItem = props.navData.find( + (item: { path?: string; title?: string }) => item.path === '' + ) + expect(emptyPathItem).toBeDefined() + expect(emptyPathItem).toEqual({ + title: 'Empty Path Item', + path: '', + }) + + // Verify valid items remain unchanged + expect(props.navData).toContainEqual({ + title: 'Valid Item', + path: 'some-path', + }) + }) + + test('should process nav data correctly for non-latest versions', async () => { + // Create test data with a heading and an empty path item + const testNavData = { + meta: { + status_code: 200, + status_text: 'OK', + }, + result: { + navData: [ + { + heading: 'Test Heading', + }, + { + title: 'Empty Path Item', + path: 'v0.4.x/', + }, + { + title: 'Valid Item', + path: 'v0.4.x/some-path', + }, + ], + }, + } + + scope + .get('/api/content/waypoint/version-metadata') + .query({ partial: 'true' }) + .reply(200, versionMetadata_200) + scope + .get('/api/content/waypoint/doc/v0.4.x/commands') + .reply(200, document_v4) + scope + .get('/api/content/waypoint/nav-data/v0.4.x/commands') + .reply(200, testNavData) + + const versionedDocsLoader = new RemoteContentLoader({ + ...loader.opts, + enabledVersionedDocs: true, + }) + + const props = await versionedDocsLoader.loadStaticProps({ + params: { + page: ['v0.4.x'], + }, + }) + + // Verify the heading was removed + expect(props.navData[0]).not.toHaveProperty('heading') + + // Verify the empty path item was removed + const emptyPathItem = props.navData.find( + (item: { path?: string; title?: string }) => + item.path && item.path.split('/').length <= 1 + ) + expect(emptyPathItem).toBeUndefined() + + // Verify valid items remain + expect(props.navData).toContainEqual({ + title: 'Valid Item', + path: 'v0.4.x/some-path', + }) + }) }) describe('mapVersionList', () => { diff --git a/src/views/docs-view/loaders/remote-content.ts b/src/views/docs-view/loaders/remote-content.ts index 3f9fd49a39..297ea9627d 100644 --- a/src/views/docs-view/loaders/remote-content.ts +++ b/src/views/docs-view/loaders/remote-content.ts @@ -238,6 +238,25 @@ export default class RemoteContentLoader implements DataLoader { navDataPromise, ]) + // For non-latest versions + if ( + versionToFetch !== this.opts.latestVersionRef && + versionToFetch !== 'latest' + ) { + // Remove the first heading from the navData + if (navData.navData.length > 0 && 'heading' in navData.navData[0]) { + navData.navData.shift() + } + + // Remove item if it's an empty path (for non-latest versions, the version is prepended to the path) + if ( + navData.navData.length > 1 && + navData.navData[0].path.split('/').length > 1 + ) { + navData.navData.splice(0, 1) + } + } + const { mdxSource } = await mdxRenderer(document.markdownSource) const frontMatter = document.metadata