Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove extraneous headers and nav item from versioned docs #2692

Merged
merged 2 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading