Skip to content

Commit

Permalink
✨ feat: generate commit history URL for updated articles
Browse files Browse the repository at this point in the history
- Introduce the `create_history_url` macro in Tera to generate URLs.
- Add TOML configuration for specifying remote repository details.
- Integrate the new feature in the page meta for displaying a link to view changes.
  • Loading branch information
welpo committed Aug 14, 2023
1 parent 0884370 commit 8461285
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ last_updated_on = "Última actualización el"
show_original_quote = "Mostrar cita original"
show_translation = "Mostrar traducción"
load_comments = "Cargar comentarios"
see_changes = "Ver cambios"
# Quotation marks for multilingual quote shortcode.
open_quotation_mark = "«"
close_quotation_mark = "»"
Expand Down Expand Up @@ -113,6 +114,7 @@ last_updated_on = "Última actualizació el"
show_original_quote = "Mostra la cita original"
show_translation = "Mostra la traducció"
load_comments = "Carregar comentaris"
see_changes = "Veure canvis"
# Quotation marks for multilingual quote shortcode.
open_quotation_mark = "«"
close_quotation_mark = "»"
Expand All @@ -133,6 +135,17 @@ language_name.ca = "Català"
language_name.en = "English"
language_name.es = "Español"

# Remote repository for your Zola site.
# Only used to link to the commit history of updated posts, right next to the updated date.
# Supports GitHub, GitLab and Gitea.
remote_repository_url = "https://github.com/welpo/tabi"
# Set this to "auto" to try and auto-detect the platform based on the repository URL.
# Accepted values are "github", "gitlab", and "gitea".
# Defaults to "auto".
remote_repository_git_platform = "auto"
# Branch in the repo hosting the Zola site. Defaults to "main".
remote_repository_branch = "main"

# Enable JavaScript theme toggler to allow users to switch between dark/light mode.
# Also enables automatic switching based on user's OS-level theme settings.
# If disabled, your site will only use the theme specified in the `default_theme` variable.
Expand Down
1 change: 1 addition & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{% import "macros/format_date.html" as macros_format_date %}
{% import "macros/add_comments.html" as macros_add_comments %}
{% import "macros/table_of_contents.html" as macros_toc %}
{% import "macros/create_history_url.html" as macros_create_history_url %}

<!DOCTYPE html>
<html lang="{{ lang }}" {% if config.extra.default_theme -%}
Expand Down
3 changes: 3 additions & 0 deletions templates/macros/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@

{% if page.updated %}
</ul><ul class="meta last-updated"><li>{%- if lang != config.default_language %} {{ trans(key="last_updated_on" | safe, lang=lang) }} {% else %} Last updated on {% endif %} {{ macros_format_date::format_date(date=page.updated, short=true) }}</li>
{% if config.extra.remote_repository_url %}
<li>&nbsp;{{ separator }}&nbsp;<a href="{{ macros_create_history_url::create_history_url(relative_path=page.relative_path) }}" target="_blank" rel="noopener noreferrer">{%- if lang != config.default_language -%}{{ trans(key="see_changes" | safe, lang=lang) }}{% else %}See changes{%- endif -%}<small></small></a></li>
{% endif %}
{% endif %}
</ul>

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

{% set repository_url = config.extra.remote_repository_url | trim_end_matches(pat='/') %}
{% set branch = config.extra.remote_repository_branch | default(value="main") %}
{% set git_platform = config.extra.remote_repository_git_platform | default(value="auto") %}

{# Auto-detect the git platform based on the URL#}
{% if git_platform == "auto" %}
{% if repository_url is containing("github.") %}
{% set git_platform = "github" %}
{% elif repository_url is containing("gitea.") %}
{% set git_platform = "gitea" %}
{% elif repository_url is containing("gitlab.") %}
{% set git_platform = "gitlab" %}
{% endif %}
{% endif %}

{# Generate the commit history URL based on the git platform #}
{% if git_platform == "github" %}
{{ repository_url ~ '/commits/' ~ branch ~ '/content/' }}{{ relative_path | urlencode }}
{% elif git_platform == "gitea" %}
{{ repository_url ~ '/commits/branch/' ~ branch ~ '/' }}{{ relative_path | urlencode }}
{% elif git_platform == "gitlab" %}
{{ repository_url ~ '/-/commits/' ~ branch ~ '/' }}{{ relative_path | urlencode }}
{% else %}
{# Throw an error with a direct link to report a bug for unsupported or unspecified platforms #}
{{ throw(message="ERROR: Unknown, unsupported, or unspecified git platform. If you're using a custom domain, please specify the 'git_platform' in the config. If you think this is a bug, report it [here](https://github.com/welpo/tabi/issues/new?assignees=&labels=bug&template=bug_report.md&title=Unsupported%20Git%20Platform%20Detected).") }}
{% endif %}

{% endmacro %}
11 changes: 11 additions & 0 deletions theme.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ language_name.ca = "Català"
language_name.en = "English"
language_name.es = "Español"

# Remote repository for your Zola site.
# Only used to link to the commit history of updated posts, right next to the updated date.
# Supports GitHub, GitLab and Gitea.
remote_repository_url = "https://github.com/welpo/tabi"
# Set this to "auto" to try and auto-detect the platform based on the repository URL.
# Accepted values are "github", "gitlab", and "gitea".
# Defaults to "auto".
remote_repository_git_platform = "auto"
# Branch in the repo hosting the Zola site. Defaults to "main".
remote_repository_branch = "main"

# Enable JavaScript theme toggler to allow users to switch between dark/light mode.
# Also enables automatic switching based on user's OS-level theme settings.
# If disabled, your site will only use the theme specified in the `default_theme` variable.
Expand Down

0 comments on commit 8461285

Please sign in to comment.