From efb545ae65815f5f9869cee6d8ed691b39447487 Mon Sep 17 00:00:00 2001 From: Kyriet Date: Thu, 9 Dec 2021 19:01:42 +0100 Subject: [PATCH] Add a workroud for localized dates on the website (#1770) * Add a workroud for localized dates on the website * Remove options passed to toLocaleDateString * fixes #1748 --- website/docusaurus.config.js | 7 ++++ website/package.json | 4 +- website/static/js/localizeLastUpdatedDate.js | 39 ++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 website/static/js/localizeLastUpdatedDate.js diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index 0696d94b..3cd3915b 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -155,5 +155,12 @@ module.exports = { showLastUpdateTime: true, } ] + ], + scripts: [ + { + src: '/js/localizeLastUpdatedDate.js', + async: true, + defer: true + } ] }; diff --git a/website/package.json b/website/package.json index f16284e6..5ad32905 100644 --- a/website/package.json +++ b/website/package.json @@ -12,8 +12,8 @@ "clear": "docusaurus clear" }, "dependencies": { - "@docusaurus/core": "2.0.0-beta.6", - "@docusaurus/preset-classic": "2.0.0-beta.6", + "@docusaurus/core": "2.0.0-beta.9", + "@docusaurus/preset-classic": "2.0.0-beta.9", "react": "^17.0.1", "react-dom": "^17.0.1" }, diff --git a/website/static/js/localizeLastUpdatedDate.js b/website/static/js/localizeLastUpdatedDate.js new file mode 100644 index 00000000..b9708ddf --- /dev/null +++ b/website/static/js/localizeLastUpdatedDate.js @@ -0,0 +1,39 @@ +function waitForElementToChange(selector) { + return new Promise(resolve => { + const observer = new MutationObserver(() => { + const element = document.querySelector(selector) + if (element) { + resolve(element); + observer.disconnect(); + } + }); + + observer.observe(document.body, { + characterData: true, + childList: true, + subtree: true + }); + }); +} + +function localizeTimeElement(element) { + if (element instanceof HTMLTimeElement && element.hasAttribute('datetime')) { + const iso8601 = element.getAttribute('datetime'); + const date = new Date(iso8601); + element.textContent = date.toLocaleDateString(); + } +} + +(async() => { + while(true) { + const selector = '.theme-last-updated time'; + + // if it exists then take it + let timeElement = document.querySelector(selector); + localizeTimeElement(timeElement); + + // wait for element to change (or appear if it didn't exist) + timeElement = await waitForElementToChange(selector); + localizeTimeElement(timeElement); + } +})();