Last active
July 8, 2019 02:40
-
-
Save michimani/be15135cac49415fdd7edb1e1acb5db8 to your computer and use it in GitHub Desktop.
Show message in an entry generated by Hugo that notifier that entry is old.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function showInfoNotifiesOldPost() { | |
const nowDate = new Date(); | |
const oldTs = 60 * 60 * 24 * 365 * 1000; // 1 year (millisecond) | |
const nowTs = nowDate.getTime(); | |
const postDateElem = document.querySelector('p.post-date > time.dt-published'); | |
if (postDateElem !== null) { | |
try { | |
const postDateStr = postDateElem.getAttribute('datetime'); | |
const postDate = new Date(postDateStr); | |
const postTs = postDate.getTime(); | |
if (nowTs - postTs > oldTs) { | |
const entrySectionElem = document.querySelector('section.content.e-content'); | |
if (entrySectionElem !== null) { | |
const infoElem = '<div id="old-entry-notifier">この記事は公開されてから 1 年以上経過しています。情報が古い可能性がありますので、ご注意ください。</div>'; | |
entrySectionElem.insertAdjacentHTML('afterbegin', infoElem); | |
} | |
} | |
} catch { | |
console.warn('failed to get post date.'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment