Skip to content

Commit 6d35f5f

Browse files
committed
perf: avoid the layout shift for post datetime
1 parent 42c44a8 commit 6d35f5f

20 files changed

+87
-160
lines changed

_includes/datetime.html

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!--
2+
Date format snippet
3+
See: ${JS_ROOT}/utils/locale-dateime.js
4+
-->
5+
6+
{% assign wrap_elem = include.wrap | default: 'em' %}
7+
8+
{% if site.prefer_datetime_locale == 'en' or lang == 'en' %}
9+
{% assign df_strftime = '%b %e, %Y' %}
10+
{% assign df_dayjs = 'll' %}
11+
{% else %}
12+
{% assign df_strftime = '%F' %}
13+
{% assign df_dayjs = 'YYYY-MM-DD' %}
14+
{% endif %}
15+
16+
<{{ wrap_elem }} class="{% if include.class %}{{ include.class }}{% endif %}"
17+
data-ts="{{ include.date | date: '%s' }}"
18+
data-df="{{ df_dayjs }}"
19+
{% if include.tooltip %}data-toggle="tooltip" data-placement="bottom"{% endif %}>
20+
{{ include.date | date: df_strftime }}
21+
</{{ wrap_elem }}>

_includes/related-posts.html

+1-4
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
{% assign less = TOTAL_SIZE | minus: index_list.size %}
6161

6262
{% if less > 0 %}
63-
6463
{% for i in (0..last_index) %}
6564
{% assign post = site.posts[i] %}
6665
{% if post.url != page.url %}
@@ -74,10 +73,8 @@
7473
{% endunless %}
7574
{% endif %}
7675
{% endfor %}
77-
7876
{% endif %}
7977

80-
8178
{% if index_list.size > 0 %}
8279
<div id="related-posts" class="mt-5 mb-2 mb-sm-4">
8380
<h3 class="pt-2 mt-1 mb-4 ml-1"
@@ -89,7 +86,7 @@
8986
<div class="card">
9087
<a href="{{ post.url | relative_url }}">
9188
<div class="card-body">
92-
{% include timeago.html date=post.date class="small" %}
89+
{% include datetime.html date=post.date class="small" %}
9390
<h3 class="pt-0 mt-1 mb-3" data-toc-skip>{{ post.title }}</h3>
9491
<div class="text-muted small">
9592
<p>

_includes/timeago.html

-15
This file was deleted.

_javascript/commons/locale-helper.js

-19
This file was deleted.

_javascript/utils/locale-datetime.js

+28-3
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,42 @@
44
* Requirement: <https://github.com/iamkun/dayjs>
55
*/
66

7+
/* A tool for locale datetime */
8+
const LocaleHelper = (function () {
9+
const $preferLocale = $('meta[name="prefer-datetime-locale"]');
10+
const locale = $preferLocale.length > 0 ?
11+
$preferLocale.attr('content').toLowerCase() : $('html').attr('lang').substr(0, 2);
12+
const attrTimestamp = 'data-ts';
13+
const attrDateFormat = 'data-df';
14+
15+
return {
16+
locale: () => locale,
17+
attrTimestamp: () => attrTimestamp,
18+
attrDateFormat: () => attrDateFormat,
19+
getTimestamp: ($elem) => Number($elem.attr(attrTimestamp)), // unix timestamp
20+
getDateFormat: ($elem) => $elem.attr(attrDateFormat)
21+
};
22+
23+
}());
24+
725
$(function() {
826
dayjs.locale(LocaleHelper.locale());
927
dayjs.extend(window.dayjs_plugin_localizedFormat);
1028

1129
$(`[${LocaleHelper.attrTimestamp()}]`).each(function () {
1230
const date = dayjs.unix(LocaleHelper.getTimestamp($(this)));
13-
const df = LocaleHelper.getDateFormat($(this));
14-
const text = date.format(df);
15-
31+
const text = date.format(LocaleHelper.getDateFormat($(this)));
1632
$(this).text(text);
1733
$(this).removeAttr(LocaleHelper.attrTimestamp());
1834
$(this).removeAttr(LocaleHelper.attrDateFormat());
35+
36+
// setup tooltips
37+
const tooltip = $(this).attr('data-toggle');
38+
if (typeof tooltip === 'undefined' || tooltip !== 'tooltip') {
39+
return;
40+
}
41+
42+
const tooltipText = date.format('llll'); // see: https://day.js.org/docs/en/display/format#list-of-localized-formats
43+
$(this).attr('data-original-title', tooltipText);
1944
});
2045
});

_javascript/utils/timeago.js

-87
This file was deleted.

_layouts/archives.html

+15-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33
# The Archives of posts.
44
---
55

6-
<div id="archives" class="pl-xl-2">
6+
{% include lang.html %}
7+
8+
{% if site.prefer_datetime_locale == 'en' or lang == 'en' %}
9+
{% assign df_strftime_m = '%b' %}
10+
{% assign df_dayjs_m = 'MMM' %}
11+
{% else %}
12+
{% assign df_strftime_m = '/ %m' %}
13+
{% assign df_dayjs_m = '/ MM' %}
14+
{% endif %}
715

16+
<div id="archives" class="pl-xl-2">
817
{% for post in site.posts %}
918
{% capture this_year %}{{ post.date | date: "%Y" }}{% endcapture %}
1019
{% capture pre_year %}{{ post.previous.date | date: "%Y" }}{% endcapture %}
@@ -17,9 +26,11 @@
1726
<li>
1827
<div>
1928
{% assign ts = post.date | date: '%s' %}
20-
<span class="date day" data-ts="{{ ts }}" data-df="DD">{{ post.date | date: "%d" }}</span>
21-
<span class="date month small text-muted" data-ts="{{ ts }}" data-df="MMM">
22-
{{ post.date | date: '%m' }}
29+
<span class="date day" data-ts="{{ ts }}" data-df="DD">
30+
{{ post.date | date: "%d" }}
31+
</span>
32+
<span class="date month small text-muted" data-ts="{{ ts }}" data-df="{{ df_dayjs_m }}">
33+
{{ post.date | date: df_strftime_m }}
2334
</span>
2435
<a href="{{ post.url | relative_url }}">{{ post.title }}</a>
2536
</div>

_layouts/category.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# The Category layout
44
---
55

6+
{% include lang.html %}
7+
68
<div id="page-category">
79
<h1 class="pl-lg-2">
810
<i class="far fa-folder-open fa-fw text-muted"></i>
@@ -15,9 +17,7 @@ <h1 class="pl-lg-2">
1517
<li class="d-flex justify-content-between pl-md-3 pr-md-3">
1618
<a href="{{ post.url | relative_url }}">{{ post.title }}</a>
1719
<span class="dash flex-grow-1"></span>
18-
<span class="text-muted small" data-ts="{{ post.date | date: '%s' }}" data-df="ll">
19-
{{ post.date | date: '%Y-%m-%d' }}
20-
</span>
20+
{% include datetime.html date=post.date wrap='span' class='text-muted small' %}
2121
</li>
2222
{% endfor %}
2323
</ul>

_layouts/home.html

+3-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
{% assign pinned_num = 0 %}
2424
{% endif %}
2525

26-
2726
<!-- Get default posts -->
2827

2928
{% assign default_beg = offset | minus: pinned.size %}
@@ -58,15 +57,11 @@ <h1>
5857
</div>
5958

6059
<div class="post-meta text-muted d-flex">
61-
6260
<div class="mr-auto">
61+
6362
<!-- posted date -->
6463
<i class="far fa-calendar fa-fw"></i>
65-
{% include timeago.html date=post.date tooltip=true capitalize=true %}
66-
67-
<!-- time to read -->
68-
<i class="far fa-clock fa-fw"></i>
69-
{% include read-time.html content=post.content %}
64+
{% include datetime.html date=post.date %}
7065

7166
<!-- categories -->
7267
{% if post.categories.size > 0 %}
@@ -78,6 +73,7 @@ <h1>
7873
{% endfor %}
7974
</span>
8075
{% endif %}
76+
8177
</div>
8278

8379
{% if post.pin %}

_layouts/post.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ <h1 data-toc-skip>{{ page.title }}</h1>
6161
<!-- published date -->
6262
<span>
6363
{{ site.data.locales[lang].post.posted }}
64-
{% include timeago.html date=page.date tooltip=true %}
64+
{% include datetime.html date=page.date tooltip=true %}
6565
</span>
6666

6767
<!-- lastmod date -->
6868
{% if page.last_modified_at %}
6969
<span>
7070
{{ site.data.locales[lang].post.updated }}
71-
{% include timeago.html date=page.last_modified_at tooltip=true %}
71+
{% include datetime.html date=page.last_modified_at tooltip=true %}
7272
</span>
7373
{% endif %}
7474

_layouts/tag.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# The layout for Tag page
44
---
55

6+
{% include lang.html %}
7+
68
<div id="page-tag">
79
<h1 class="pl-lg-2">
810
<i class="fa fa-tag fa-fw text-muted"></i>
@@ -14,9 +16,7 @@ <h1 class="pl-lg-2">
1416
<li class="d-flex justify-content-between pl-md-3 pr-md-3">
1517
<a href="{{ post.url | relative_url }}">{{ post.title }}</a>
1618
<span class="dash flex-grow-1"></span>
17-
<span class="text-muted small" data-ts="{{ post.date | date: '%s' }}" data-df="ll">
18-
{{ post.date | date: '%Y-%m-%d' }}
19-
</span>
19+
{% include datetime.html date=post.date wrap='span' class='text-muted small' %}
2020
</li>
2121
{% endfor %}
2222
</ul>

_sass/layout/post.scss

+2-4
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ nav[data-toggle=toc] {
173173

174174
em {
175175
@extend %normal-font-style;
176+
177+
color: var(--relate-post-date);
176178
}
177179

178180
.card {
@@ -194,10 +196,6 @@ nav[data-toggle=toc] {
194196
}
195197
}
196198

197-
.timeago {
198-
color: var(--relate-post-date);
199-
}
200-
201199
p {
202200
font-size: 0.9rem;
203201
margin-bottom: 0.5rem;

assets/js/dist/categories.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)