Skip to content

Commit

Permalink
Remove extraneous headers and nav item from versioned docs (#2692)
Browse files Browse the repository at this point in the history
* remove extraneous headers and nav item from versioned docs

* add tests
  • Loading branch information
im2nguyen authored Feb 22, 2025
1 parent a392322 commit 525e0e1
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
130 changes: 130 additions & 0 deletions src/views/docs-view/loaders/__tests__/remote-content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
19 changes: 19 additions & 0 deletions src/views/docs-view/loaders/remote-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 525e0e1

Please sign in to comment.