From 24b0ea9bb868390e1ef3290e476fd66788c083d5 Mon Sep 17 00:00:00 2001 From: Rob Winch <362503+rwinch@users.noreply.github.com> Date: Thu, 14 Nov 2024 08:37:11 -0600 Subject: [PATCH] feat: Stay on the same page when switching version Closes gh-269 --- gulp.d/tasks/build-preview-pages.js | 2 +- preview-src/samples/edge-cases/index.adoc.yml | 5 ++ src/helpers/versionTree.js | 35 ++++++-- src/partials/version.hbs | 2 +- test/versionTree-test.js | 89 ++++++++++++++++++- 5 files changed, 123 insertions(+), 10 deletions(-) diff --git a/gulp.d/tasks/build-preview-pages.js b/gulp.d/tasks/build-preview-pages.js index 8cbae9a..6cb3659 100644 --- a/gulp.d/tasks/build-preview-pages.js +++ b/gulp.d/tasks/build-preview-pages.js @@ -50,7 +50,7 @@ module.exports = const siteRootPath = path.relative(ospath.dirname(file.path), ospath.resolve(previewSrc)) const uiModel = { ...baseUiModel } const sharedPageModel = page.component ? baseUiModel.shared[page.component.name][page.version] : {} - uiModel.page = { ...uiModel.page, ...page, ...sharedPageModel } + uiModel.page = { ...uiModel.page, ...sharedPageModel, ...page } uiModel.siteRootPath = siteRootPath uiModel.siteRootUrl = path.join(siteRootPath, 'index.html') uiModel.uiRootPath = path.join(siteRootPath, '_') diff --git a/preview-src/samples/edge-cases/index.adoc.yml b/preview-src/samples/edge-cases/index.adoc.yml index 5cfe965..648164c 100644 --- a/preview-src/samples/edge-cases/index.adoc.yml +++ b/preview-src/samples/edge-cases/index.adoc.yml @@ -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 diff --git a/src/helpers/versionTree.js b/src/helpers/versionTree.js index 2efb15e..7ac32b3 100644 --- a/src/helpers/versionTree.js +++ b/src/helpers/versionTree.js @@ -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 +} diff --git a/src/partials/version.hbs b/src/partials/version.hbs index 445d44b..9810fdb 100644 --- a/src/partials/version.hbs +++ b/src/partials/version.hbs @@ -9,7 +9,7 @@