Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll strip_html but allow certain HTML tags

Tags:

jekyll

liquid

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?

like image 622
Matt Smith Avatar asked Sep 25 '16 19:09

Matt Smith


1 Answers

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>
like image 172
rgmt Avatar answered Sep 28 '22 15:09

rgmt