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

Fix previous releases sidebar feature for ecosystem projects #267

Merged
merged 10 commits into from
May 4, 2023
91 changes: 88 additions & 3 deletions __tests__/utils.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;700&family=IBM+Plex+Sans:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet">
<script src="{{ pathto('_static/js/web-components/top-nav-bar.js', 1) }}"></script>
<script type="text/javascript" src="{{ pathto('_static/js/utils.js', 1) }}"></script>

{%- if analytics_enabled %}
<script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<div class="sidebar-l1-expandable">Previous Releases</div>
<div class="sidebar-subheadings">
{% for version in version_list | sort(reverse=True) %}
<div class="sidebar-l2"><a href="/documentation/stable/{{ version }}/index.html">{{ version }}</a></div>
<div class="sidebar-l2">
<a href="#" onclick="redirectToVersion( {{ version }} )" rel="nofollow">{{ version }}</a>
</div>
{% endfor %}
</div>
{% endif %}
45 changes: 45 additions & 0 deletions qiskit_sphinx_theme/furo/base/static/js/utils.js
Original file line number Diff line number Diff line change
@@ -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/<project>`.
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,
};
}
1 change: 1 addition & 0 deletions qiskit_sphinx_theme/pytorch_base/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
<script type="text/javascript" src="{{ pathto('_static/js/vendor/bootstrap.min.js', 1) }}"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>
<script type="text/javascript" src="{{ pathto('_static/js/theme.js', 1) }}"></script>
<script type="text/javascript" src="{{ pathto('_static/js/utils.js', 1) }}"></script>

<script type="text/javascript" src="{{ pathto('_static/js/vendor/anchor.min.js', 1) }}"></script>

Expand Down
4 changes: 3 additions & 1 deletion qiskit_sphinx_theme/pytorch_base/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
<div class="sidebar-l1-expandable">Previous Releases</div>
<div class="sidebar-subheadings">
{% for version in version_list | sort(reverse=True) %}
<div class="sidebar-l2"><a href="/documentation/stable/{{ version }}/index.html">{{ version }}</a></div>
<div class="sidebar-l2">
<a href="#" onclick="redirectToVersion( {{ version }} )" rel="nofollow">{{ version }}</a>
</div>
{% endfor %}
</div>
{% endif %}
Expand Down
45 changes: 45 additions & 0 deletions qiskit_sphinx_theme/pytorch_base/static/js/utils.js
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy and paste of the Furo file. The Jest test is set up to ensure that the tests pass for both Furo and Pytorch.

We aren't testing that the file contents are identical between Furo and Pytorch because we may want utils.js to diverge. For example, maybe we add a util to Furo that is not needed for Pytorch.

Original file line number Diff line number Diff line change
@@ -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/<project>`.
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,
};
}