Sha256: 2f2c7eaf46639816573a0c3d8b1ef16cd311ad11512f2f364aea105be32f53fe

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

/**
 * Update month/day to locale datetime
 *
 * Requirement: <https://github.com/iamkun/dayjs>
 */

/* A tool for locale datetime */
const LocaleHelper = (function () {
  const $preferLocale = $('meta[name="prefer-datetime-locale"]');
  const locale = $preferLocale.length > 0 ?
      $preferLocale.attr('content').toLowerCase() : $('html').attr('lang').substr(0, 2);
  const attrTimestamp = 'data-ts';
  const attrDateFormat = 'data-df';

  return {
    locale: () => locale,
    attrTimestamp: () => attrTimestamp,
    attrDateFormat: () => attrDateFormat,
    getTimestamp: ($elem) => Number($elem.attr(attrTimestamp)),  // unix timestamp
    getDateFormat: ($elem) => $elem.attr(attrDateFormat)
  };

}());

$(function() {
  dayjs.locale(LocaleHelper.locale());
  dayjs.extend(window.dayjs_plugin_localizedFormat);

  $(`[${LocaleHelper.attrTimestamp()}]`).each(function () {
    const date = dayjs.unix(LocaleHelper.getTimestamp($(this)));
    const text = date.format(LocaleHelper.getDateFormat($(this)));
    $(this).text(text);
    $(this).removeAttr(LocaleHelper.attrTimestamp());
    $(this).removeAttr(LocaleHelper.attrDateFormat());

    // setup tooltips
    const tooltip = $(this).attr('data-toggle');
    if (typeof tooltip === 'undefined' || tooltip !== 'tooltip') {
      return;
    }

    const tooltipText = date.format('llll'); // see: https://day.js.org/docs/en/display/format#list-of-localized-formats
    $(this).attr('data-original-title', tooltipText);
  });
});

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redeyed-jekyll-theme-0.1.2 _javascript/utils/locale-datetime.js
redeyed-jekyll-theme-0.1.1 _javascript/utils/locale-datetime.js