Skip to content

Instantly share code, notes, and snippets.

@suture
Last active September 8, 2015 14:20
Show Gist options
  • Save suture/91d420be49ee1656353f to your computer and use it in GitHub Desktop.
Save suture/91d420be49ee1656353f to your computer and use it in GitHub Desktop.
Shopify - Strip Image from Excerpt
{% assign sizes = '_pico.,_icon.,_thumb.,_small.,_compact.,_medium.,_large.,_grande.,_1024x1024.,_2048x2048.,_master' | split: ',' %}
{% assign image_src = null %}
{% assign image_alt = null %}
{% if article.excerpt contains "<img" %}
{% assign image_src = article.excerpt | split: 'src="' %}
{% assign image_src = image_src[1] | split: '"' | first | replace: '//cdn', 'http://cdn' | replace: 'http:http://', 'http://' | remove: 'https:' %}
{% for s in sizes %}
{% assign image_src = image_src | replace: s, '_1024x1024.' %}
{% endfor %}
{% assign image_alt = article.excerpt | split: 'alt="' %}
{% assign image_alt = image_alt[1] | split: '"' | first %}
{% assign img_tag_junk = article.excerpt | split: '<img' %}
{% assign img_tag_junk = img_tag_junk[1] | split: '>' | first %}
{% endif %}
{% if image_src != null %}
<img src="{{ image_src }}" alt="{% if image_alt != null %}{{ image_alt }}{% else %}{{ article.title }}{% endif %}">
{% endif %}
{{ article.excerpt | replace_first: '<img', '<!-- Image Stripped ' | replace_first: img_tag_junk, '--' }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment