diff --git a/__tests__/utils.test.js b/__tests__/utils.test.js index 83cc2eb7..18719b10 100644 --- a/__tests__/utils.test.js +++ b/__tests__/utils.test.js @@ -1,5 +1,90 @@ -const { expect, test } = require("@jest/globals"); +const { describe, expect, test } = require("@jest/globals"); +const { + determineVersionURL: determineVersionURLPytorch, +} = require("../qiskit_sphinx_theme/pytorch_base/static/js/utils.js"); +const { + determineVersionURL: determineVersionURLFuro, +} = require("../qiskit_sphinx_theme/furo/base/static/js/utils.js"); -test("dummy test to check Jest infrastructure works. Remove after", () => { - expect(true).toBe(true); +const checkVersionURL = (inputURL, version, expected) => { + expect(determineVersionURLPytorch(inputURL, version)).toEqual(expected); + expect(determineVersionURLFuro(inputURL, version)).toEqual(expected); +}; + +describe("determineVersionURL()", () => { + test("handles the /ecosystem scheme", () => { + checkVersionURL( + "https://qiskit.org/ecosystem/nature", + "0.4", + "/ecosystem/nature/stable/0.4/index.html" + ); + checkVersionURL( + "https://qiskit.org/ecosystem/finance", + "0.6", + "/ecosystem/finance/stable/0.6/index.html" + ); + checkVersionURL( + "https://qiskit.org/ecosystem/nature/index.html", + "0.4", + "/ecosystem/nature/stable/0.4/index.html" + ); + checkVersionURL( + "https://qiskit.org/ecosystem/nature/apidocs/index.html", + "0.4", + "/ecosystem/nature/stable/0.4/index.html" + ); + + // Already on stable URL: + checkVersionURL( + "https://qiskit.org/ecosystem/nature/stable/0.6/index.html", + "0.4", + "/ecosystem/nature/stable/0.4/index.html" + ); + checkVersionURL( + "https://qiskit.org/ecosystem/finance/stable/0.7/index.html", + "0.6", + "/ecosystem/finance/stable/0.6/index.html" + ); + checkVersionURL( + "https://qiskit.org/ecosystem/nature/stable/0.6/apidocs/index.html", + "0.4", + "/ecosystem/nature/stable/0.4/index.html" + ); + }); + + test("handles Terra", () => { + checkVersionURL( + "https://qiskit.org/documentation", + "0.4", + "/documentation/stable/0.4/index.html" + ); + checkVersionURL( + "https://qiskit.org/documentation/index.html", + "0.4", + "/documentation/stable/0.4/index.html" + ); + checkVersionURL( + "https://qiskit.org/documentation/apidocs/index.html", + "0.4", + "/documentation/stable/0.4/index.html" + ); + + // Already on stable URL: + checkVersionURL( + "https://qiskit.org/documentation/stable/0.6/index.html", + "0.4", + "/documentation/stable/0.4/index.html" + ); + checkVersionURL( + "https://qiskit.org/documentation/stable/0.6/apidocs/index.html", + "0.4", + "/documentation/stable/0.4/index.html" + ); + }); + + test("return null if the URL does not match", () => { + checkVersionURL("https://qiskit.org", "0.4", null); + checkVersionURL("https://qiskit.org/ecosystem", "0.4", null); + checkVersionURL("file:///Users/qiskit/index.html", "0.4", null); + }); }); diff --git a/qiskit_sphinx_theme/furo/base/custom_templates/extra_head.html b/qiskit_sphinx_theme/furo/base/custom_templates/extra_head.html index 40526af7..5478f427 100644 --- a/qiskit_sphinx_theme/furo/base/custom_templates/extra_head.html +++ b/qiskit_sphinx_theme/furo/base/custom_templates/extra_head.html @@ -2,6 +2,7 @@ + {%- if analytics_enabled %} + diff --git a/qiskit_sphinx_theme/pytorch_base/sidebar.html b/qiskit_sphinx_theme/pytorch_base/sidebar.html index a0cbd6b8..8bb774dd 100644 --- a/qiskit_sphinx_theme/pytorch_base/sidebar.html +++ b/qiskit_sphinx_theme/pytorch_base/sidebar.html @@ -18,7 +18,9 @@ {% endif %} diff --git a/qiskit_sphinx_theme/pytorch_base/static/js/utils.js b/qiskit_sphinx_theme/pytorch_base/static/js/utils.js new file mode 100644 index 00000000..095b90c0 --- /dev/null +++ b/qiskit_sphinx_theme/pytorch_base/static/js/utils.js @@ -0,0 +1,45 @@ +/** + * Determine the URL for the version's documentation. + * + * @param version currentURL: The current browser's URL. + * @param version string: The version of the docs, e.g. '0.4'. + * @returns string | null + */ +const determineVersionURL = (currentURL, version) => { + // We expect URls to either be `documentation/` (Terra) or `ecosystem/`. + const match = currentURL.match( + /(?:.*)(\/ecosystem\/\w+|\/documentation)(?:\/|$)/ + ); + if (!match) { + return null; + } + return `${match[1]}/stable/${version}/index.html`; +}; + +/** + * Change the browser's URL to the version's documentation. Do nothing if the current URL + * is unexpected. + * + * @param version string: The version of the docs, e.g. '0.4'. + */ +const redirectToVersion = (version) => { + const result = determineVersionURL(window.location.href, version); + if (!result) { + console.error( + `Could not determine the version URL for ${window.location.href} with version ${version}` + ); + return; + } + window.location.href = result; +}; + +if (typeof module === "undefined") { + // Export utils for Sphinx usage. Assigning the values to `window` makes + // the values available globally. + window.redirectToVersion = redirectToVersion; +} else { + // Export utils for Jest testing. + module.exports = { + determineVersionURL, + }; +}