Is there a way to iterate through children of rich_text_field?
Something like this (my code doesn’t work) could be useful to build a per-product FAQ, building accordion interface around the parsed rich-text:
{% for item in product.metafields.custom.faq.children %}
{% if item.type == 'heading' %}
{% comment %} Print HTML for heading {% endcomment %}
#### {{ item.children[0].value }}
{% endif %}
{% if item.type == 'paragraph' %}
{% comment %} Print HTML for paragraphs {% endcomment %}
{{ item.children[0].value }}
{% endif %}
{% endfor %}
A solution/npm package, for anyone look for converting a Metaobject’s rich text type to HTML using Javascript (i.e. you are querying the Metaobject using the storefront API or using Hydrogen. I built the package to make it super easy to convert it Shopify rich text AST to HTML.
I am posting this in this thread as I had searched for a Shopify Official solution far and wide without success ( one would think they would have one since they must be converting it themselves internally. So I just had to build it myself. We have been using it since internally since Feb. without any issues. Hopefully this is helpful for those looking far and wide as I did.
Rich Text type:-
metafield_tag filter is specifically needed for rich text metafields in Shopify.
Rich text metafields store formatted text content, including HTML tags for elements like paragraphs, bold text, or lists.
The metafield_tag filter acts as a parser, interpreting these HTML tags and converting them into the actual formatted text you see on the storefront.
Other types:-
All other metafield types don’t contain HTML tags, so there’s no need for parsing or rendering with metafield_tag. The data is presented directly based on its type.
I can’t agree with you more. Liquid filters would normally the last place I will dig through if I cannot access the correct format no matter how. This one in particular what stops us from putting it on the liquid api metafield / metaobject docs?
Another thing I never feel clear enough is between the type definition in liquid schema and metafield data source types. For instance I want some html from metafield data source and render as it is in liquid. Which type in metafield for the field (likely rich text area or multi lines text) and which type in liquid schema (rich-text)? How may I make them compatible with each other so that I can use the dynamic data source in Customizer?
And don’t even mention the headless communication with metafield yes it requires a package to do the conversion.
lastly please allow me to double ask, what’s the point of not to output html from rich text field?