Add version control menu (#5222)
* Add version control menu * Constify things Co-authored-by: Lysandre Debut <lysandre@huggingface.co> * Apply suggestions from code review Co-authored-by: Julien Chaumond <chaumond@gmail.com> Co-authored-by: Lysandre Debut <lysandre@huggingface.co> Co-authored-by: Julien Chaumond <chaumond@gmail.com>
This commit is contained in:
@@ -1,5 +1,45 @@
|
|||||||
/* Our DOM objects */
|
/* Our DOM objects */
|
||||||
|
|
||||||
|
/* Version control */
|
||||||
|
|
||||||
|
.version-button {
|
||||||
|
background-color: #6670FF;
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
padding: 5px;
|
||||||
|
font-size: 15px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.version-button:hover, .version-button:focus {
|
||||||
|
background-color: #A6B0FF;
|
||||||
|
}
|
||||||
|
|
||||||
|
.version-dropdown {
|
||||||
|
display: none;
|
||||||
|
background-color: #6670FF;
|
||||||
|
min-width: 160px;
|
||||||
|
overflow: auto;
|
||||||
|
font-size: 15px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.version-dropdown a {
|
||||||
|
color: white;
|
||||||
|
padding: 3px 4px;
|
||||||
|
text-decoration: none;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
.version-dropdown a:hover {
|
||||||
|
background-color: #A6B0FF;
|
||||||
|
}
|
||||||
|
|
||||||
|
.version-show {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Framework selector */
|
||||||
|
|
||||||
.framework-selector {
|
.framework-selector {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: row;
|
flex-direction: row;
|
||||||
@@ -38,6 +78,7 @@
|
|||||||
|
|
||||||
/* The research field on top of the toc tree */
|
/* The research field on top of the toc tree */
|
||||||
.wy-side-nav-search{
|
.wy-side-nav-search{
|
||||||
|
padding-top: 0;
|
||||||
background-color: #6670FF;
|
background-color: #6670FF;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,26 @@
|
|||||||
|
// These two things need to be updated at each release for the version selector.
|
||||||
|
// Last stable version
|
||||||
|
const stableVersion = "v2.11.0"
|
||||||
|
// Dictionary doc folder to label
|
||||||
|
const versionMapping = {
|
||||||
|
"master": "master",
|
||||||
|
"": "v2.11.0 (stable)",
|
||||||
|
"v2.10.0": "v2.10.0",
|
||||||
|
"v2.9.1": "v2.9.0/v2.9.1",
|
||||||
|
"v2.8.0": "v2.8.0",
|
||||||
|
"v2.7.0": "v2.7.0",
|
||||||
|
"v2.6.0": "v2.6.0",
|
||||||
|
"v2.5.1": "v2.5.0/v2.5.1",
|
||||||
|
"v2.4.0": "v2.4.0/v2.4.1",
|
||||||
|
"v2.3.0": "v2.3.0",
|
||||||
|
"v2.2.0": "v2.2.0/v2.2.1/v2.2.2",
|
||||||
|
"v2.1.1": "v2.1.1",
|
||||||
|
"v2.0.0": "v2.0.0",
|
||||||
|
"v1.2.0": "v1.2.0",
|
||||||
|
"v1.1.0": "v1.1.0",
|
||||||
|
"v1.0.0": "v1.0.0"
|
||||||
|
}
|
||||||
|
|
||||||
function addIcon() {
|
function addIcon() {
|
||||||
const huggingFaceLogo = "https://huggingface.co/landing/assets/transformers-docs/huggingface_logo.svg";
|
const huggingFaceLogo = "https://huggingface.co/landing/assets/transformers-docs/huggingface_logo.svg";
|
||||||
const image = document.createElement("img");
|
const image = document.createElement("img");
|
||||||
@@ -58,6 +81,60 @@ function addGithubButton() {
|
|||||||
document.querySelector(".wy-side-nav-search .icon-home").insertAdjacentHTML('afterend', div);
|
document.querySelector(".wy-side-nav-search .icon-home").insertAdjacentHTML('afterend', div);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function addVersionControl() {
|
||||||
|
// To grab the version currently in view, we parse the url
|
||||||
|
const parts = location.toString().split('/');
|
||||||
|
let versionIndex = parts.length - 2
|
||||||
|
// Main classes and models are nested so we need to go deeper
|
||||||
|
if (parts[versionIndex] == "main_classes" || parts[versionIndex] == "model_doc") {
|
||||||
|
versionIndex = parts.length - 3
|
||||||
|
}
|
||||||
|
const version = parts[versionIndex];
|
||||||
|
|
||||||
|
// Menu with all the links,
|
||||||
|
const versionMenu = document.createElement("div");
|
||||||
|
|
||||||
|
const htmlLines = [];
|
||||||
|
for (const [key, value] of Object.entries(versionMapping)) {
|
||||||
|
var urlParts = (key == "") ? [] : [key];
|
||||||
|
urlParts = urlParts.concat(parts.slice(versionIndex));
|
||||||
|
htmlLines.push(`<a href="${urlParts.join('/')}">${value}</a>`);
|
||||||
|
}
|
||||||
|
|
||||||
|
versionMenu.classList.add("version-dropdown");
|
||||||
|
versionMenu.innerHTML = htmlLines.join('\n');
|
||||||
|
|
||||||
|
// Button for version selection
|
||||||
|
const versionButton = document.createElement("div");
|
||||||
|
versionButton.classList.add("version-button");
|
||||||
|
let label = (version == "transformers") ? stableVersion : version
|
||||||
|
versionButton.innerText = label.concat(" ▼");
|
||||||
|
|
||||||
|
// Toggle the menu when we click on the button
|
||||||
|
versionButton.addEventListener("click", () => {
|
||||||
|
versionMenu.classList.toggle("version-show");
|
||||||
|
});
|
||||||
|
|
||||||
|
// Hide the menu when we click elsewhere
|
||||||
|
window.addEventListener("click", (event) => {
|
||||||
|
if (event.target != versionButton){
|
||||||
|
versionMenu.classList.remove('version-show');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Container
|
||||||
|
const div = document.createElement("div");
|
||||||
|
div.appendChild(versionButton);
|
||||||
|
div.appendChild(versionMenu);
|
||||||
|
div.style.paddingTop = '25px';
|
||||||
|
div.style.backgroundColor = '#6670FF';
|
||||||
|
div.style.display = 'block';
|
||||||
|
div.style.textAlign = 'center';
|
||||||
|
|
||||||
|
const scrollDiv = document.querySelector(".wy-side-scroll");
|
||||||
|
scrollDiv.insertBefore(div, scrollDiv.children[1]);
|
||||||
|
}
|
||||||
|
|
||||||
function addHfMenu() {
|
function addHfMenu() {
|
||||||
const div = `
|
const div = `
|
||||||
<div class="menu">
|
<div class="menu">
|
||||||
@@ -149,6 +226,7 @@ function parseGithubButtons (){"use strict";var e=window.document,t=e.location,o
|
|||||||
|
|
||||||
function onLoad() {
|
function onLoad() {
|
||||||
addIcon();
|
addIcon();
|
||||||
|
addVersionControl();
|
||||||
addCustomFooter();
|
addCustomFooter();
|
||||||
addGithubButton();
|
addGithubButton();
|
||||||
parseGithubButtons();
|
parseGithubButtons();
|
||||||
|
|||||||
@@ -187,8 +187,8 @@ epub_title = project
|
|||||||
epub_exclude_files = ['search.html']
|
epub_exclude_files = ['search.html']
|
||||||
|
|
||||||
def setup(app):
|
def setup(app):
|
||||||
app.add_stylesheet('css/huggingface.css')
|
app.add_css_file('css/huggingface.css')
|
||||||
app.add_stylesheet('css/code-snippets.css')
|
app.add_css_file('css/code-snippets.css')
|
||||||
app.add_js_file('js/custom.js')
|
app.add_js_file('js/custom.js')
|
||||||
|
|
||||||
# -- Extension configuration -------------------------------------------------
|
# -- Extension configuration -------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user