{%- macro breadcrumb_text(text) -%}
    {%- if config.kerko.breadcrumb.text_max_length -%}
        {{- text|striptags|truncate(config.kerko.breadcrumb.text_max_length, leeway=config.kerko.breadcrumb.text_max_length_leeway)|escape -}}
    {%- else -%}
        {{- text -}}
    {%- endif -%}
{%- endmacro %}

{%- macro breadcrumb_items(links) %}
    {#-
        Display breadcrumb items.

        Order of appearance:
        - The breadcrumb base, from configuration.
        - The given links (items must have 'url', 'text', and 'new_window'
          attributes), except for the last one, which may be just a string.
    #}
    {%- for link in config.kerko_composer.link_groups.breadcrumb_base.get_ordered_links() + links|default([]) %}
        {%- if loop.last %}
            {% if config.kerko.breadcrumb.include_current -%}
                <li class="breadcrumb-item active" aria-current="page">
                    {%- if link.text -%}
                        {{- breadcrumb_text(link.text|escape) -}}
                    {%- else -%}
                        {{- breadcrumb_text(link) -}}
                    {%- endif -%}
                </li>
            {%- endif -%}
        {%- else %}
            <li class="breadcrumb-item">
                <a href="{{ link.url }}" {% if link.new_window %}target="_blank" rel="noopener"{% endif %}>{{ breadcrumb_text(link.text) }}</a>
            </li>
        {%- endif %}
    {%- endfor %}
{%- endmacro %}

{%- macro breadcrumb(links) %}
    {%- if config.kerko.breadcrumb.enabled %}
        {%- set items = breadcrumb_items(links) %}
        {%- if items %}
            <div class="row">
                <nav class="col d-print-none" aria-label="{{ _('Breadcrumb') }}">
                    <ol class="breadcrumb">
                        {{ items }}
                    </ol>
                </nav>
            </div>
        {%- endif %}
    {%- endif %}
{%- endmacro %}
