Skip to content

Commit

Permalink
Adds versioning support!
Browse files Browse the repository at this point in the history
  • Loading branch information
cadojo committed Jan 4, 2025
1 parent 02be919 commit ff13138
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 118 deletions.
5 changes: 5 additions & 0 deletions docs/src/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ bibliography: references.bib

format:
html:
resources:
- _static/versions.js
include-in-header:
text: <script type="text/javascript" src="_static/versions.js"></script>
code-link: true
number-sections: false
css: _static/style.css
theme:
light:
- _static/theme.scss
Expand Down
4 changes: 4 additions & 0 deletions docs/src/_static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.dropdown-item {
text-align: right;
display: block;
}
185 changes: 67 additions & 118 deletions docs/src/_static/versions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//
// The code below was originally written by the authors of Wflow.jl. Their
// license is provided below. Chat GPT was used to iterately modify the code.
//

// MIT License for Wflow.jl
//
// Copyright (c) 2020 Deltares and contributors
Expand All @@ -20,126 +25,70 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

function checkPathExists(url) {
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
xhr.open('HEAD', url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve(true);
} else if (xhr.status === 404) {
resolve(false);
} else {
reject(new Error(xhr.statusText));
}
}
};
xhr.onerror = function () {
reject(new Error('Network Error'));
};
xhr.send();
});
}

window.onload = function () {
// Dynamically load the `versions.js` script
const script = document.createElement('script');
script.src = 'https://raw.githubusercontent.com/cadojo/DocumenterQuarto.jl/gh-pages/versions.js'; // Replace with your actual path
script.async = true;

// Handle the script load event
script.onload = function () {
// Ensure the DOC_VERSIONS variable exists
if (typeof DOC_VERSIONS === 'undefined') {
console.error('DOC_VERSIONS variable is not defined in the script.');
return;
}

const dropdown = document.querySelector('#nav-menu-version').nextElementSibling;
console.log('Dropdown element:', dropdown); // Log the dropdown element

// Clear all existing dropdown items
dropdown.innerHTML = '';

// Process each version in the DOC_VERSIONS array
DOC_VERSIONS.forEach(
version => {
console.log('Adding version:', version);

// Create a new li element
const li = document.createElement('li');

// Create a new a element
const a = document.createElement('a');
a.className = 'dropdown-item';
a.href = "";
a.textContent = version;

// Add the a element to the li
li.appendChild(a);

// Add the li to the dropdown
dropdown.appendChild(li);
});

console.log('Dropdown after adding items:', dropdown); // Log the dropdown after adding items

// Get all dropdown items within the specific dropdown menu
var dropdownMenu = document.querySelector('#nav-menu-version').nextElementSibling;
const versionsUrl = 'https://raw.githubusercontent.com/cadojo/DocumenterQuarto.jl/gh-pages/versions.js';

var dropdownItems = dropdownMenu.querySelectorAll('.dropdown-item');

// Get the current page in chunks
var currentPagePath = window.location.pathname.split('/');

for (var i = 0; i < dropdownItems.length; i++) {
// Get textcontent
var textContent = dropdownItems[i].textContent;

// Get the index of the current version
var index = currentPagePath.indexOf(textContent);

if (index !== -1) {
// Remove the active-item class from all items
for (var j = 0; j < dropdownItems.length; j++) {
dropdownItems[j].classList.remove('active-item');
}

dropdownItems[i].classList.add('active-item');
break
// Fetch the versions.js file
fetch(versionsUrl)
.then(response => {
if (!response.ok) {
throw new Error(`Failed to fetch ${versionsUrl}: ${response.statusText}`);
}
}

console.log('current page path', currentPagePath);

for (var i = 0; i < dropdownItems.length; i++) {
// Add click event listener to each item
dropdownItems[i].addEventListener('click', function (event) {
// Prevent default action
event.preventDefault();

// Get the clicked item's text
var itemText = this.textContent;
var itemHref = this.getAttribute('href')

// Loop through each dropdown item again to find a match in the current page's path
for (var j = 0; j < dropdownItems.length; j++) {
// Get the dropdown item's text
var dropdownText = dropdownItems[j].textContent;
console.log('Dropdown item:', dropdownText);

window.location.href = itemHref;
return response.text();
})
.then(scriptContent => {
// Use a regular expression to find the DOC_VERSIONS array
const regex = /var DOC_VERSIONS = (\[.*?\]);/s;
const match = scriptContent.match(regex);

if (match && match[1]) {
// Remove trailing commas if they exist and fix the array syntax for JSON
let arrayString = match[1].trim();

// Remove any trailing comma before closing bracket
arrayString = arrayString.replace(/,\s*([\]}])/g, '$1');

// Parse the cleaned array string
try {
const DOC_VERSIONS = JSON.parse(arrayString);

console.log('DOC_VERSIONS loaded:', DOC_VERSIONS);

// Process DOC_VERSIONS as usual
const dropdown = document.querySelector('#nav-menu-version')?.nextElementSibling;
if (!dropdown) {
console.error('Dropdown element not found!');
return;
}

dropdown.innerHTML = ''; // Clear existing items
DOC_VERSIONS.forEach(version => {
const li = document.createElement('li');
const a = document.createElement('a');
a.className = 'dropdown-item';
a.href = `/${version}/index.html`;

// Wrap version text in a <code> element
const code = document.createElement('code');
code.textContent = version;

// Add <code> to <a>
a.appendChild(code);

li.appendChild(a); // Add <a> to <li>
dropdown.appendChild(li); // Add <li> to the dropdown
});

console.log('Dropdown populated:', dropdown);

} catch (error) {
console.error('Error parsing DOC_VERSIONS:', error);
}
});
}
};

// Handle script load errors
script.onerror = function () {
console.error('Failed to load versions.js');
};

// Append the script to the document
document.head.appendChild(script);
} else {
console.error('DOC_VERSIONS not found in script.');
}
})
.catch(error => {
console.error('Error fetching versions.js:', error);
});
};

0 comments on commit ff13138

Please sign in to comment.