Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{% comment %}
Remove orphans from a string of text by adding a non-breaking space between the last two words.
Usage: {% include 'adopt' string: 'my string' %}
{% endcomment %}
{%- assign string_parts = string | split: ' ' -%}
{%- assign last_part = string_parts | last -%}
{% if last_part contains ' ' %}
{{ string }}
{% else %}
{%- assign last_part_nbsp = last_part | prepend: ' ' -%}
{%- assign string_nbsp = string | replace: last_part, last_part_nbsp | replace: '  ', ' ' -%}
{{ string_nbsp }}
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment