In my Jekyll post I'm using strip_html
to display a brief 50-word intro of the post on the main page:
<p>{{ post.content | strip_html | truncatewords:50 }}</p>
strip_html
removes all HTML from this excerpt. But I'd like so include some HTML, specifically <i>
and <p>
.
Is there a configuration (_config.yaml
) for doing that, or is there a limitation where strip_html
doesn't have customization?
AFAIK there is no built-in way to customize strip_html
.
If you have an exclusive -- and not so long -- list of your wanted tags, you may first use replace
on the tags you want to keep to replace them with non html markers, then use strip_html
, and replace
again to get back the html:
{% assign preprocessed_content=post.content | replace: '<p>', '__p__' %}
{% assign preprocessed_content=preprocessed_content | replace: '</p>', '__/p__' %}
{% assign truncated_content=preprocessed_content | strip_html | truncatewords:50 %}
{% assign cleaned_content=truncated_content | replace: '__p__', '<p>' %}
{% assign cleaned_content=cleaned_content | replace: '__/p__', '</p>' %}
<p>{{ cleaned_content }}</p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With