Skip to content

Add warning for stale version of documentation #2012

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

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions assets/css/custom-props/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
--black: hsl(0, 0%, 0%);
--black-opacity-10: hsla(0, 0%, 0%, 10%);
--black-opacity-50: hsla(0, 0%, 0%, 50%);
--orangeDark: hsl(30, 90%, 40%);
--orangeLight: hsl(30, 80%, 50%);
}

@media screen and (max-width: 768px) {
Expand Down
1 change: 1 addition & 0 deletions assets/css/custom-props/theme-dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ body.dark {
--sidebarHover: var(--white);
--sidebarScrollbarThumb: var(--coldGray);
--sidebarScrollbarTrack: var(--sidebarBackground);
--sidebarStaleVersion: var(--orangeLight);
--sidebarSubheadings: var(--gray400);
--sidebarItem: var(--gray200);
--sidebarInactiveItemBorder: var(--gray400);
Expand Down
1 change: 1 addition & 0 deletions assets/css/custom-props/theme-light.css
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
--sidebarHover: var(--black);
--sidebarScrollbarThumb: var(--coldGrayLight);
--sidebarScrollbarTrack: var(--sidebarBackground);
--sidebarStaleVersion: var(--orangeDark);
--sidebarSubheadings: var(--black);
--sidebarItem: var(--black);
--sidebarInactiveItemBorder: var(--gray500);
Expand Down
16 changes: 16 additions & 0 deletions assets/css/sidebar.css
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@
display: none;
}

.sidebar .sidebar-staleVersion {
display: inline;
}

.sidebar .sidebar-staleVersion > a {
color: var(--sidebarStaleVersion);
font-weight: 400;
}

.sidebar .sidebar-staleIcon {
font-size: 18px;
position: relative;
top: 3px;
line-height: 0;
}

.sidebar .sidebar-list-nav {
display: flex;
margin: 0;
Expand Down
8 changes: 8 additions & 0 deletions assets/js/handlebars/templates/versions-dropdown.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@
{{/each}}
</select>
</label>
{{#if latestVersion}}
<div class="sidebar-staleVersion">
<a href="{{latestVersion}}" title="This version of package is not the latest version">
<i class="ri-alert-line sidebar-staleIcon" aria-hidden="true"></i>
<span>Go to latest</span>
</a>
</div>
{{/if}}
</form>
24 changes: 22 additions & 2 deletions assets/js/sidebar/sidebar-version-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,32 @@ if (!isEmbedded) {
isCurrentVersion: node.version === currentVersion
}))

versionsContainer.innerHTML = versionsDropdownTemplate({ nodes })
const latestVersionNode = versionNodes.find(node => node.latest)
const latestVersion = latestVersionNode?.version !== currentVersion ? latestVersionNode?.url : null

qs(VERSIONS_DROPDOWN_SELECTOR).addEventListener('change', handleVersionSelected)
versionsContainer.innerHTML = versionsDropdownTemplate({ nodes, latestVersion})

const select = qs(VERSIONS_DROPDOWN_SELECTOR)
select.addEventListener('change', handleVersionSelected)
adjustWidth(select)
}
}

// Function to adjust the width of the select element
function adjustWidth (select) {
// Create a temporary element to measure the width
const temp = document.createElement('span')
temp.style.visibility = 'hidden'
temp.style.position = 'absolute'
temp.style.whiteSpace = 'nowrap'
temp.style.font = window.getComputedStyle(select).font
temp.textContent = select.options[select.selectedIndex].text

document.body.appendChild(temp)
select.style.width = `${temp.offsetWidth + 20}px`
document.body.removeChild(temp)
}

function handleVersionSelected (event) {
Copy link
Member

Choose a reason for hiding this comment

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

handleVersionSelected makes sure that when we are on, say, https://hexdocs.pm/elixir/1.18.0/Task.html and from version selector we choose v1.18.1, we'll end up at https://hexdocs.pm/elixir/1.18.1/Task.html.

I think it'd be nice for "Go to latest" to work similarly, preserve relative path.

I'm not very well versed in JS but this is a PoC (copy pasting the implementation with minor changes) for this functionality:

diff --git a/assets/js/sidebar/sidebar-version-select.js b/assets/js/sidebar/sidebar-version-select.js
index 48968c88..de80813f 100644
--- a/assets/js/sidebar/sidebar-version-select.js
+++ b/assets/js/sidebar/sidebar-version-select.js
@@ -34,6 +34,9 @@ if (!isEmbedded) {
     const select = qs(VERSIONS_DROPDOWN_SELECTOR)
     select.addEventListener('change', handleVersionSelected)
     adjustWidth(select)
+
+    const versionsGoToLatest = qs(".sidebar-staleVersion a")
+    versionsGoToLatest.addEventListener('click', handleGoToLatestClicked)
   }
 }

@@ -67,6 +70,22 @@ function handleVersionSelected (event) {
     })
 }

+function handleGoToLatestClicked (event) {
+  const url = this.href
+  const pathSuffix = window.location.pathname.split('/').pop() + window.location.hash
+  const otherVersionWithPath = `${url}/${pathSuffix}`
+  event.preventDefault();
+
+  checkUrlExists(otherVersionWithPath)
+    .then(exists => {
+      if (exists) {
+        window.location.href = otherVersionWithPath
+      } else {
+        window.location.href = url
+      }
+    })
+}

@alisinabh if this piques your interest perhaps you could send another PR?

const url = event.target.value
const pathSuffix = window.location.pathname.split('/').pop() + window.location.hash
Expand Down