2016-09-25 71 views
2

在我的化身文章中,我使用strip_html顯示後的主網頁上簡短的50個字的介紹:傑基爾strip_html但允許某些HTML標記

<p>{{ post.content | strip_html | truncatewords:50 }}</p> 

strip_html移除此摘錄所有HTML。但我想這樣包括一些HTML,特別是<i><p>

是否有配置(_config.yaml)用於這樣做,或者是否存在strip_html沒有自定義的限制?

回答

2

AFAIK沒有內置的方式來定製strip_html

如果你有一個獨特的 - 而不是那麼久 - 你想要的標籤的列表,你可能首先使用上要保持標籤replace與非HTML標記來替換它們,然後再次使用strip_htmlreplace找回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> 
+0

謝謝,我會試試看。 –