Skip to content

Commit

Permalink
✨ feat: allow dual date-format (short/long)
Browse files Browse the repository at this point in the history
  • Loading branch information
welpo committed Mar 10, 2023
1 parent e93b33e commit 9887fb0
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 51 deletions.
9 changes: 7 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ highlight_code = true
highlight_theme = "css"

[extra]
# If unset, uses format: "6 July 2049" ("%d %B %Y"). Don't leave it empty or dates won't appear. Either use a proper format or comment out the line.
# date_format = "%d %B %Y"
# Date format used when listing posts (main page, /blog section, tag posts list…)
# Default is "6th July 2049".
long_date_format = ""

# Date format used for blog posts.
# Default is "31st Dec 2011".
short_date_format = ""

# Custom separator used in title tag and posts metadata (between date, time to read, and tags).
separator = ""
Expand Down
3 changes: 2 additions & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
{% import "macros/page_header.html" as macros_page_header %}
{% import "macros/page_desc.html" as macros_page_desc %}
{% import "macros/content.html" as macros_content %}
{% import "macros/cards_posts.html" as macros_cards_posts %}
{% import "macros/cards_pages.html" as macros_cards_pages %}
{% import "macros/set_title.html" as macros_set_title %}
{% import "macros/paginate.html" as macros_paginate %}
{% import "macros/format_date.html" as macros_format_date %}

<!DOCTYPE html>
<html lang = "en">
Expand Down
2 changes: 1 addition & 1 deletion templates/cards.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{%- set show_pages = section.pages -%}
{% endif -%}

{{ macros_cards_posts::cards_posts(pages=show_pages) }}
{{ macros_cards_pages::cards_pages(pages=show_pages) }}
</main>

{% if paginator %}
Expand Down
43 changes: 43 additions & 0 deletions templates/macros/cards_pages.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{% macro cards_pages(pages) %}

<div class="cards">
{%- for page in pages %}
<div class="card">
{% if page.extra.local_image %}
<img class="card-image" alt={{ page.extra.local_image }} src="{{ get_url(path=page.extra.local_image) }}">
{% elif page.extra.remote_image %}
<img class="card-image" alt={{ page.extra.remote_image }} src="{{ page.extra.remote_image }}">
{% else %}
<div class="card-image-placeholder"></div>
{% endif %}

<div class="card-info">
<h1 class="card-title">
{% if page.extra.link_to %}
<a rel="noopener noreferrer" target="_blank" href={{ page.extra.link_to }}>{{page.title}}</a>
{% else %}
<a href={{ page.permalink }}>{{page.title}}</a>
{% endif %}
</h1>

<div class="meta">
{% if page.date %}
{{ macros_format_date::format_date(date=page.date, short=false) }}
{% endif %}
{% if page.draft %}
<span class="draft-label">DRAFT</span>
{% endif %}
</div>

<div class="card-description">
{% if page.description %}
{{ page.description }}
{% endif %}
</div>
</div>
</div>

{% endfor -%}
</div>

{% endmacro cards_pages %}
43 changes: 0 additions & 43 deletions templates/macros/cards_posts.html

This file was deleted.

4 changes: 2 additions & 2 deletions templates/macros/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{% endif %}

{% if page.date %}
<li>{{ page.date | date(format=config.extra.date_format | default (value="%d %B %Y")) }} {{ separator }}</li>
<li>{{ macros_format_date::format_date(date=page.date, short=true) }} {{ separator }}</li>
{% endif %}

<li title="{{ page.word_count }} words">&nbsp;{{ page.reading_time }} min read</li>
Expand All @@ -31,7 +31,7 @@
{% endif %}

{% if page.updated %}
</ul><ul class="meta last-updated"><li>Last updated on {{ page.updated | date(format=config.extra.date_format | default (value="%d %B %Y")) }}</li>
</ul><ul class="meta last-updated"><li>Last updated on {{ macros_format_date::format_date(date=page.updated, short=true) }}</li>
{% endif %}
</ul>

Expand Down
33 changes: 33 additions & 0 deletions templates/macros/format_date.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% macro format_date(date, short) %}

{% if config.extra.short_date_format %}
{{ date | date(format=config.extra.short_date_format) }}
{% elif config.extra.long_date_format %}
{{ date | date(format=config.extra.long_date_format) }}
{% else %}
{% set day = date | date(format='%-d') | int %}

{% if day in [11, 12, 13] %}
{% set suffix = "th" %}
{% else %}
{% set last_digit = day % 10 %}
{% if last_digit == 1 %}
{% set suffix = "st" %}
{% elif last_digit == 2 %}
{% set suffix = "nd" %}
{% elif last_digit == 3 %}
{% set suffix = "rd" %}
{% else %}
{% set suffix = "th" %}
{% endif %}
{% endif %}

{# Return the date. #}
{% if short == true %}
{{ date | date(format="%-d") }}{{ suffix }} {{ date | date(format="%b %Y") }}
{% else %}
{{ date | date(format="%-d") }}{{ suffix }} {{ date | date(format="%B %Y") }}
{% endif %}
{% endif %}

{% endmacro %}
2 changes: 1 addition & 1 deletion templates/macros/list_posts.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

{% if post.date %}
<div class="date">
{{ post.date | date(format=config.extra.date_format | default (value="%d %B %Y")) }}
{{ macros_format_date::format_date(date=post.date, short=false) }}
</div>
{% endif %}

Expand Down
4 changes: 3 additions & 1 deletion templates/section.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

{% block main_content %}

<main>
{% if section.extra.section_path -%}
{% set extra_section = get_section(path=section.extra.section_path) %}
{% endif -%}
Expand All @@ -10,7 +11,7 @@
{{ macros_page_desc::page_desc(desc=section.extra.header, page=section) }}
{% endif -%}

<main class="list">
<div class="list">
<div>
{{ macros_page_header::page_header(title=section.title) }}
</div>
Expand All @@ -23,6 +24,7 @@

{% set max = section.extra.max_posts | default(value=999999) %}
{{ macros_list_posts::list_posts(posts=pages, max=max) }}
</div>
</main>

{% if paginator %}
Expand Down

0 comments on commit 9887fb0

Please sign in to comment.