1
0
mirror of https://github.com/cotes2020/jekyll-theme-chirpy.git synced 2026-08-06 05:47:39 +00:00
Files
jekyll-theme-chirpy/_javascript/modules/components/locale-datetime.js
Yura 01c62bc636 fix(dom): add missing datetime attribute on time element (#2759)
The datetime attribute was missing from the time element, which is
recommended for semantic HTML. Add it using the date_to_xmlschema
Liquid filter to ensure proper ISO 8601 format.
2026-06-29 07:16:49 +08:00

39 lines
1.0 KiB
JavaScript

/**
* Update month/day to locale datetime
*
* Requirement: <https://github.com/iamkun/dayjs>
*/
/* A tool for locale datetime */
class LocaleHelper {
static datetimeAttr = 'datetime';
static get locale() {
return document.documentElement.getAttribute('lang').substring(0, 2);
}
static getDatetime(elem) {
return elem.getAttribute(this.datetimeAttr);
}
}
export function initLocaleDatetime() {
dayjs.locale(LocaleHelper.locale);
dayjs.extend(window.dayjs_plugin_localizedFormat);
document
.querySelectorAll(`[${LocaleHelper.datetimeAttr}]`)
.forEach((elem) => {
const date = dayjs(LocaleHelper.getDatetime(elem));
elem.textContent = date.format(elem.dataset.df);
delete elem.dataset.df;
// setup tooltips
if ('bsToggle' in elem.dataset && elem.dataset.bsToggle === 'tooltip') {
// see: https://day.js.org/docs/en/display/format#list-of-localized-formats
const tooltipText = date.format('llll');
elem.dataset.bsTitle = tooltipText;
}
});
}