-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix previous releases sidebar feature for ecosystem projects (#267)
Closes #235. As summarized in #235 (comment), we were only handling Terra properly. I used test driven development to ensure I handled all the edge cases. ## Leverages ecosystem redirects We are now successfully redirecting ecosystem projects, e.g. `documentation/experiments/` to `ecosystem/experiments`. That simplifies the edge cases we need to handle: we only need to handle Terra (`documentation/`) and Ecosystem (`ecosystem/<project>`). ## Adds `utils.js` file Having a dedicated JavaScript file for our logic will make it easier to find all our custom logic. It also allows us to test the contents with Jest.
- Loading branch information
1 parent
4ca7eb3
commit 78303c6
Showing
7 changed files
with
186 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |