From d1ef0f85fafec00bf84ccde0085ec7f9329e7843 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B3=80=EC=9E=AC=ED=95=9C?= Date: Thu, 17 Oct 2024 23:06:44 +0900 Subject: [PATCH] fix(docs): ensure version dropdown sorts correctly regardless of browser language #20289 (#20322) Signed-off-by: jaehanbyun --- docs/assets/versions.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/docs/assets/versions.js b/docs/assets/versions.js index af95a7140a442..c1a99d0287c6a 100644 --- a/docs/assets/versions.js +++ b/docs/assets/versions.js @@ -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') { @@ -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]; } @@ -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
- 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(); @@ -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;