{%- import "macros/feed.html" as feed_macros -%}
{%- import "macros/post.html" as post_macros -%}

<!doctype html>
<html lang="{{ lang }}">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    {% include "partials/content_security_policy.html" %}
    <title>{%- block title -%}{{ config.title }}{%- endblock -%}</title>

    <link rel="preload" href="{{ get_url(path="fonts/FiraCode-Regular.woff2") }}" as="font" type="font/woff2" crossorigin="anonymous">
    <link rel="preload" href="{{ get_url(path="fonts/FiraCode-Bold.woff2") }}" as="font" type="font/woff2" crossorigin="anonymous">
    <link rel="stylesheet" href="{{ get_url(path="css/style.css", cachebust=true) }}">
    {%- if page.extra.stylesheets or section.extra.stylesheets %}
    {%- for stylesheet in page.extra.stylesheets | default(value=section.extra.stylesheets) %}
    <link rel="stylesheet" href="{{ get_url(path=stylesheet, cachebust=true) }}">
    {% endfor -%}
    {%- endif %}

    {%- if config.extra.favicon %}
    {% set meta = get_image_metadata(path=config.extra.favicon) %}
    {% if meta is defined and meta.mime %}
    {% set mime_type = meta.mime %}
    {% else %}
    {% set mime_type = "image/png" %}
    {% endif %}
    <link rel="icon" type="{{ mime_type | safe }}" href="{{ get_url(path=config.extra.favicon, cachebust=true) }}">
    {% if mime_type is ending_with("png") %}
    {% set resized = resize_image(path=config.extra.favicon, width=180, height=180, op="fit") -%}
    <link rel="apple-touch-icon" href="{{ resized.url | safe }}">
    {% endif %}
    {%- elif config.extra.favicon_emoji %}
    <link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>{{ config.extra.favicon_emoji }}</text></svg>">
    {%- endif %}


    {%- if config.extra.close_responsive_menu_on_resize %}
    <script src="{{ get_url(path="js/auto-close-popover-on-resize.js", cachebust=true) }}" defer></script>
    {%- endif %}
    {%- if page.extra.copy_button | default(value=section.extra.copy_button | default(value=config.extra.copy_button)) %}
    <script src="{{ get_url(path="js/copy-code-to-clipboard.js", cachebust=true) }}" defer></script>
    {%- endif %}
    <script src="{{ get_url(path="js/theme-switcher.js", cachebust=true) }}" defer></script>


    {# Canonical and alternate links -#}
    {% if current_url -%}
    <link rel="canonical" href="{{ current_url | safe }}">
    {% endif -%}
    {%- if config.generate_feeds %}
    {%- for feed in config.feed_filenames -%}
    {{ feed_macros::link_alternate(filename=feed, title=config.title) }}
    {% endfor -%}
    {%- block feeds -%}{%- endblock -%}
    {% endif %}

    {#- Standard meta tags -#}
    {%- if page.authors -%}
    {%- for author in page.authors %}
    <meta name="author" content="{{ author }}">
    {%- endfor -%}
    {%- elif page and config.author %}
    <meta name="author" content="{{ config.author }}">
    {%- endif %}
    {%- block description -%}
    {%- if config.description %}
    <meta name="description" content="{{ config.description }}">
    {%- endif %}
    {%- endblock %}
    {% block robots -%}
    <meta name="robots" content="index, follow">
    {%- endblock %}
    <meta name="theme-color" content="#211f1a">
    <meta name="color-scheme" content="dark">
    <script type="application/ld+json">
    {
    "@context": "https://schema.org",
    "@type": "Person",
    "name": "Raphael Bitton",
    "url": "https://rbitton.com",
    "sameAs": [
        "https://github.com/rbitton1729",
        "https://gitlab.com/rbitton",
        "https://www.reddit.com/user/rbitton",
        "https://www.linkedin.com/in/rbitton1729",
        "https://www.instagram.com/rbitton1729",
        "https://git.rbitton.com/rbitton",
        "https://flights.rbitton.com/share/public"
    ]
    }
    </script>

    {# Social media meta tags -#}
    {#- FIXME: Refactor all of these to blocks/set-blocks when Tera V2 is available. -#}
    {#- See this GitHub comment: https://github.com/Keats/tera/issues/637#issuecomment-2974769236 -#}
    {%- if current_path and current_path == "/" -%}
    {%- set page_title = config.title -%}
    <meta property="og:title" content="{{ page_title }}">
    {% elif current_path -%}
    {%- set page_title = page.title | default(value=section.title | default(value=taxonomy.name | capitalize | default(value=config.title))) -%}
    <meta property="og:title" content="{{ page_title | replace(from='"', to="&quot;") | safe }}">
    {% endif -%}

    {% if page -%}
    <meta property="og:type" content="article">
    {% else -%}
    <meta property="og:type" content="website">
    {% endif -%}

    {%- if page.extra.social_media_image.path or section.extra.social_media_image.path -%}
    {%- set relative_path = page.extra.social_media_image.path | default(value=section.extra.social_media_image.path) -%}
    {%- set base_path = page.colocated_path | default(value=section.relative_path | trim_end_matches(pat="_index.md")) -%}
    {%- set absolute_path = base_path ~ relative_path -%}
    {%- set image_meta = get_image_metadata(path=absolute_path) -%}
    {%- set resized_image = resize_image(path=absolute_path, width=1200, height=675, op="fit") -%}

    <meta property="og:image" content="{{ resized_image.url | safe }}">
    <meta property="og:image:width" content="{{ resized_image.width }}">
    <meta property="og:image:height" content="{{ resized_image.height }}">
    <meta property="og:image:type" content="{{ image_meta.mime | safe }}">
    {% if page.extra.social_media_image.alt_text or page.extra.social_media_image.alt_text -%}
    {%- set alt_text = page.extra.social_media_image.alt_text | default(value=page.extra.social_media_image.alt_text) -%}
    <meta property="og:image:alt" content="{{ alt_text | replace(from='"', to="&quot;") | safe }}">
    {% endif -%}
    <meta name="twitter:card" content="summary_large_image">
    {% endif -%}

    {%- if current_url -%}
    {% set permalink = page.permalink | default(value=section.permalink | default(value=current_url)) -%}
    <meta property="og:url" content="{{ permalink | safe }}">
    {% endif -%}

    {%- if page.description -%}
    <meta property="og:description" content="{{ page.description | markdown(inline=true) | striptags }}">
    {% elif page.summary -%}
    {%- set inline_summary = page.summary | linebreaksbr | replace(from="<br>", to=" ") | striptags -%}
    <meta property="og:description" content="{{ inline_summary | trim_end_matches(pat=".") | safe }}…">
    {%- elif section.description -%}
    <meta property="og:description" content="{{ section.description | markdown(inline=true) | striptags }}">
    {% elif config.description -%}
    <meta property="og:description" content="{{ config.description }}">
    {% endif -%}
    <meta property="og:site_name" content="{{ config.title }}">
    <meta property="og:updated_time" content="{{ now() }}">
    {%- if page.date %}
    <meta property="article:published_time" content="{{ page.date | date(format="%+") }}">
    {%- endif -%}
    {%- if page.updated %}
    <meta property="article:modified_time" content="{{ page.updated | date(format="%+") }}">
    {%- endif -%}
    {%- if page.taxonomies and page.taxonomies.categories %}
    <meta property="article:section" content="{{ page.taxonomies.categories[0] }}">
    {%- endif -%}
    {%- if page.taxonomies and page.taxonomies.tags -%}
    {%- for tag in page.taxonomies.tags %}
    <meta property="article:tag" content="{{ tag }}">
    {%- endfor -%}
    {%- endif -%}
    {%- if config.extra.fediverse_creator %}
    <meta name="fediverse:creator" content="{{ config.extra.fediverse_creator }}">
    {%- endif %}
</head>

<body class="layout-{{ config.extra.layout }}">
    <header class="header">
        {% filter indent -%}{%- filter indent -%}
        {%- block header -%}
        {% include "partials/header.html" %}
        {%- endblock -%}
        {%- endfilter -%}{%- endfilter %}
    </header>
    <main id="main">
        {% block main %}{% endblock %}
    </main>
    <footer class="footer">
        {% filter indent -%}{%- filter indent -%}
        {%- block footer -%}
        {% include "partials/footer.html" %}
        {%- endblock -%}
        {%- endfilter -%}{%- endfilter %}
    </footer>
</body>

</html>
