{% extends "base.html" %}

{% block main_content %}
{%- set separator = config.extra.separator | default(value="•") -%}

{%- set rel_attributes = macros_rel_attributes::rel_attributes() | trim -%}

{%- if config.markdown.external_links_target_blank -%}
    {%- set blank_target = "target=_blank" -%}
{%- else -%}
    {%- set blank_target = "" -%}
{%- endif -%}

{# Debugging #}
{# {% set last_ancestor = page.ancestors | slice(start=-1) %}
{% set current_section = get_section(path=last_ancestor.0) %}

{% set settings_to_test = [
    "footnote_backlinks",
    "katex",
    "quick_navigation_buttons",
    "show_reading_time",
    "show_remote_changes",
    "toc",
] %}

<table>
    <thead>
        <tr>
            <th>setting</th>
            <th>page</th>
            <th>section</th>
            <th>config</th>
            <th>macro output</th>
        </tr>
    </thead>
    <tbody>
        {% for setting in settings_to_test %}
            <tr>
                <td><code>{{ setting }}</code></td>
                <td>{{ page.extra[setting] | default(value="⬛") }}</td>
                <td>{{ current_section.extra[setting] | default(value="⬛") }}</td>
                <td>{{ config.extra[setting] | default(value="⬛") }}</td>
                <td>{{ macros_settings::evaluate_setting_priority(setting=setting, page=page) }}</td>
            </tr>
        {% endfor %}
    </tbody>
</table> #}
{# End debugging #}

<main>
    <article>
        <h1 class="article-title">
            {{ page.title }}
        </h1>

        <ul class="meta">
            {% if page.draft %}
                <li class="draft-label">{{ macros_translate::translate(key="draft", default="DRAFT", language_strings=language_strings) }}</li>
            {% endif %}

            {% if page.date %}
                <li>{{ macros_format_date::format_date(date=page.date, short=true, language_strings=language_strings) }}</li>
            {% endif %}

            {# page settings override config settings #}
            {% if macros_settings::evaluate_setting_priority(setting="show_reading_time", page=page, default_global_value=true) == "true" %}
                {{ separator }} <li title="{{ page.word_count }} {{ macros_translate::translate(key="words", default="words", language_strings=language_strings) }}">{{ page.reading_time }}&nbsp;{{ macros_translate::translate(key="min_read", default="min read", language_strings=language_strings) }}</li>
            {% endif %}

            {%- if page.taxonomies and page.taxonomies.tags -%}
                {{ separator }}&nbsp;<li>{{- macros_translate::translate(key="tags", default="tags", language_strings=language_strings) | capitalize -}}:&nbsp;</li>
                {%- for tag in page.taxonomies.tags -%}
                    <li><a href={{ get_taxonomy_url(kind='tags', name=tag, lang=lang) | safe }}>{{ tag }}</a>
                    {%- if not loop.last -%}
                        ,&nbsp;
                    {%- endif -%}
                    </li>
                {%- endfor -%}
            {%- endif -%}

            {% if page.updated %}
                </ul><ul class="meta last-updated"><li>{{ macros_translate::translate(key="last_updated_on", default="Last updated on", language_strings=language_strings) }} {{ macros_format_date::format_date(date=page.updated, short=true, language_strings=language_strings) }}</li>
                {# Show link to remote changes if enabled #}
                {% if config.extra.remote_repository_url and macros_settings::evaluate_setting_priority(setting="show_remote_changes", page=page, default_global_value=true) == "true" %}
                    {{ separator }}
                    <li><a href="{{ macros_create_history_url::create_history_url(relative_path=page.relative_path) }}" {{ blank_target }} rel="{{ rel_attributes }}">{{ macros_translate::translate(key="see_changes", default="See changes", language_strings=language_strings) }}<small> ↗</small></a></li>
                {% endif %}
            {% endif %}
        </ul>

        {% if page.extra.tldr %}
            <div class="tldr">
                <h3>TL;DR:</h3>
                <p>{{ page.extra.tldr }}</p>
            </div>
        {% endif %}

        {# Optional table of contents below the header #}
        {% if page.toc and macros_settings::evaluate_setting_priority(setting="toc", page=page, default_global_value=false) == "true" %}
            {{ macros_toc::toc(page=page, header=true, language_strings=language_strings) }} 
        {% endif %}

        <section class="body">
            {# The replace pattern is used to enable arbitrary locations for the Table of Contents #}
            {# This is Philipp Oppermann's workaround: https://github.com/getzola/zola/issues/584#issuecomment-474329637 #}
            {{ page.content | replace(from="<!-- toc -->", to=macros_toc::toc(page=page, header=false, language_strings=language_strings)) | safe }}
        </section>

        {# Check if comments are enabled #}
        {% set giscus_enabled = config.extra.giscus.enabled_for_all_posts or page.extra.giscus %}
        {% set utterances_enabled = config.extra.utterances.enabled_for_all_posts or page.extra.utterances %}
        {% set hyvortalk_enabled = config.extra.hyvortalk.enabled_for_all_posts or page.extra.hyvortalk %}
        {% set isso_enabled = config.extra.isso.enabled_for_all_posts or page.extra.isso %}

        {# Ensure only one comment system is enabled #}
        {# Counter for enabled comment systems #}
        {% set enabled_systems = 0 %}

        {# Check and count the enabled comment systems #}
        {% if giscus_enabled %}
            {% set comment_system = "giscus" %}
            {% set enabled_systems = enabled_systems + 1 %}
        {% endif %}
        {% if utterances_enabled %}
            {% set comment_system = "utterances" %}
            {% set enabled_systems = enabled_systems + 1 %}
        {% endif %}
        {% if hyvortalk_enabled %}
            {% set comment_system = "hyvortalk" %}
            {% set enabled_systems = enabled_systems + 1 %}
        {% endif %}
        {% if isso_enabled %}
            {% set comment_system = "isso" %}
            {% set enabled_systems = enabled_systems + 1 %}
        {% endif %}

        {# Ensure only one comment system is enabled #}
        {% if enabled_systems > 1 %}
            {{ throw(message="ERROR: Multiple comment systems have been enabled for the same page. Check your config.toml and individual page settings to ensure only one comment system is activated at a time.") }}
        {% endif %}

        {% if comment_system %}
            {% set automatic_loading = config.extra[comment_system].automatic_loading %}
            {{ macros_add_comments::add_comments(comment_system=comment_system, automatic_loading=automatic_loading) }}
        {% endif %}

    </article>
</main>

{# Quick navigation buttons #}
{% if macros_settings::evaluate_setting_priority(setting="quick_navigation_buttons", page=page, default_global_value=false) == "true" %}
    <div id="button-container">
        {# Button to go show a floating Table of Contents #}
        {% if page.toc %}
            <div id="toc-floating-container">
                <input type="checkbox" id="toc-toggle" class="toggle"/>
                <label for="toc-toggle" class="overlay"></label>
                <label for="toc-toggle" id="toc-button" class="button" title="Toggle Table of Contents" aria-label="toggle Table of Contents">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -960 960 960"><path d="M414.82-193.094q-18.044 0-30.497-12.32-12.453-12.319-12.453-30.036t12.453-30.086q12.453-12.37 30.497-12.37h392.767q17.237 0 29.927 12.487 12.69 12.486 12.69 30.203 0 17.716-12.69 29.919t-29.927 12.203H414.82Zm0-244.833q-18.044 0-30.497-12.487Q371.87-462.9 371.87-480.45t12.453-29.92q12.453-12.369 30.497-12.369h392.767q17.237 0 29.927 12.511 12.69 12.512 12.69 29.845 0 17.716-12.69 30.086-12.69 12.37-29.927 12.37H414.82Zm0-245.167q-18.044 0-30.497-12.32t-12.453-30.037q0-17.716 12.453-30.086 12.453-12.369 30.497-12.369h392.767q17.237 0 29.927 12.486 12.69 12.487 12.69 30.203 0 17.717-12.69 29.92-12.69 12.203-29.927 12.203H414.82ZM189.379-156.681q-32.652 0-55.878-22.829t-23.226-55.731q0-32.549 23.15-55.647 23.151-23.097 55.95-23.097 32.799 0 55.313 23.484 22.515 23.484 22.515 56.246 0 32.212-22.861 54.893-22.861 22.681-54.963 22.681Zm0-245.167q-32.652 0-55.878-23.134-23.226-23.135-23.226-55.623 0-32.487 23.467-55.517t56.12-23.03q32.102 0 54.721 23.288 22.62 23.288 22.62 55.775 0 32.488-22.861 55.364-22.861 22.877-54.963 22.877Zm-.82-244.833q-32.224 0-55.254-23.288-23.03-23.289-23.03-55.623 0-32.333 23.271-55.364 23.272-23.03 55.495-23.03 32.224 0 55.193 23.288 22.969 23.289 22.969 55.622 0 32.334-23.21 55.364-23.21 23.031-55.434 23.031Z"/></svg>
                </label>
                <div class="toc-content">
                    {{ macros_toc::toc(page=page, header=false, language_strings=language_strings) }}
                </div>
            </div>
        {% endif %}

        {# Button to go to the comment section #}
        {% if comment_system %}
            <a href="#comments" id="comments-button" title="Go to comment section">
                <svg viewBox="0 0 20 20" fill="currentColor"><path d="M18 10c0 3.866-3.582 7-8 7a8.841 8.841 0 01-4.083-.98L2 17l1.338-3.123C2.493 12.767 2 11.434 2 10c0-3.866 3.582-7 8-7s8 3.134 8 7zM7 9H5v2h2V9zm8 0h-2v2h2V9zM9 9h2v2H9V9z" clip-rule="evenodd" fill-rule="evenodd"/></svg>
            </a>
        {% endif %}

        {# Button to go to the top of the page #}
        <a href="#" id="top-button" title="Go to top of page">
            <svg viewBox="0 0 20 20" fill="currentColor"><path d="M3.293 9.707a1 1 0 010-1.414l6-6a1 1 0 011.414 0l6 6a1 1 0 01-1.414 1.414L11 5.414V17a1 1 0 11-2 0V5.414L4.707 9.707a1 1 0 01-1.414 0z"/></svg>
        </a>
    </div>
{% endif %}

{# Add KaTeX functionality #}
{%- if macros_settings::evaluate_setting_priority(setting="katex", page=page, default_global_value=false) == "true" -%}
    <link rel="stylesheet" href="{{ get_url(path='katex.min.css', trailing_slash=false) | safe }}">
    <script defer src="{{ get_url(path='js/katex.min.js', trailing_slash=false) | safe }}"></script>
{%- endif -%}

{# Add copy button to code blocks #}
{%- if macros_settings::evaluate_setting_priority(setting="copy_button", page=page, default_global_value=true) == "true" -%}
    <script defer src="{{ get_url(path='js/copyCodeToClipboard.min.js', trailing_slash=false) | safe }}"/></script>
{%- endif -%}

{# Add backlinks to footnotes #}
{%- if macros_settings::evaluate_setting_priority(setting="footnote_backlinks", page=page, default_global_value=false) == "true" -%}
    <script defer src="{{ get_url(path='js/footnoteBacklinks.min.js', trailing_slash=false | safe )}}"/></script>
{%- endif -%}

{% endblock main_content %}
