How can I detect   from collection.description?

Chungki
Visitor
1 0 0

Hi, 

I'm coding in Liquid for my theme in order to search given keyword from collection.description. The problem is that the collection.description which is written by richtext editor has unintended   and this &nbsp doesn't work same as normal space. I've been struggling so hard to find any helpful information from Shopify Community, but I cannot make it.

 

Here's a simplified example to describe the problem. Let's say the collection.description is "<p>Hello my&nbsp;world</p>" (You can see the same result only if the object contains &nbsp;):

 

<!-- Code -->
{% assign keyword1 = " " %}
{% assign keyword2 = "&nbsp;" %}
{% assign keyword3 = "Hello my" %}
{% assign keyword4 = "my&nbsp;world" %}

{% if collection.description contains keyword1 %} TRUE {% else %} FALSE {% endif %}
{% if collection.description contains keyword2 %} TRUE {% else %} FALSE {% endif %}
{% if collection.description contains keyword3 %} TRUE {% else %} FALSE {% endif %}
{% if collection.description contains keyword4 %} TRUE {% else %} FALSE {% endif %}
<!-- Result -->
TRUE
FALSE
TRUE
FALSE

 

 

It happens only if &nbsp; is added by richtext editor (or any Shopify's end). If you replace the collection.description with another String variable of same value, all cases above return TRUE as follows:

 

<!-- Code -->
{% assign desc = "Hello my&nbsp;world" %}

{% if desc contains keyword1 %} TRUE {% else %} FALSE {% endif %}
{% if desc contains keyword2 %} TRUE {% else %} FALSE {% endif %}
{% if desc contains keyword3 %} TRUE {% else %} FALSE {% endif %}
{% if desc contains keyword4 %} TRUE {% else %} FALSE {% endif %}
<!-- Result -->
TRUE
TRUE
TRUE
TRUE

 

 

Because of this feature, I cannot use some Liquid filters with collection.description such as contains, replace and remove. Furthermore, this can happen not only with collection description, but also with product description, page content, and blog post. 

Anybody who can figure out?

Reply 1 (1)

max-at-latori
Shopify Partner
1 0 0

+1

 

I also can't manage to strip/remove/replace the additional `&nbsp;` at the end of my `product.description`.

 

So far I tried

  • | strip_html
  • | strip
  • | replace: "&nbsp;", ""
  • | remove: "&nbsp;"
  • | remove: "\0026nbsp;"

LOL, while typing this I tried one more thing:

 

{%- assign stripped_description = product.description | remove: ' ' -%}

strip nbsp.png

 

Just put a non-breaking space in there. Mac: ⌥ + Space

VSCode will warn you that it's bad practice, but 🤷🏻‍

 

For your example it works as well.

This is so ridiculous …