diff options
-rw-r--r-- | content/frontend/default/components/versions_menu.vue | 19 | ||||
-rw-r--r-- | layouts/head.html | 7 | ||||
-rw-r--r-- | lib/helpers/versions.rb | 44 | ||||
-rw-r--r-- | spec/frontend/default/components/versions_menu_spec.js | 13 |
4 files changed, 49 insertions, 34 deletions
diff --git a/content/frontend/default/components/versions_menu.vue b/content/frontend/default/components/versions_menu.vue index 01773330..0e86d58f 100644 --- a/content/frontend/default/components/versions_menu.vue +++ b/content/frontend/default/components/versions_menu.vue @@ -21,7 +21,7 @@ export default { if (isGitLabHosted()) { try { this.versions = await getVersions(); - this.activeVersion = this.getActiveVersion(this.versions); + this.activeVersion = document.querySelector('meta[name="gitlab-docs-version"]').content; } catch (err) { console.error(`Failed to fetch versions.json: ${err}`); // eslint-disable-line no-console } @@ -43,23 +43,6 @@ export default { } return path; }, - getActiveVersion(versions) { - let activeVersion = versions.next; - - // Check if the first item in the URL path is a valid version. - // If so, that should be the active menu item. - const versionPath = window.location.pathname.split('/')[1]; - - Object.keys(versions).forEach((key) => { - if ( - versions[key] === versionPath || - (versions[key].constructor === Array && versions[key].includes(versionPath)) - ) { - activeVersion = versionPath; - } - }); - return activeVersion; - }, }, }; </script> diff --git a/layouts/head.html b/layouts/head.html index 667a4d33..4dc6dfda 100644 --- a/layouts/head.html +++ b/layouts/head.html @@ -15,15 +15,12 @@ <% if @item[:noindex] or !production_and_default_branch? %> <meta name="robots" content="noindex, nofollow"> <% end %> +<meta name="gitlab-docs-version" content="<%= site_version %>"> <% if @config[:algolia] == "true" %> <!--https://community.algolia.com/docsearch/required-configuration.html#introduces-global-information-as-meta-tags--> <meta name="docsearch:language" content="en" /> -<% if !ENV['CI_COMMIT_REF_NAME'].nil? and stable_version?(ENV['CI_COMMIT_REF_NAME']) %> -<meta name="docsearch:version" content="<%= ENV['CI_COMMIT_REF_NAME'] %>" /> -<% else %> -<meta name="docsearch:version" content="<%= ENV['CI_DEFAULT_BRANCH'] %>" /> -<% end %> +<meta name="docsearch:version" content="<%= docsearch_version %>" /> <meta name="docsearch:pageRank" content="<%= algolia_rank(@item).to_s %>" /> <meta name="docsearch:level" content="<%= algolia_level(@item).to_s %>" /> <link crossorigin href="https://3PNCFOU757-dsn.algolia.net" rel="preconnect" /> diff --git a/lib/helpers/versions.rb b/lib/helpers/versions.rb index 70fa6a67..90b5739b 100644 --- a/lib/helpers/versions.rb +++ b/lib/helpers/versions.rb @@ -15,13 +15,49 @@ module Nanoc::Helpers end # - # Check if the current version is the latest. + # Get online versions from the JSON file. # - def latest? + def get_versions file = File.read('./content/versions.json') parsed = JSON.parse(file) - latest_version = parsed[0]['current'] - ENV['CI_COMMIT_REF_NAME'] == ENV['CI_DEFAULT_BRANCH'] || ENV['CI_COMMIT_REF_NAME'] == latest_version + parsed[0] + end + + # + # Returns the site version using the branch or tag from the CI build. + # + def site_version + if !ENV['CI_COMMIT_REF_NAME'].nil? && stable_version?(ENV['CI_COMMIT_REF_NAME']) + ENV['CI_COMMIT_REF_NAME'] + else + # If this wasn't built on CI, this is a local site that can default to the pre-release version. + get_versions["next"] + end + end + + # + # Returns the site version for Algolia DocSearch. + # + # For DocSearch, we need to pass "main" instead of a version number + # in the case of the pre-release site. + # + def docsearch_version + if get_versions["next"] == site_version + ENV['CI_DEFAULT_BRANCH'] + else + site_version + end + end + + # + # Check if this site version is the latest. + # + # We consider two versions to be "latest": + # 1) The main branch (CI_DEFAULT_BRANCH), which are pre-release docs for the next version. + # 2) The most recent stable release, which is "current" in versions.json. + # + def latest? + ENV['CI_COMMIT_REF_NAME'] == ENV['CI_DEFAULT_BRANCH'] || ENV['CI_COMMIT_REF_NAME'] == get_versions["current"] end # diff --git a/spec/frontend/default/components/versions_menu_spec.js b/spec/frontend/default/components/versions_menu_spec.js index 035c59fc..8881ab09 100644 --- a/spec/frontend/default/components/versions_menu_spec.js +++ b/spec/frontend/default/components/versions_menu_spec.js @@ -13,6 +13,12 @@ jest.mock('../../../../content/frontend/services/fetch_versions'); beforeEach(() => { jest.clearAllMocks(); + + const meta = document.createElement('meta'); + meta.setAttribute('name', 'gitlab-docs-version'); + meta.setAttribute('content', '15.5'); + document.head.appendChild(meta); + getVersions.mockResolvedValueOnce(mockVersions); }); @@ -72,11 +78,4 @@ describe('component: Versions menu', () => { }, ); }); - - it('Detects the active version from the page URL', async () => { - setWindowPath('/14.10/runner'); - const wrapper = mount(VersionsMenu); - await flushPromises(); - expect(wrapper.vm.getActiveVersion(mockVersions)).toBe('14.10'); - }); }); |