Skip to content

Commit

Permalink
fix(docs): ensure version dropdown sorts correctly regardless of brow…
Browse files Browse the repository at this point in the history
…ser language #20289 (#20322)

Signed-off-by: jaehanbyun <awbrg789@naver.com>
  • Loading branch information
jaehanbyun authored Oct 17, 2024
1 parent c216ece commit d1ef0f8
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions docs/assets/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const observerOptions = {
subtree: true
};

const VERSION_REGEX = /\/en\/(release-(?:v\d+|[\d\.]+|\w+)|latest|stable)\//;

const observerCallback = function(mutationsList, observer) {
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
Expand All @@ -20,7 +22,7 @@ const observer = new MutationObserver(observerCallback);
observer.observe(targetNode, observerOptions);

function getCurrentVersion() {
const currentVersion = window.location.href.match(/\/en\/(release-(?:v\d+|[\d\.]+|\w+)|latest|stable)\//);
const currentVersion = window.location.href.match(VERSION_REGEX);
if (currentVersion && currentVersion.length > 1) {
return currentVersion[1];
}
Expand Down Expand Up @@ -80,11 +82,16 @@ function sortVersionLinks(container) {
const dlElements = container.querySelectorAll('dl');

dlElements.forEach(dl => {
const dt = dl.querySelector('dt');
if (dt && dt.textContent.trim().toLowerCase() === 'versions') {
// Found the Versions <dl>
const ddElements = Array.from(dl.querySelectorAll('dd'));
const ddElements = Array.from(dl.querySelectorAll('dd'));

// Check if ddElements contain version links
const isVersionDl = ddElements.some(dd => {
const link = dd.querySelector('a');
return VERSION_REGEX.test(link?.getAttribute?.('href'));
});

// This dl contains version links, proceed to sort
if (isVersionDl) {
// Define sorting criteria
ddElements.sort((a, b) => {
const aText = a.textContent.trim().toLowerCase();
Expand All @@ -103,7 +110,7 @@ function sortVersionLinks(container) {
if (aVersionMatch && bVersionMatch) {
const aVersion = aVersionMatch[1].split('.').map(Number);
const bVersion = bVersionMatch[1].split('.').map(Number);

for (let i = 0; i < Math.max(aVersion.length, bVersion.length); i++) {
const aNum = aVersion[i] || 0;
const bNum = bVersion[i] || 0;
Expand Down

0 comments on commit d1ef0f8

Please sign in to comment.