Optimize related include (#33104)

This commit is contained in:
Joakim Sørensen 2024-06-06 17:26:25 +02:00 committed by GitHub
parent e986103ebf
commit 5f0c3ff81b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 10 deletions

27
plugins/site_pages.rb Normal file
View File

@ -0,0 +1,27 @@
module Jekyll
class SitePagesGenerator < Jekyll::Generator
def generate(site)
all_pages = Array.new
site.collections.each do |name, collection|
all_pages.concat(collection.docs)
end
site.data["site_pages"] = all_pages
.concat(site.pages)
.concat(site.documents)
.map { |entry|
[
entry.url.to_s,
{
"title" => entry["title"],
"description" => entry["description"],
"url" => entry.url,
"relative_path" => entry.relative_path
}
]
}
.to_h
end
end
end

View File

@ -11,23 +11,19 @@
<div class="text related-topics">
<h2><a class="title-link" href="#related-topics" name="related-topics"></a>Related topics</h2>
<ul>
{% assign site_part = "pages,documents" | split: "," %}
{%- for related in related_topics -%}
{%- for part in site_part -%}
{%- for entry in site[part] -%}
{% assign computed = related.docs | split: "#" %}
{% assign target_url = computed[0] %}
{% assign target_page = site.data["site_pages"][target_url] %}
{% unless target_page == null %}
{% assign link_target = computed[1] %}
{% assign suggested_title = related.title | default: link_target | split: "-" | join: " " | capitalize %}
{%- if computed[0] == entry.url -%}
<li>
<a href="{{ entry.url | relative_url }}{% if link_target %}#{{link_target}}{% endif %}" class="topic">
{{ suggested_title | default: entry.title}}
<a href="{{ target_page['url'] | relative_url }}{% if link_target %}#{{link_target}}{% endif %}" class="topic">
{{ suggested_title | default: target_page["title"]}}
</a>
</li>
{% break %}{% break %}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{% endunless %}
{%- endfor -%}
</ul>
</div>