Skip to content

Instantly share code, notes, and snippets.

@michimani
Last active July 8, 2019 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michimani/be15135cac49415fdd7edb1e1acb5db8 to your computer and use it in GitHub Desktop.
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.
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