Skip to content

Commit

Permalink
Use page.versions
Browse files Browse the repository at this point in the history
This allows staying on the same page when switching to a version that
contains the same page.

Closes gh-269
  • Loading branch information
rwinch committed Nov 13, 2024
1 parent 45c7586 commit 97969fa
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 9 deletions.
5 changes: 5 additions & 0 deletions preview-src/samples/edge-cases/index.adoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ breadcrumbs:
- content: Edge cases
url: '#'
urlType: fragment
versions:
- url: '#/samples/edge-cases.html'
displayVersion: 2.0.0
- url: '#/1.0.0/samples/edge-cases.html'
displayVersion: 1.0.0
35 changes: 28 additions & 7 deletions src/helpers/versionTree.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
'use strict'

module.exports = (components) => versionTree(components)
module.exports = (components, page) => versionTree(components, page)

function versionTree (components) {
function versionTree (components, page) {
const versionToUrl = {}
if (page && page.versions) {
page.versions.forEach((v) => {
versionToUrl[v.displayVersion] = v.url
})
}
for (const [, component] of Object.entries(components)) {
component.versionTree = splitVersions(component.versions)
const componentVersionToUrl = component &&
page &&
page.component &&
component.name === page.component.name ? versionToUrl : {}
component.versionTree = splitVersions(component.versions, componentVersionToUrl)
}
return components
}

function splitVersions (versions) {
const snapshot = versions.filter((v) => v.displayVersion.includes('SNAPSHOT'))
const stable = versions.filter((v) => !v.displayVersion.includes('-'))
const preview = versions.filter((v) => !snapshot.includes(v) && !stable.includes(v))
function splitVersions (versions, versionToUrl) {
const snapshot = versions.filter((v) => v.displayVersion.includes('SNAPSHOT')).map((v) => navVersion(v, versionToUrl))
const stable = versions.filter((v) => !v.displayVersion.includes('-')).map((v) => navVersion(v, versionToUrl))
const preview = versions.filter((v) => !v.displayVersion.includes('SNAPSHOT') &&
v.displayVersion.includes('-')).map((v) => navVersion(v, versionToUrl))
return {
snapshot: snapshot.length > 0 ? snapshot : null,
stable: stable.length > 0 ? stable : null,
preview: preview.length > 0 ? preview : null,
}
}

function navVersion (v, versionToUrl) {
const navVersion =
v.latest ? { latest: v.latest, url: v.url, displayVersion: v.displayVersion }
: { url: v.url, displayVersion: v.displayVersion }
if (versionToUrl[v.displayVersion]) {
navVersion.url = versionToUrl[v.displayVersion]
}
return navVersion
}
2 changes: 1 addition & 1 deletion src/partials/version.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="col-left">

<ul class="nav-versions">
{{#each (versionTree site.components)}}
{{#each (versionTree site.components @root.page)}}
<li class="component">
<div>
<a class="title" href="{{{relativize ./url}}}">{{{./title}}}</a>
Expand Down
89 changes: 88 additions & 1 deletion test/versionTree-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { expect } = require('./harness')
const versionTree = require('../src/helpers/versionTree.js')

describe('versionTree', () => {
it('should return stable, preview and snapshot versions', () => {
it('should return stable, preview and snapshot versions', () => {
const result = versionTree({
test: {
versions: [
Expand Down Expand Up @@ -45,6 +45,93 @@ describe('versionTree', () => {
expect(tree.snapshot[1].displayVersion).is.eql('3.0.0-SNAPSHOT')
})

it('page version overrides urls when component names are the same', () => {
const result = versionTree({
test: {
name: 'test',
versions: [
{
displayVersion: '2.0.0',
url: './version.html',
},
],
},
}, {
component: {
name: 'test',
},
versions: [
{
displayVersion: '2.0.0',
url: './page.html',
},
],
})

const tree = result.test.versionTree
expect(tree.stable.length).is.eql(1)
expect(tree.stable[0].displayVersion).is.eql('2.0.0')
expect(tree.stable[0].url).is.eql('./page.html')
})

it('does not override if page does not define same version', () => {
const result = versionTree({
test: {
name: 'test',
versions: [
{
displayVersion: '2.0.0',
url: './version.html',
},
],
},
}, {
component: {
name: 'test',
},
versions: [
{
displayVersion: '1.0.0',
url: './page.html',
},
],
})

const tree = result.test.versionTree
expect(tree.stable.length).is.eql(1)
expect(tree.stable[0].displayVersion).is.eql('2.0.0')
expect(tree.stable[0].url).is.eql('./version.html')
})

it('page versions do not override if different component name', () => {
const result = versionTree({
test: {
name: 'test',
versions: [
{
displayVersion: '2.0.0',
url: './version.html',
},
],
},
}, {
component: {
name: 'baz',
},
versions: [
{
displayVersion: '2.0.0',
url: './page.html',
},
],
})

const tree = result.test.versionTree
expect(tree.stable.length).is.eql(1)
expect(tree.stable[0].displayVersion).is.eql('2.0.0')
expect(tree.stable[0].url).is.eql('./version.html')
})

it('should return an empty structure', () => {
const result = versionTree({
test: {
Expand Down

0 comments on commit 97969fa

Please sign in to comment.