✨ feat(social-media-cards): support relative paths (#163)
This commit is contained in:
parent
b797879403
commit
049babbd97
@ -92,13 +92,32 @@
|
|||||||
{# 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="") -%}
|
{%- 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 %}
|
{% if social_media_card %}
|
||||||
<meta property="og:image" content="{{ get_url(path=social_media_card, cachebust=true) }}" />
|
{# Try to construct the image path relative to the current page #}
|
||||||
{%- set meta = get_image_metadata(path=social_media_card, allow_missing=true) -%}
|
{% set colocated_path = page.colocated_path | default(value="") %}
|
||||||
{%- if meta -%}
|
{% set file_path = colocated_path ~ social_media_card %}
|
||||||
<meta property="og:image:width" content="{{ meta.width }}" />
|
|
||||||
<meta property="og:image:height" content="{{ meta.height }}" />
|
{# Fetch metadata to verify image existence at the relative path #}
|
||||||
{%- endif -%}
|
{%- set meta = get_image_metadata(path=file_path, allow_missing=true) -%}
|
||||||
<meta name="twitter:image" content="{{ get_url(path=social_media_card, cachebust=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" />
|
<meta name="twitter:card" content="summary_large_image" />
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user