mirror of
https://github.com/welpo/tabi.git
synced 2025-10-11 07:46:15 +02:00
🐛 fix: support relative paths in inherited social media card
When a page inherits social_media_card from its parent, look for the image in the parent directory before trying the current page path. This fixes the incorrect path resolution when social_media_card is set on a parent page. The template now tries paths in this order: 1. Current page directory 2. Parent directory 3. Absolute path
This commit is contained in:
parent
a7833299ff
commit
2025b6a6f2
2 changed files with 45 additions and 30 deletions
|
@ -102,36 +102,7 @@
|
||||||
<meta property="og:type" content="article" />
|
<meta property="og:type" content="article" />
|
||||||
|
|
||||||
{# Image for social media sharing #}
|
{# Image for social media sharing #}
|
||||||
{%- set social_media_card = macros_settings::evaluate_setting_priority(setting="social_media_card", page=page | default(value=""), section=section | default(value=""), default_global_value="") -%}
|
{%- include "partials/social_media_images.html" -%}
|
||||||
{% if social_media_card %}
|
|
||||||
{# Try to construct the image path relative to the current page #}
|
|
||||||
{% set colocated_path = page.colocated_path | default(value="") %}
|
|
||||||
{% set file_path = colocated_path ~ social_media_card %}
|
|
||||||
|
|
||||||
{# Fetch metadata to verify image existence at the relative path #}
|
|
||||||
{%- set meta = get_image_metadata(path=file_path, allow_missing=true) -%}
|
|
||||||
|
|
||||||
{# Check if relative path exists, else try absolute path #}
|
|
||||||
{% if meta %}
|
|
||||||
{% set final_path = file_path %}
|
|
||||||
{% else %}
|
|
||||||
{# If the relative path didn't work, try fetching metadata for the absolute path #}
|
|
||||||
{% set meta = get_image_metadata(path=social_media_card, allow_missing=true) %}
|
|
||||||
{% if meta %}
|
|
||||||
{% set final_path = social_media_card %}
|
|
||||||
{% else %}
|
|
||||||
{# Throw an error if the image doesn't exist at either path #}
|
|
||||||
{{ throw(message="Could not get metadata for the specified social media card image in page " ~ page.path ~ ". Attempted relative path: '" ~ file_path ~ "' and absolute path: '" ~ social_media_card ~ "'. Ensure the file exists at one of these locations.") }}
|
|
||||||
{% endif %}
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
{# Generate the social media meta tags #}
|
|
||||||
<meta property="og:image" content="{{ get_url(path=final_path, cachebust=true) }}" />
|
|
||||||
<meta property="og:image:width" content="{{ meta.width }}" />
|
|
||||||
<meta property="og:image:height" content="{{ meta.height }}" />
|
|
||||||
<meta name="twitter:image" content="{{ get_url(path=final_path, cachebust=true) }}" />
|
|
||||||
<meta name="twitter:card" content="summary_large_image" />
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
{# Add og:locale and hreflang tags for multilingual sites #}
|
{# Add og:locale and hreflang tags for multilingual sites #}
|
||||||
{%- if config.languages | length > 0 and current_url %}
|
{%- if config.languages | length > 0 and current_url %}
|
||||||
|
|
44
templates/partials/social_media_images.html
Normal file
44
templates/partials/social_media_images.html
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
{# Image for social media sharing #}
|
||||||
|
{%- set social_media_card = macros_settings::evaluate_setting_priority(setting="social_media_card", page=page | default(value=""), section=section | default(value=""), default_global_value="") -%}
|
||||||
|
{% if social_media_card %}
|
||||||
|
{# Try to find the image in current page, parent directories, and as absolute path #}
|
||||||
|
{% set page_path = page.colocated_path | default(value="") %}
|
||||||
|
{% set current_path = page_path ~ social_media_card %}
|
||||||
|
|
||||||
|
{# Try parent path by removing the last directory component #}
|
||||||
|
{% set parent_path = page_path | split(pat="/") | slice(end=-2) | join(sep="/") %}
|
||||||
|
{% if parent_path %}
|
||||||
|
{% set parent_path = parent_path ~ "/" %}
|
||||||
|
{% endif %}
|
||||||
|
{% set parent_relative_path = parent_path ~ social_media_card %}
|
||||||
|
|
||||||
|
{# Check all possible locations #}
|
||||||
|
{%- set current_meta = get_image_metadata(path=current_path, allow_missing=true) -%}
|
||||||
|
{%- set parent_meta = get_image_metadata(path=parent_relative_path, allow_missing=true) -%}
|
||||||
|
{%- set absolute_meta = get_image_metadata(path=social_media_card, allow_missing=true) -%}
|
||||||
|
|
||||||
|
{% if current_meta %}
|
||||||
|
{% set final_path = current_path %}
|
||||||
|
{% set meta = current_meta %}
|
||||||
|
{% elif parent_meta %}
|
||||||
|
{% set final_path = parent_relative_path %}
|
||||||
|
{% set meta = parent_meta %}
|
||||||
|
{% elif absolute_meta %}
|
||||||
|
{% set final_path = social_media_card %}
|
||||||
|
{% set meta = absolute_meta %}
|
||||||
|
{% else %}
|
||||||
|
{# Throw an error if the image doesn't exist at any path #}
|
||||||
|
{{ throw(message="Could not find social media card image. Tried:
|
||||||
|
1. Current page path: '" ~ current_path ~ "'
|
||||||
|
2. Parent page path: '" ~ parent_relative_path ~ "'
|
||||||
|
3. Absolute path: '" ~ social_media_card ~ "'
|
||||||
|
Please ensure the file exists at one of these locations.") }}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{# Generate the social media meta tags #}
|
||||||
|
<meta property="og:image" content="{{ get_url(path=final_path, cachebust=true) }}" />
|
||||||
|
<meta property="og:image:width" content="{{ meta.width }}" />
|
||||||
|
<meta property="og:image:height" content="{{ meta.height }}" />
|
||||||
|
<meta name="twitter:image" content="{{ get_url(path=final_path, cachebust=true) }}" />
|
||||||
|
<meta name="twitter:card" content="summary_large_image" />
|
||||||
|
{% endif %}
|
Loading…
Add table
Add a link
Reference in a new issue