Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.