feat: allow pinned posts with pagination (#428)

This commit is contained in:
Óscar
2024-11-14 00:24:00 +01:00
committed by GitHub
parent 5927409c41
commit d7da6feaa4
6 changed files with 62 additions and 19 deletions

View File

@@ -3,30 +3,42 @@
{# It would also work with arrays (e.g. ["dates"] or ["indexes"] or even ["indexes","dates"]). #}
{# Nevertheless, arrays cannot be used as a default value for a macro parameter in Tera (see https://github.com/Keats/tera/issues/710). #}
{# `paginator` is only used to compute indexes metadata and can be let empty otherwise. #}
{% macro list_posts(posts, max, metadata="dates", language_strings="", section_path="blog", paginator="", pinned_first=false) %}
{% macro list_posts(posts, all_posts="", max=999999, metadata="dates", language_strings="", section_path="blog", paginator="", pinned_first=false, current_page=1) %}
{%- set separator = config.extra.separator | default(value="•") -%}
{# Separate pinned and regular posts #}
{% set all_posts = [] %}
{# Separate pinned and regular posts from all_posts if available, otherwise from posts #}
{% if pinned_first %}
{% set source_posts = all_posts | default(value=posts) %}
{% set pinned_posts = [] %}
{% set regular_posts = [] %}
{% for post in posts %}
{% for post in source_posts %}
{% if post.extra.pinned %}
{% set_global pinned_posts = pinned_posts | concat(with=post) %}
{% else %}
{% set_global regular_posts = regular_posts | concat(with=post) %}
{% endif %}
{% endfor %}
{% set all_posts = pinned_posts | concat(with=regular_posts) %}
{# On page 1 or when no pagination, show pinned then regular #}
{% if current_page == 1 %}
{% if paginator %}
{# With pagination: pinned + current page's posts #}
{% set display_posts = pinned_posts | concat(with=posts) %}
{% else %}
{# Without pagination: pinned + regular (no duplicates) #}
{% set display_posts = pinned_posts | concat(with=regular_posts) %}
{% endif %}
{% else %}
{% set display_posts = posts %}
{% endif %}
{% else %}
{% set all_posts = posts %}
{% set display_posts = posts %}
{% endif %}
<div class="bloglist-container">
{# Display all posts (pinned first, then regular) #}
{% for post in all_posts %}
{# Display posts #}
{% for post in display_posts %}
{% if loop.index <= max %}
{% if loop.index == max or loop.last %}
{% set bottom_divider = false %}

View File

@@ -9,7 +9,7 @@
{%- set paginator_has_no_pages = paginator and paginator.pages | length == 0 -%}
{%- set extra_section_has_pages = extra_section and extra_section.pages | length > 0 -%}
{# Display a warning if both settings are set, paginator has no pages, but extra section does #}
{# Display warning if both settings are set #}
{%- if both_settings_set and paginator_has_no_pages and extra_section_has_pages -%}
<div class="admonition warning">
<div class="admonition-icon admonition-icon-warning"></div>
@@ -30,14 +30,29 @@
</div>
{%- endif -%}
{%- if paginator %}
{# Get all posts for pinning if we're in root section with pagination #}
{%- if paginator and is_root_section -%}
{%- set root_section = get_section(path="_index.md") -%}
{%- set all_posts = root_section.pages -%}
{%- set pages = paginator.pages -%}
{% else %}
{%- elif paginator -%}
{%- set all_posts = paginator.pages -%}
{%- set pages = paginator.pages -%}
{%- else -%}
{%- set all_posts = extra_section.pages -%}
{%- set pages = extra_section.pages -%}
{% endif -%}
{%- endif -%}
{% set max_posts = section.extra.max_posts | default(value=999999) %}
{{ macros_list_posts::list_posts(posts=pages, max=max_posts, language_strings=language_strings, section_path=path, pinned_first=true) }}
{{ macros_list_posts::list_posts(
posts=pages,
all_posts=all_posts,
max=max_posts,
language_strings=language_strings,
section_path=path,
pinned_first=is_root_section,
current_page=paginator.current_index | default(value=1)
) }}
</div>
{% if paginator and paginator.pages | length > 0 %}

View File

@@ -2,6 +2,16 @@
{% block main_content %}
{# We'll only pin posts in the root section. #}
{# Right now both the main page and blog/ use the same `section.html` template. #}
{# To avoid using different templates, we do this. #}
{%- if lang == config.default_language -%}
{%- set expected_root = "/" -%}
{%- else -%}
{%- set expected_root = "/" ~ lang ~ "/" -%}
{%- endif -%}
{%- set is_root_section = current_path == expected_root -%}
{%- set show_projects_first = section.extra.show_projects_first | default(value=false) -%}
{%- if show_projects_first -%}
{%- set first_section = "projects" -%}