perf(core): speed up the Jekyll build times (#1163)

Refactor `_includes/related-posts.html`
This commit is contained in:
Cotes Chung 2023-08-08 06:47:36 +08:00
parent 54e1dbe325
commit 0d4103d47b
No known key found for this signature in database
GPG key ID: 0D9E54843167A808

View file

@ -1,7 +1,4 @@
<!-- <!-- Recommend the other 3 posts according to the tags and categories of the current post. -->
Recommend the other 3 posts according to the tags and categories of the current post,
if the number is not enough, use the other latest posts to supplement.
-->
<!-- The total size of related posts --> <!-- The total size of related posts -->
{% assign TOTAL_SIZE = 3 %} {% assign TOTAL_SIZE = 3 %}
@ -14,11 +11,21 @@
{% assign SEPARATOR = ':' %} {% assign SEPARATOR = ':' %}
{% assign match_posts = '' | split: '' %}
{% for category in page.categories %}
{% assign match_posts = match_posts | push: site.categories[category] | uniq %}
{% endfor %}
{% for tag in page.tags %}
{% assign match_posts = match_posts | push: site.tags[tag] | uniq %}
{% endfor %}
{% assign last_index = match_posts.size | minus: 1 %}
{% assign score_list = '' | split: '' %} {% assign score_list = '' | split: '' %}
{% assign last_index = site.posts.size | minus: 1 %}
{% for i in (0..last_index) %} {% for i in (0..last_index) %}
{% assign post = site.posts[i] %} {% assign post = match_posts[i] %}
{% if post.url == page.url %} {% if post.url == page.url %}
{% continue %} {% continue %}
@ -54,34 +61,20 @@
{% endfor %} {% endfor %}
{% endif %} {% endif %}
<!-- Fill with the other newlest posts --> {% assign relate_posts = '' | split: '' %}
{% assign less = TOTAL_SIZE | minus: index_list.size %}
{% if less > 0 %} {% for index in index_list %}
{% for i in (0..last_index) %} {% assign i = index | to_integer %}
{% assign post = site.posts[i] %} {% assign relate_posts = relate_posts | push: match_posts[i] %}
{% if post.url != page.url %}
{% capture cur_index %}{{ i }}{% endcapture %}
{% unless index_list contains cur_index %}
{% assign index_list = index_list | push: cur_index %}
{% assign less = less | minus: 1 %}
{% if less <= 0 %}
{% break %}
{% endif %}
{% endunless %}
{% endif %}
{% endfor %} {% endfor %}
{% endif %}
{% if index_list.size > 0 %} {% if relate_posts.size > 0 %}
<div id="related-posts" class="mb-2 mb-sm-4"> <div id="related-posts" class="mb-2 mb-sm-4">
<h3 class="pt-2 mb-4 ms-1" data-toc-skip> <h3 class="pt-2 mb-4 ms-1" data-toc-skip>
{{ site.data.locales[include.lang].post.relate_posts }} {{ site.data.locales[include.lang].post.relate_posts }}
</h3> </h3>
<div class="row row-cols-1 row-cols-md-2 row-cols-xl-3 g-4 mb-4"> <div class="row row-cols-1 row-cols-md-2 row-cols-xl-3 g-4 mb-4">
{% for entry in index_list %} {% for post in relate_posts %}
{% assign index = entry | plus: 0 %}
{% assign post = site.posts[index] %}
<div class="col"> <div class="col">
<a href="{{ post.url | relative_url }}" class="card post-preview h-100"> <a href="{{ post.url | relative_url }}" class="card post-preview h-100">
<div class="card-body"> <div class="card-body">