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
This commit is contained in:
Kyriet 2021-12-09 19:01:42 +01:00 committed by GitHub
parent 346c3b2289
commit efb545ae65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 2 deletions

View File

@ -155,5 +155,12 @@ module.exports = {
showLastUpdateTime: true,
}
]
],
scripts: [
{
src: '/js/localizeLastUpdatedDate.js',
async: true,
defer: true
}
]
};

View File

@ -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"
},

View File

@ -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);
}
})();