Skip to content

Instantly share code, notes, and snippets.

@stewartknapman
Created May 1, 2018 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stewartknapman/def11aefd1a95a6028139d5df4b210e8 to your computer and use it in GitHub Desktop.
Save stewartknapman/def11aefd1a95a6028139d5df4b210e8 to your computer and use it in GitHub Desktop.
{% 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